📄 calculate.java
字号:
import java.awt.*;
import java.awt.event.*;
public class Calculate extends Frame implements ActionListener
{
Frame f;
TextField t1;
TextField t2;
TextField t3;
TextField t4;
Label L1;
Button btn;
public Calculate()
{
Frame f=new Frame("计算器");
f.setSize(300,200);
f.setLayout(new FlowLayout());
t1=new TextField(5);
t2=new TextField(5);
t3=new TextField(5);
t4=new TextField(5);
L1=new Label("=");
btn=new Button("计算");
f.add(t1);f.add(t2);f.add(t3);f.add(t4);
f.add(L1);f.add(btn);
f.setVisible(true);
btn.addActionListener(this);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
float x,y;
double result=0;
String op;
try{
x=Float.parseFloat(t1.getText());
y=Float.parseFloat(t3.getText());
op=t2.getText();
if(op.equals("+")) result=x+y;
else if(op.equals("-")) result=x-y;
else if(op.equals("*")) result=x*y;
else if(op.equals("/")) result=x/y;
t4.setText(Double.toString(result));
} catch(Exception ee) {t4.setText("数据错误");}
}
public static void main(String args[])
{
new Calculate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -