📄 mycomput1.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class Mycomput1 extends Frame implements ActionListener,ItemListener
{
TextField text1,text2,text3;
Label lab1,lab2,lab3;
Button ad,sub,milt,div,but;
public Mycomput1()
{
super("简单计算器");
setSize(300,200);
}
public void init()
{
GridBagLayout gbl=new GridBagLayout();
setLayout(gbl);
lab1=new Label("操作数:");
add(lab1);
text1=new TextField(30);
text1.addActionListener(this);
add(text1);
lab2=new Label("操作数:");
add(lab2);
text2=new TextField(30);
text2.addActionListener(this);
add(text2);
lab3=new Label("结果:");
add(lab3);
text3=new TextField(30);
text3.addActionListener(this);
add(text3);
ad=new Button("加");
sub=new Button("减");
milt=new Button("乘");
div=new Button("除");
but=new Button("退出");
add(ad);
ad.addActionListener(this);
add(sub);
sub.addActionListener(this);
add(milt);
milt.addActionListener(this);
add(div);
div.addActionListener(this);
add(but);
but.addActionListener(this);
addWindowListener(new koWindowListener());
}
public void itemStateChanged(ItemEvent e)
{
}
public void actionPerformed(ActionEvent e)
{ double d;
int align=FlowLayout.LEFT;
if(e.getSource()==ad)
{ 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));
}
if(e.getSource()==sub)
{ 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));
}
if(e.getSource()==milt)
{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));
}
if(e.getSource()==div)
{
double i,j;
i=Double.valueOf(text1.getText()).doubleValue();
j=Double.valueOf(text2.getText()).doubleValue();
d=i/j;
text3.setText(String.valueOf(d));
}
if(e.getSource()==but)
{
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 Mycomput1();}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -