📄 computer.java
字号:
import java.awt.*;
import java.awt.event.*;
public class Computer implements WindowListener,ActionListener{
private Frame f;
double d0,d1,d2,result;
boolean flag=true;
String s,oper;
TextField tf1;
Panel p=new Panel();
public static void main(String args[])
{ Computer e=new Computer();
e.go();
}
public void go(){
int i;
result=0;
s=new String();
oper=new String("+");
f=new Frame("计算器");
MenuBar mb=new MenuBar();
f.setMenuBar(mb);
Menu m1=new Menu("编辑");
Menu m2=new Menu("帮助");
Menu m3=new Menu("关于");
mb.add(m1);
mb.add(m3);
mb.setHelpMenu(m2);
tf1=new TextField("",15);
Button[] b=new Button[21];
for(i=1;i<21;i++)
{
b[i]=new Button();
b[i].setFont(new Font("仿宋",0,16));
}
b[1].setLabel("退格");
b[2].setLabel("CE");
b[3].setLabel("C");
b[4].setLabel("/");
b[5].setLabel("7");
b[6].setLabel("8");
b[7].setLabel("9");
b[8].setLabel("*");
b[9].setLabel("4");
b[10].setLabel("5");
b[11].setLabel("6");
b[12].setLabel("-");
b[13].setLabel("1");
b[14].setLabel("2");
b[15].setLabel("3");
b[16].setLabel("+");
b[17].setLabel("0");
b[18].setLabel("+/-");
b[19].setLabel(".");
b[20].setLabel("=");
p.setLayout(new GridLayout(5,4));
p.setBackground(new Color(80,30,100));
for(i=1;i<21;i++)
{ p.add(b[i]);
b[i].addActionListener(this);
b[i].setBackground(new Color(20,130,180));
b[i].setForeground(Color.yellow);
}
for(i=1;i<4;i++)
{
b[i].setBackground(new Color(120,180,170));
b[i].setForeground(Color.blue);
}
for(i=1;i<=4;i++)
{ b[i*4].setBackground(new Color(120,180,170));
b[i*4].setForeground(Color.red);
}
b[20].setBackground(Color.red);
f.addWindowListener(this);
f.setLayout(new BorderLayout());
f.add("North",tf1);
f.add("Center",p);
f.setSize(200,200);
f.setVisible(true);
}
public void windowClosing(WindowEvent e){System.exit(1);}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void actionPerformed(ActionEvent e){
String i1=tf1.getText();
s=e.getActionCommand();
if(s=="0"| s=="1"|s=="2"|s=="3"|s=="4"|s=="5"|s=="6"|s=="7"|s=="8"|s=="9"|s==".")
{ if(flag) tf1.setText(i1+s);
else
{ tf1.setText(s);
flag=true;
}
}
else if(s=="+"|s=="-"|s=="*"|s=="/")
{ result=Double.parseDouble(i1);
flag=false;
oper=s;
}
else if(s=="=")
{ d0=Double.parseDouble(i1);
if(oper=="+") result+=d0;
if(oper=="-") result-=d0;
if(oper=="*") result*=d0;
if(oper=="/") result/=d0;
tf1.setText(Double.toString(result));
flag=false;
}
else if(s=="CE")
{ tf1.setText("");
flag=false;
}
else if(s=="C")
{ result=0;
tf1.setText("");
flag=false;
}
else if(s=="退格")
{ String ss=tf1.getText();
int i=ss.length();
ss=ss.substring(0,i-1);
tf1.setText(ss);
}
else if(s=="+/-")
{ d2=-1*Double.parseDouble(tf1.getText());
tf1.setText(Double.toString(d2));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -