📄 jisuanqi.java
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.*;
class MyWing extends Frame implements ActionListener
{
Button a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
Box box1,box2,box3,box4,box0;
TextField text;
String str;
int num;
double num1,num2,num3;
MyWing (String s)
{
super(s);
setLayout(new BorderLayout());
setSize(250,250);
setLocation(380,220);
setBackground(Color.green);
Panel p=new Panel();
text=new TextField("0",26);
text.setBackground(Color.white);
text.setEditable(false);
text.setFocusable(false);
p.add(text);
add(p,BorderLayout.NORTH);
box0=Box.createHorizontalBox();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
box3=Box.createVerticalBox();
box4=Box.createVerticalBox();
a0=new Button("7");
a0.addActionListener(this);
a1=new Button("4");
a1.addActionListener(this);
a2=new Button("1");
a2.addActionListener(this);
a3=new Button("0");
a3.addActionListener(this);
a4=new Button("←");
a4.addActionListener(this);
a5=new Button("8");
a5.addActionListener(this);
a6=new Button("5");
a6.addActionListener(this);
a7=new Button("2");
a7.addActionListener(this);
a8=new Button(".");
a8.addActionListener(this);
a9=new Button("T");
a9.addActionListener(this);
b0=new Button("9");
b0.addActionListener(this);
b1=new Button("6");
b1.addActionListener(this);
b2=new Button("3");
b2.addActionListener(this);
b3=new Button("=");
b3.addActionListener(this);
b4=new Button("C");
b4.addActionListener(this);
b5=new Button("+");
b5.addActionListener(this);
b6=new Button("-");
b6.addActionListener(this);
b7=new Button("*");
b7.addActionListener(this);
b8=new Button("/");
b8.addActionListener(this);
b9=new Button("SHUT");
b9.setBackground(Color.red);
b9.addActionListener(this);
box1.add(a0);
box1.add(Box.createVerticalStrut(12));
box1.add(a1);
box1.add(Box.createVerticalStrut(12));
box1.add(a2);
box1.add(Box.createVerticalStrut(12));
box1.add(a3);
box1.add(Box.createVerticalStrut(12));
box1.add(a4);
box1.add(Box.createVerticalStrut(12));
box2.add(a5);
box2.add(Box.createVerticalStrut(12));
box2.add(a6);
box2.add(Box.createVerticalStrut(12));
box2.add(a7);
box2.add(Box.createVerticalStrut(12));
box2.add(a8);
box2.add(Box.createVerticalStrut(12));
box2.add(a9);
box2.add(Box.createVerticalStrut(12));
box3.add(b0);
box3.add(Box.createVerticalStrut(12));
box3.add(b1);
box3.add(Box.createVerticalStrut(12));
box3.add(b2);
box3.add(Box.createVerticalStrut(12));
box3.add(b3);
box3.add(Box.createVerticalStrut(12));
box3.add(b4);
box3.add(Box.createVerticalStrut(12));
box4.add(b5);
box4.add(Box.createVerticalStrut(12));
box4.add(b6);
box4.add(Box.createVerticalStrut(12));
box4.add(b7);
box4.add(Box.createVerticalStrut(12));
box4.add(b8);
box4.add(Box.createVerticalStrut(12));
box4.add(b9);
box4.add(Box.createVerticalStrut(12));
box0.add(Box.createHorizontalStrut(12));
box0.add(box1);
box0.add(Box.createHorizontalStrut(12));
box0.add(box2);
box0.add(Box.createHorizontalStrut(12));
box0.add(box3);
box0.add(Box.createHorizontalStrut(12));
box0.add(box4);
box0.add(Box.createHorizontalStrut(12));
add(box0,BorderLayout.CENTER);
validate();
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==a0)
{
text.setText(text.getText()+7);
}
else if(e.getSource()==a1)
{
text.setText(text.getText()+4);
}
else if(e.getSource()==a2)
{
text.setText(text.getText()+1);
}
else if(e.getSource()==a3)
{
text.setText(text.getText()+0);
}
else if(e.getSource()==a4)
{
str=text.getText();
text.setText(str.substring(0,str.length()-1));
}
else if(e.getSource()==a5)
{
text.setText(text.getText()+8);
}
else if(e.getSource()==a6)
{
text.setText(text.getText()+5);
}
else if(e.getSource()==a7)
{
text.setText(text.getText()+2);
}
else if(e.getSource()==a8)
{
text.setText(text.getText()+".");
}
else if(e.getSource()==a9)
{
text.setText(""+new Date());
}
else if(e.getSource()==b0)
{
text.setText(text.getText()+9);
}
else if(e.getSource()==b1)
{
text.setText(text.getText()+6);
}
else if(e.getSource()==b2)
{
text.setText(text.getText()+3);
}
else if(e.getSource()==b3)
{
if(num==1)
{
num2=Double.parseDouble(text.getText());
num3=num1+num2;
}
else if(num==2)
{
num2=Double.parseDouble(text.getText());
num3=num1-num2;
}
else if(num==3)
{
num2=Double.parseDouble(text.getText());
num3=num1*num2;
}
else if(num==4)
{
num2=Double.parseDouble(text.getText());
num3=num1/num2;
}
text.setText(""+num3);
}
else if(e.getSource()==b4)
{
text.setText(null);
num=0;
num1=0.0;
num2=0.0;
}
else if(e.getSource()==b5)
{
num1=Double.parseDouble(text.getText());
text.setText("");
num=1;
}
else if(e.getSource()==b6)
{
num1=Double.parseDouble(text.getText());
text.setText("");
num=2;
}
else if(e.getSource()==b7)
{
num1=Double.parseDouble(text.getText());
text.setText("");
num=3;
}
else if(e.getSource()==b8)
{
num1=Double.parseDouble(text.getText());
text.setText("");
num=4;
}
else if(e.getSource()==b9)
{
System.exit(0);
}
}
}
class JiSuanQi
{
public static void main (String args[])
{
MyWing win=new MyWing("计算器");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -