📄 mycomput.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class Mycomput extends Frame
{
TextField text1,text2,text3;
Label lab1,lab2,lab3;
Button ad,sub,milt,div,but;
public Mycomput()
{ super("简单计算器");
setLayout(new FlowLayout());
setSize(300,200);
setVisible(true);
GridBagLayout gbl=new GridBagLayout();
setLayout(gbl);
lab1=new Label("操作数:");
add(lab1);
text1=new TextField(10);
text1.addActionListener(new koActionListener());
add(text1);
lab2=new Label("操作数:");
add(lab2);
text2=new TextField(10);
text2.addActionListener(new koActionListener());
add(text2);
lab3=new Label("结果:");
add(lab3);
text3=new TextField(10);
text3.addActionListener(new koActionListener());
add(text3);
ad=new Button("加");
sub=new Button("减");
milt=new Button("乘");
div=new Button("除");
but=new Button("退出");
add(ad);
ad.addActionListener(new koActionListener());
add(sub);
sub.addActionListener(new koActionListener());
add(milt);
milt.addActionListener(new koActionListener());
add(div);
div.addActionListener(new koActionListener());
add(but);
but.addActionListener(new koActionListener());
addWindowListener(new koWindowListener());
}
class koActionListener implements ActionListener
{public void actionPerformed(ActionEvent e)
{double d;
int align=FlowLayout.LEFT;
String ko=e.getActionCommand();
if(ko.equals(ad.getLabel())){
align=FlowLayout.LEFT;
double i,j;
i=Double.valueOf(text1.getText()).doubleValue();
j=Double.valueOf(text2.getText()).doubleValue();
d=i+j;
text3.setText(String.valueOf(d));
}
else if(ko.equals(sub.getLabel())){
align=FlowLayout.CENTER;
double i,j;
i=Double.valueOf(text1.getText()).doubleValue();
j=Double.valueOf(text2.getText()).doubleValue();
d=i-j;
text3.setText(String.valueOf(d));}
else if(ko.equals(milt.getLabel())){
align=FlowLayout.RIGHT;
double i,j;
i=Double.valueOf(text1.getText()).doubleValue();
j=Double.valueOf(text2.getText()).doubleValue();
d=i*j;
text3.setText(String.valueOf(d));
}
else if(ko.equals(div.getLabel())){
align=FlowLayout.RIGHT;
double i,j;
i=Double.valueOf(text1.getText()).doubleValue();
j=Double.valueOf(text2.getText()).doubleValue();
d=i/j; text3.setText(String.valueOf(d));
}
else if(ko.equals(but.getLabel()))
{System.exit(0);}}}
class koWindowListener extends WindowAdapter
{public void windowClosing(WindowEvent e)
{e.getWindow().dispose();
System.exit(0);
}}
public static void main(String args[])
{new Mycomput();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -