📄 jisuan.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class jisuan extends Frame implements ActionListener
{
Label t;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
Panel p;//定义一个用于显示的标签和16个按钮,一个用于对按钮布局的面板
String str; //用来记录要显示在标签里的信息
char fuhao;//用来记录用户点击的运算符
boolean panduan,dian;//判断用户是否点过运算符或小数点按钮
double d1,d2;//用来记录用户要进行运算的2个数
jisuan()
{
str="";
fuhao='0';
panduan=dian=false;
d1=d2=0.0;
//初始化都设置为最低值
t=new Label("0.");
t.setAlignment(2);//设置文本为右对齐
t.setForeground(Color.red);//设置文本颜色为红色
t.setBackground(Color.white);//设置标签的背景色为白色
//下面是初始化按钮的文本和文本颜色
b0=new Button("0");
b0.setForeground(Color.BLUE);
b1=new Button("1");
b1.setForeground(Color.BLUE);
b2=new Button("2");
b2.setForeground(Color.BLUE);
b3=new Button("3");
b3.setForeground(Color.BLUE);
b4=new Button("4");
b4.setForeground(Color.BLUE);
b5=new Button("5");
b5.setForeground(Color.BLUE);
b6=new Button("6");
b6.setForeground(Color.BLUE);
b7=new Button("7");
b7.setForeground(Color.BLUE);
b8=new Button("8");
b8.setForeground(Color.BLUE);
b9=new Button("9");
b9.setForeground(Color.BLUE);
b10=new Button(".");
b10.setForeground(Color.BLUE);
b11=new Button("=");
b11.setForeground(Color.red);
b12=new Button("+");
b12.setForeground(Color.red);
b13=new Button("-");
b13.setForeground(Color.red);
b14=new Button("*");
b14.setForeground(Color.red);
b15=new Button("/");
b15.setForeground(Color.red);
//下面是为所有按钮都添加监听
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
//将按钮加入到面板中
p=new Panel(new GridLayout(4,4,8,8));
p.setBackground(new Color(212,208,200));
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b15);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b14);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b13);
p.add(b0);
p.add(b10);
p.add(b11);
p.add(b12);
//加入到窗体中
this.setTitle("一个简单的计算器");
this.setResizable(false);//设置本窗口不能改变大小
this.setSize(200,200);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();//获得屏幕大小
this.setLocation((d.width-200)/2,(d.height-200)/2);//设置本窗口居中
this.setVisible(true);
this.addWindowListener(new WindowAdapter()//窗口可以关闭
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public static void main(String args[])
{
jisuan my=new jisuan();
}
public void actionPerformed(ActionEvent e)
{
char c=e.getActionCommand().charAt(0);//获得被点击的按钮的文本
switch(c)//通过按钮的文本开始判断
{
case '+'://是点击的符号的话
panduan=true;//告诉程序用户以后输入的数字都要保存到d2中了
fuhao='+';//保存用户点击的运算符
str="";//将用于显示的字符串清空,因为第一个数字已经输入完毕
dian=false;//第二个数字又可以输入小数点了,所以要设置为假
break;//以下运算符同理
case '-':
panduan=true;
fuhao='-';
str="";
dian=false;
break;
case '*':
panduan=true;
fuhao='*';
str="";
dian=false;
break;
case '/':
panduan=true;
fuhao='/';
str="";
dian=false;
break;
case '=':
switch(fuhao)//通过运算符判断用户要进行的是什么操作
{
case '+':
d1=d1+d2;//将运算结果保存到第一个数中,以便重复运算
str="";//将用于显示的字符串清空
t.setText(String.valueOf(d1));//显示结果
dian=panduan=false;//又可以重新接受用户输入了
break;//以下运算符同理
case '-':
d1=d1-d2;
str="";
t.setText(String.valueOf(d1));
dian=panduan=false;
break;
case '*':
d1=d1*d2;
str="";
t.setText(String.valueOf(d1));
dian=panduan=false;
break;
case '/':
d1=d1/d2;
str="";
t.setText(String.valueOf(d1));
dian=panduan=false;
break;
}
break;
case '.':
if(dian==true)//如果用户在输入时已经输入过小数点,直接跳过,不做任何操作
break;
else//否则
{
str=str+String.valueOf('.');
t.setText(str);//显示小数点
dian=true; //以后再不能输入小数点了
}
break;
default://缺省的话,说明用户点击的是数字按钮
str=str+String.valueOf(c);//将用户点的数和以前点数的都连接起来
t.setText(str);//显示在标签中
if(panduan==false)//如果还没有点运算符的话,说明用户还在输入第一个操作数
d1=Double.parseDouble(str);//那么就将这个数保存在d1中
else//否则就是输入的第二的操作数
d2=Double.parseDouble(str);//保存在d2中
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -