📄 cac.java
字号:
/**
* Calculator.java
*
* 创建日期:2007-8-29
*
* @author Tofer
*/
package Calculator;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener {
private Frame f;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,badd,bjian,bchen,bchu,bdian,bdeng,bcls;
public TextField tf1;
private Panel p1,p2;
public Calculator(){
f=new Frame("Calculator");
p1=new Panel();
p2=new Panel();
}
public void lauchCal(){
f.setSize(400,300);
p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(4,4,5,5));
f.setLayout(new BorderLayout());
tf1=new TextField(30);
tf1.setText("");
b0=new Button("0");
b0.addActionListener(this);
b1=new Button("1");
b1.addActionListener(this);
b2=new Button("2");
b2.addActionListener(this);
b3=new Button("3");
b3.addActionListener(this);
b4=new Button("4");
b4.addActionListener(this);
b5=new Button("5");
b5.addActionListener(this);
b6=new Button("6");
b6.addActionListener(this);
b7=new Button("7");
b7.addActionListener(this);
b8=new Button("8");
b8.addActionListener(this);
b9=new Button("9");
b9.addActionListener(this);
badd=new Button("+");
badd.addActionListener(this);
bjian=new Button("-");
bjian.addActionListener(this);
bchen=new Button("*");
bchen.addActionListener(this);
bchu=new Button("/");
bchu.addActionListener(this);
bdian=new Button(".");
bdian.addActionListener(this);
bdeng=new Button("=");
bdeng.addActionListener(this);
bcls=new Button("cls");
bcls.addActionListener(this);
p1.add(tf1);
p2.add(b0);
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(badd);
p2.add(bjian);
p2.add(bchen);
p2.add(bchu);
p2.add(bdian);
p2.add(bdeng);
p1.add(bcls);
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
String temp,temp1,temp2,temp3;
Double shu1,shu2=0.0,shu3=0.0;
int comm;
System.out.println(e.getActionCommand());
temp=e.getActionCommand();
if("cls".equals(temp)){tf1.setText("");}else{
if("=".equals(temp)){
temp1=tf1.getText();
if(temp1.contains("+")){
comm=temp1.indexOf("+");
shu1=Double.parseDouble(temp1.substring(0,comm));
shu2=Double.parseDouble(temp1.substring(comm+1));
shu3=shu1+shu2;
temp3=Double.toString(shu3);
tf1.setText(temp3);
}else if(temp1.contains("*")){
comm=temp1.indexOf("*");
shu1=Double.parseDouble(temp1.substring(0,comm));
shu2=Double.parseDouble(temp1.substring(comm+1));
shu3=shu1*shu2;
temp3=Double.toString(shu3);
tf1.setText(temp3);
}else if(temp1.contains("/")){
comm=temp1.indexOf("/");
shu1=Double.parseDouble(temp1.substring(0,comm));
shu2=Double.parseDouble(temp1.substring(comm+1));
shu3=shu1/shu2;
temp3=Double.toString(shu3);
tf1.setText(temp3);
}else if(temp1.contains("-")){
comm=temp1.indexOf("-");
shu1=Double.parseDouble(temp1.substring(0,comm));
shu2=Double.parseDouble(temp1.substring(comm+1));
shu3=shu1-shu2;
temp3=Double.toString(shu3);
tf1.setText(temp3);
}else{
tf1.setText("不能计算,缺少另一个运算数");
}
}else{
temp2=tf1.getText();
temp2=temp2+temp;
tf1.setText(temp2);
}
}
}
public static void main(String[] args){
Calculator guiwindow=new Calculator();
guiwindow.lauchCal();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -