📄 sy7_6.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class Mycomput extends Frame implements ActionListener,ItemListener
{ TextField text1,text2,text3;
Label lab1,lab2,lab3;
Button ad,sub,milt,div,but;
public Mycomput()
{
super("简单计算器");
setSize(300,200);
}
public void init()
{
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
setLayout(gbl);
lab1=new Label("操作数:");
text1=new TextField(10);
text1.addActionListener(this);
System.out.println();
lab2=new Label("操作数:");
text2=new TextField(10);
text2.addActionListener(this);
lab3=new Label("结果:");
text3=new TextField(10);
text3.addActionListener(this);
ad=new Button("加");
sub=new Button("减");
milt=new Button("乘");
div=new Button("除");
but=new Button("退出");
ad.addActionListener(this);
sub.addActionListener(this);
milt.addActionListener(this);
div.addActionListener(this);
but.addActionListener(this);
gbc.gridx=0;gbc.gridy=2;
gbc.gridwidth =1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.SOUTHWEST;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(lab1,gbc);
add(lab1);
gbc.gridx=1;gbc.gridy=2;
gbc.gridwidth =1;
gbc.gridheight=1;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,1,5);
gbl.setConstraints(text1,gbc);
add(text1);
gbc.gridx=0;gbc.gridy=3;
gbc.gridwidth =1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.SOUTHWEST;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,2,5);
gbl.setConstraints(lab2,gbc);
add(lab2);
gbc.gridx=1;gbc.gridy=3;
gbc.gridwidth =1;
gbc.gridheight=1;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,2,5);
gbl.setConstraints(text2,gbc);
add(text2);
gbc.gridx=0;gbc.gridy=4;
gbc.gridwidth =1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.SOUTHWEST;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,3,5);
gbl.setConstraints(lab3,gbc);
add(lab3);
gbc.gridx=1;gbc.gridy=4;
gbc.gridwidth =1;
gbc.gridheight=1;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,3,5);
gbl.setConstraints(text3,gbc);
add(text3);
gbc.gridx=3;gbc.gridy=2;
gbc.gridwidth =2;
gbc.gridheight=1;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,2,5);
gbl.setConstraints(ad,gbc);
add(ad);
gbc.gridx=3;gbc.gridy=3;
gbc.gridwidth =2;
gbc.gridheight=1;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,3,5);
gbl.setConstraints(sub,gbc);
add(sub);
gbc.gridx=3;gbc.gridy=4;
gbc.gridwidth =2;
gbc.gridheight=1;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,4,5);
gbl.setConstraints(milt,gbc);
add(milt);
gbc.gridx=3;gbc.gridy=5;
gbc.gridwidth =2;
gbc.gridheight=1;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,5,5);
gbl.setConstraints(div,gbc);
add(div);
gbc.gridx=1;gbc.gridy=6;
gbc.gridwidth =2;
gbc.gridheight=1;
gbc.weightx=1;gbc.weighty=0;
gbc.insets=new Insets(2,5,6,5);
gbl.setConstraints(but,gbc);
add(but);
}
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 class Sy7_6
{
public static void main(String[] args)
{
Mycomput comput=new Mycomput();
comput.init();
comput.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -