重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
创新互联是一家专业从事成都网站设计、成都网站建设、网页设计的品牌网络公司。如今是成都地区具影响力的网站设计公司,作为专业的成都网站建设公司,创新互联依托强大的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、营销型网站建设及网站设计开发服务!
import java.awt.event.*;
import javax.swing.*;
public class calculator extends JFrame implements ActionListener {
private boolean i=true,clickable = true ;//这里的clickabel 是用来判断小数点的 如果有小数点的话 clickable则为false就不对小数点进行添加了 i在下面有解释
private double value=0; //这个就是显示出来的值了
String operate = "="; //操作数 默认情况下是=号
JFrame jframe=new JFrame("计算机");
JTextField text1=new JTextField("0");
JButton back = new JButton("退格");
JButton daoshu = new JButton("1/x");
JButton remain = new JButton("%");
JButton PI = new JButton("PI"); //显示圆周率小数点后7位
JButton square = new JButton("x^2");
JButton cube = new JButton("x^3");
JButton plu=new JButton("+");
JButton min=new JButton("-");
JButton mul=new JButton("*");
JButton equ=new JButton("=");
JButton div=new JButton("/");
JButton code=new JButton("+/-");
JButton point=new JButton(".");
JButton num1=new JButton("1");
JButton num2=new JButton("2");
JButton num3=new JButton("3");
JButton num4=new JButton("4");
JButton num5=new JButton("5");
JButton num6=new JButton("6");
JButton num7=new JButton("7");
JButton num8=new JButton("8");
JButton num9=new JButton("9");
JButton num0=new JButton("0");
JButton num00=new JButton("00");
JButton clear=new JButton("C");
calculator(){
jframe.setSize(400, 400);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setBackground(Color.gray);
p.setLayout(new GridLayout(5, 5)); //采用的布局方式为网格布局
PI.addActionListener(this);
daoshu.addActionListener(this);
remain.addActionListener(this);
square.addActionListener(this);
cube.addActionListener(this);
plu.addActionListener(this);
equ.addActionListener(this);
num1.addActionListener(this);
num2.addActionListener(this);
clear.addActionListener(this);
num3.addActionListener(this);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
num0.addActionListener(this);
num00.addActionListener(this);
min.addActionListener(this);
code.addActionListener(this);
div.addActionListener(this);
point.addActionListener(this);
mul.addActionListener(this);
back.addActionListener(this);
jframe.add(p);
p.add(PI);
p.add(code);
p.add(daoshu);
p.add(back);
p.add(clear);
p.add(num7);
p.add(num8);
p.add(num9);
p.add(plu);
p.add(remain);
p.add(num4);
p.add(num5);
p.add(num6);
p.add(min);
p.add(square);
p.add(num1);
p.add(num2);
p.add(num3);
p.add(mul);
p.add(cube);
p.add(num0);
p.add(num00);
p.add(point);
p.add(div);
p.add(equ);
jframe.add(text1,"North");
jframe.setLocation(300, 300);
jframe.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
String str1,str2;
Object soc=evt.getSource();
String s = evt.getActionCommand(); //获取按钮的数据
if(soc==back){ //进行退格操作
str1=text1.getText();
int n=str1.length();
text1.setText(str1.substring(0,n-1));
str2 = text1.getText();
}
else if (s.equals("x^2")) {
str1=text1.getText();
double k = Double.parseDouble(str1);
k *=k;
text1.setText(String.valueOf(k));
}
else if (s.equals("x^3")) {
str1=text1.getText();
double k = Double.parseDouble(str1);
k =k*k*k;
text1.setText(String.valueOf(k));
}
else if(s.equals("PI")){ //显示圆周率
double Pi=3.1415926;
String pi=String.valueOf(Pi);
text1.setText(pi);
}
else if(s.equals("+/-")){ //变换正负号
double a = 0;
str1=text1.getText();
a=Double.parseDouble(str1.trim());
a=-1*a;
str1=String.valueOf(a);
text1.setText(str1);
}
else if(s.equals("1/x")){ //求倒
str1=text1.getText();
double i = 1/Double.parseDouble(str1.trim());
String k = String.valueOf(i);
text1.setText(k);
}
else if(s.equals(".")){ //小数点
clickable=true;
for (int i = 0; i text1.getText().length(); i++)
if ('.' == text1.getText().charAt(i))
{
clickable=false;
break;
} //第一层判断是否里面含有小数点;
if(clickable==true) //第二层判断
text1.setText(text1.getText()+".");
i=false;
}
else if(s.equals("C")){ //清零 所有的数据变回初始值
value = 0;
text1.setText("0");
i=true;
operate = "=" ;
}
else if ('0' = s.charAt(0) s.charAt(0) = '9' ) { //若输入的为数字以及小数点的时候执行下面
if (i) { //判断获得的command是否为操作符
text1.setText(s);
}
else {
text1.setText(text1.getText() + s);
}
i = false;
}
else { //若输入运算操作符的话则执行下面的代码
count(Double.parseDouble(text1.getText().trim())); //这里一开始调用 count()方法的话首先执行的是operate.equals("=")
//执行上个操作符的运算
operate = s;
i = true;
}
}
private void count(double a) { //计算区域
if (operate.equals("+")) {
value += a;
}
else if (operate.equals("-")) {
value -= a;
}
else if (operate.equals("*")) {
value *= a;
}
else if (operate.equals("/")) {
value /= a;
}
else if (operate.equals("%")) {
value %= a;
}
else if (operate.equals("=")) {
value = a;
}
text1.setText(Double.toString(value));
}
public static void main(String[] arg)
{
calculator cal=new calculator();
}
}
这是例子 你可以参考下~监听的 部分
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
public class calculator
{
String str1="0"; //运算数1 初值一定为0 为了程序的安全
String str2="0"; //运算数2
String fh="+"; //运算符
String jg="";//结果
//状态开关 重要
int k1=1;//开关1 用于选择输入方向 将要写入str2或 str2
int k2=1;//开关2 符号键 次数 k21说明进行的是2+3-9+8 这样的多符号运算
int k3=1;//开关3 str1 是否可以被清0 ==1时可以 !=1时不能被清0
int k4=1;//开关4 str2 同上
int k5=1;//开关5 控制小数点可否被录入 ==1时可以 !=1 输入的小数点被丢掉
JButton jicunqi; //寄存器 记录 是否连续按下符号键
Vector vt=new Vector(20,10);
JFrame frame=new JFrame("sunshine---计算器");
JTextField jg_TextField=new JTextField(jg,20);//20列
JButton clear_Button=new JButton("清除");
JButton button0=new JButton("0");
JButton button1=new JButton("1");
JButton button2=new JButton("2");
JButton button3=new JButton("3");
JButton button4=new JButton("4");
JButton button5=new JButton("5");
JButton button6=new JButton("6");
JButton button7=new JButton("7");
JButton button8=new JButton("8");
JButton button9=new JButton("9");
JButton button_Dian=new JButton(".");
JButton button_jia=new JButton("+");
JButton button_jian=new JButton("-");
JButton button_cheng=new JButton("*");
JButton button_chu=new JButton("/");
JButton button_dy=new JButton("=");
public static void main(String[] args)
{
calculator calculator=new calculator();
}
calculator()
{
jg_TextField.setHorizontalAlignment(JTextField.RIGHT );//文本框 右对齐
JPanel pan=new JPanel();
pan.setLayout(new GridLayout(4,4,5,5));//四行四列 边距为5像素
pan.add(button7);
pan.add(button8);
pan.add(button9);
pan.add(button_chu);
pan.add(button4);
pan.add(button5);
pan.add(button6);
pan.add(button_cheng);
pan.add(button1);
pan.add(button2);
pan.add(button3);
pan.add(button_jian);
pan.add(button0);
pan.add(button_Dian);
pan.add(button_dy);
pan.add(button_jia);
pan.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));//pan对象的边距
JPanel pan2=new JPanel();
pan2.add(jg_TextField);
JPanel pan3=new JPanel(); //为什么要 多此一句呢? 因为我不会设置 按钮的大小
pan3.setLayout(new FlowLayout());
pan3.add(clear_Button);
//clear_Button.setSize(10,10);//设置清零按钮的大小 吗的 不好使 !!
frame.setLocation(300, 200); //主窗口 出现在位置
frame.setResizable(false); //不能调大小
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(pan2,BorderLayout.NORTH);
frame.getContentPane().add(pan,BorderLayout.CENTER);
frame.getContentPane().add(pan3,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
//以上是 控件 和 布局
//下面是事件处理 程 序
//--------------- 数 字 键 ----------------
class JianTing implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1;//还原开关k5状态
}
str1=str1+ss;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//显示
}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //还原开关k5状态
}
str2=str2+ss;
//k2=2;
k4=k4+1;
///////////////测试////////////////
jg_TextField.setText(str2);
}
}
}
//--------符 号-----------
class JianTing_fh implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss2=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if(k2==1)
{
k1=2;//开关 k1 为1时,向数1写 为2时,向数2写
k5=1;
fh=ss2;
k2=k2+1;//按符号键的次数
}
else
{
int a=vt.size();
JButton c=(JButton)vt.get(a-2); if(!(c.getText().equals("+"))!(c.getText().equals("-"))!(c.getText().equals("*"))!(c.getText().equals("/")))
{
yuns();
str1=jg;
k1=2;//开关 k1 为1时,向数1写 为2时,向数2写
k5=1;
k4=1;
fh=ss2;
} k2=k2+1;
}
}
}
//--------清除-------
class JianTing_clear implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
k5=1;
k2=1;
k1=1;
k3=1;
k4=1;
str1="0";
str2="0";
fh="";
jg="";
jg_TextField.setText(jg);
vt.clear();
}
}
//----------------等 于 ---------------------
class JianTing_dy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
yuns();
k1=1; //还原开关k1状态
//str1=jg;
k2=1;
k3=1;//还原开关k3状态
k4=1; //还原开关k4状态
str1=jg; //为7+5=12 +5=17 这种计算做准备
}
}
//----------------小数点 ---------------------
class JianTing_xiaos implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if(k5==1)
{
String ss2=((JButton)e.getSource()).getText();
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1; //还原开关k5状态
}
str1=str1+ss2;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//显示
}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //还原开关k5状态
}
str2=str2+ss2;
//k2=2;
k4=k4+1;
///////////////测试////////////////
jg_TextField.setText(str2);
}
}
k5=k5+1;
}
}
//注册 监听器
JianTing_dy jt_dy=new JianTing_dy();
JianTing jt= new JianTing();//临听数字键
JianTing_fh jt_fh= new JianTing_fh();//临 听符 号键
JianTing_clear jt_c=new JianTing_clear(); //清除键
JianTing_xiaos jt_xs=new JianTing_xiaos();// 小数点 键
button7.addActionListener(jt);
button8.addActionListener(jt);
button9.addActionListener(jt);
button_chu.addActionListener(jt_fh);
button4.addActionListener(jt);
button5.addActionListener(jt);
button6.addActionListener(jt);
button_cheng.addActionListener(jt_fh);
button1.addActionListener(jt);
button2.addActionListener(jt);
button3.addActionListener(jt);
button_jian.addActionListener(jt_fh);
button0.addActionListener(jt);
button_Dian.addActionListener(jt_xs);
button_dy.addActionListener(jt_dy);
button_jia.addActionListener(jt_fh);
clear_Button.addActionListener(jt_c);
//关闭事件处理程序
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
//---------------计 算------------------
public void yuns()
{
double a2,b2;//运算数1,2
String c=fh;// 运算符
double jg2=0 ;//结果
if (c.equals(""))
{
//System.out.println("请输入运算符");
jg_TextField.setText("请输入运算符");
}
else
{
System.out.println("str1:"+str1);//调试时 使 用
System.out.println("str2:"+str2);//调试时 使 用
System.out.println("运算符:"+fh);//调试时 使 用
if (str1.equals(".")) //字符串 "." 转换成double型数据时 会出错 所以手工转
str1="0.0";
if (str2.equals("."))
str2="0.0";
a2=Double.valueOf(str1).doubleValue();
b2=Double.valueOf(str2).doubleValue();
System.out.println("double型的a2:"+a2); //调试时 使 用
System.out.println("double型的b2:"+b2); //调试时 使 用
if (c.equals("+"))
{
jg2=a2+b2;
}
if (c.equals("-"))
{
jg2=a2-b2;
}
if (c.equals("*"))
{
jg2=a2*b2;
}
if (c.equals("/"))
{
if(b2==0)
{
jg2=0;//0000000000000 by 0 cu!
}
else
{
jg2=a2/b2;
}
}
System.out.println("double型a2"+fh+"b2结果:"+jg2);
System.out.println();
jg=((new Double(jg2)).toString());
jg_TextField.setText(jg);
}
}
}
import java.text.DecimalFormat;
import java.util.Scanner;
public class Zhidao {
public static void main(String[] args) {
String condition = "";
Zhidao zhidao = new Zhidao();
do{
Scanner scanner = new Scanner(System.in);
try{
System.out.print("请输入第一个数:");
double x = scanner.nextDouble();
System.out.print("请输入第二个数:");
double y = scanner.nextDouble();
System.out.print("请输入运算符:");
String s = scanner.next();
char z = s.charAt(0);
zhidao.yunsuan(x, y, z);
}catch(Exception e){
System.out.println("请输入正确的数据!");
}
System.out.print("是否继续?continue:继续,任意字符:结束");
condition = scanner.next();
}while("continue".equals(condition));
}
public static void yunsuan(double x,double y,Character z){
DecimalFormat r=new DecimalFormat();
r.applyPattern("#0.00");
if(z.equals('+')){
System.out.println(x+"+"+y+"=" + r.format((x+y)));
} else if(z.equals('-')){
System.out.println(x+"-"+y+"=" + r.format((x-y)));
} else if(z.equals('*')){
System.out.println(x+"*"+y+"=" + r.format((x*y)));
} else if(z.equals('/')){
if(y==0){
System.out.println("被除数不能为0");
} else{
System.out.println(x+"/"+y+"=" + r.format((x/y)));
}
}else{
System.out.println("无法识别改运算符");
}
}
}
刚好用设计模式(简单工厂模式)编写了一个计算器。
package com.medavis.simplefactory.ui;
import java.util.Scanner;
import com.medavis.simplefactory.operate.OperatorFactory;
public class CaculatorUI {
private static Scanner sc;
public static void main(String[] args) {
try {
System.out.println("Please input a number:");
sc = new Scanner(System.in);
long first = sc.nextLong();
System.out.println("Please input a operational character (+-*/):");
sc = new Scanner(System.in);
String operator = sc.nextLine();
System.out.println("Please input a number:");
sc = new Scanner(System.in);
long second = sc.nextLong();
OperatorFactory opf=new OperatorFactory();
System.out.println(first + operator + second + "=" + opf.getOperator(operator).getResult(first, second));
} catch(Exception e) {
System.out.println("Your input is wrong!");
}
}
}
package com.medavis.simplefactory.operate;
public class OperatorAdd extends Operator{
@Override
public long getResult(long first, long second) {
return first+second;
}
}
package com.medavis.simplefactory.operate;
public class OperatorDiv extends Operator{
@Override
public long getResult(long first, long second) {
return first/second;
}
}
package com.medavis.simplefactory.operate;
public class OperatorFactory {
public Operator getOperator(String operator) throws Exception{
switch(operator){
case "+":
return new OperatorAdd();
case "-":
return new OperatorSub();
case "*":
return new OperatorMul();
case "/":
return new OperatorDiv();
default:
throw new Exception();
}
}
}
package com.medavis.simplefactory.operate;
public class OperatorMul extends Operator {
@Override
public long getResult(long first, long second) {
return first*second;
}
}
package com.medavis.simplefactory.operate;
public class OperatorSub extends Operator{
@Override
public long getResult(long first, long second) {
return first-second;
}
}
(首先建个类,把这些复制粘贴进去)
import java.awt.*;
import javax.swing.*;
public class F {
JFrame frame = new JFrame("计算机");
JPanel pl = new JPanel();
JPanel p2 = new JPanel();
static JTextField show = new JTextField();
static JButton b0 = new JButton("0");
static JButton b1 = new JButton("1");
static JButton b2 = new JButton("2");
static JButton b3 = new JButton("3");
static JButton b4 = new JButton("4");
static JButton b5 = new JButton("5");
static JButton b6 = new JButton("6");
static JButton b7 = new JButton("7");
static JButton b8 = new JButton("8");
static JButton b9 = new JButton("9");
JButton bjia = new JButton("+");
JButton bjian = new JButton("-");
JButton bcheng = new JButton("*");
JButton bchu = new JButton("/");
JButton bdian = new JButton(".");
JButton bdeng = new JButton("=");
JButton bqingchu = new JButton("清除");
public void y() {
pl.setLayout(new GridLayout(1, 1));
pl.add(show);
}
public void p() {
b1.addActionListener(new U());
b2.addActionListener(new U());
b3.addActionListener(new U());
b4.addActionListener(new U());
b5.addActionListener(new U());
b6.addActionListener(new U());
b7.addActionListener(new U());
b8.addActionListener(new U());
b9.addActionListener(new U());
b0.addActionListener(new U());
bjia.addActionListener(new Fu());
bjian.addActionListener(new Fu());
bcheng.addActionListener(new Fu());
bchu.addActionListener(new Fu());
bdeng.addActionListener(new Deng());
bqingchu.addActionListener(new Qing());
p2.setLayout(new GridLayout(6, 3));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b0);
p2.add(bjia);
p2.add(bjian);
p2.add(bcheng);
p2.add(bchu);
p2.add(bdian);
p2.add(bqingchu);
p2.add(bdeng);
}
public void o() {
frame.setLayout(new BorderLayout());
frame.add(pl, BorderLayout.NORTH);
frame.add(p2, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
F f = new F();
f.y();
f.p();
f.o();
}
}
(再新建个类 把这些也复制粘贴进去)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class U implements ActionListener {
public static String str = "";
public static String a = "";
public static String b = "";
public static String k = "";
public void actionPerformed(ActionEvent e) {
String w = e.getActionCommand();//字
if (k.equals("")) {
a += w;
F.show.setText(a);
} else {
b += w;
F.show.setText(b);
}
}
}
(再新建一个,把下面的复制粘贴)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Deng implements ActionListener {
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(U.a);
int b = Integer.parseInt(U.b);
int c = 0;
if (U.k.equals("+")) {
c = a + b;
} else
if (U.k.equals("-")) {
c = a - b;
} else
if (U.k.equals("*")) {
c = a * b;
} else
if (U.k.equals("/")) {
c = a / b;
} else {
}
String d = String.valueOf(c);
F.show.setText(d);
U.a = d;
U.b = "";
U.k = "";
}
}
(在建一个 复制粘贴)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Fu implements ActionListener {
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();//字
U.k = a;
}
}
(在建一个)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Qing implements ActionListener {
public void actionPerformed(ActionEvent e) {
U.a = "";
U.b = "";
U.k = "";
F.show.setText("");
}
}