📄 calculator.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class calculator extends Frame{
public static void main(String[] args){
new CFrame("郭敏的计算器");
}
}
class CFrame extends Frame{
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
JTextField tf;
//String text = "";
StringBuffer str;
double x,y;
int z;
public CFrame (String s){
super(s);
setLayout(new BorderLayout());
setLocation(350,250);
setSize(200,250);
tf= new JTextField("0",300);
b0=new Button("C");
b0.setForeground(Color.red);
b0.addActionListener(new behavior());
b1=new Button("1");
b1.setForeground(Color.blue);
b1.addActionListener(new behavior());
b2=new Button("2");
b2.setForeground(Color.blue);
b2.addActionListener(new behavior());
b3=new Button("3");
b3.setForeground(Color.blue);
b3.addActionListener(new behavior());
b4=new Button("+");
b4.setForeground(Color.red);
b4.addActionListener(new behavior());
b5=new Button("4");
b5.setForeground(Color.blue);
b5.addActionListener(new behavior());
b6=new Button("5");
b6.setForeground(Color.blue);
b6.addActionListener(new behavior());
b7=new Button("6");
b7.setForeground(Color.blue);
b7.addActionListener(new behavior());
b8=new Button("-");
b8.setForeground(Color.red);
b8.addActionListener(new behavior());
b9=new Button("7");
b9.setForeground(Color.blue);
b9.addActionListener(new behavior());
b10=new Button("8");
b10.setForeground(Color.blue);
b10.addActionListener(new behavior());
b11=new Button("9");
b11.setForeground(Color.blue);
b11.addActionListener(new behavior());
b12=new Button("*");
b12.setForeground(Color.red);
b12.addActionListener(new behavior());
b13=new Button("0");
b13.setForeground(Color.blue);
b13.addActionListener(new behavior());
b14=new Button(".");
b14.setForeground(Color.blue);
b14.addActionListener(new behavior());
b15=new Button("=");
b15.setForeground(Color.red);
b15.addActionListener(new behavior());
b16=new Button("/");
b16.setForeground(Color.red);
b16.addActionListener(new behavior());//实例化所有的按钮,设置颜色,注册动作监听器
Panel p1 = new Panel(new BorderLayout());
Panel p2=new Panel(new GridLayout(4,4));
Panel p3=new Panel(new BorderLayout());
str=new StringBuffer();
p1.add(tf);
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(b10);p2.add(b11);p2.add(b12);
p2.add(b13); p2.add(b14);p2.add(b15);p2.add(b16);
p3.add(b0);
add(p1,"North");add(p2,"Center");add(p3,"South");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1){
System.exit(0);
}
});
setVisible(true);
}
class behavior implements ActionListener{
public void actionPerformed(ActionEvent e2){
try{
if(e2.getSource()==b0){
tf.setText("0");
str.setLength(0);
}//选择清零
else if(e2.getSource()==b4){
x=Double.parseDouble(tf.getText().trim());
str.setLength(0);
y=0d;
z=0;
}//加功能
else if(e2.getSource()==b8){
x=Double.parseDouble(tf.getText().trim());
str.setLength(0);
y=0d;
z=1;
}//减功能
else if(e2.getSource()==b12){
x=Double.parseDouble(tf.getText().trim());
str.setLength(0);
y=0d;
z=2;
}//乘功能
else if(e2.getSource()==b16){
x=Double.parseDouble(tf.getText().trim());
str.setLength(0);
y=0d;
z=3;
}//除功能
else if(e2.getSource()==b15){
str.setLength(0);
switch(z){
case 0:tf.setText(""+(x+y));break;
case 1:tf.setText(""+(x-y));break;
case 2:tf.setText(""+(x*y));break;
case 3:tf.setText(""+(x/y));break;
}
}//实现等号运算
else if(e2.getSource()==b14)//单击"."按钮输入小数
{
if(tf.getText().trim().indexOf(".")!=-1)//判断字符串中是否已经包含了小数点
{
}
else//如果没数点有小
{
if(tf.getText().trim().equals("0"))//如果初时显示为0
{
str.setLength(0);
tf.setText((str.append("0"+e2.getActionCommand())).toString());
}
else if(tf.getText().trim().equals(""))//如果初时显示为空则不做任何操作
{
}
else
{
tf.setText(str.append(e2.getActionCommand()).toString());
}
}
y=0d;
}
else//其他的数字键
{
tf.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf.getText().trim());
}
}
catch(NumberFormatException e){
tf.setText("数字格式异常");
}
catch(StringIndexOutOfBoundsException e){
tf.setText("字符串索引越界");
}//捕获异常
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -