我的程序如下importjavax.swing.*;importjava.awt.*;importjavax.swing.border.*;importjava.awt.event.*;publicclassExample7{publicstaticvoidmain(Stringargs[]){WindowBoxwin=new...
我的程序如下
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Example7 {
public static void main(String args[]){
WindowBox win=new WindowBox("简单计数器");
}
}
class WindowBox extends Frame implements ActionListener{
Box baseBox,boxV1,boxV2,boxV3;
Label labe1,labe2,labe3;
TextField text1,text2,text3;
Button butt1,butt2,butt3,butt4;
WindowBox(String s){
super(s);
boxV1=Box.createVerticalBox();
labe1=new Label("操作数");
boxV1.add(labe1);
boxV1.add(Box.createVerticalStrut(8));
labe2=new Label("操作数");
boxV1.add(labe2);
boxV1.add(Box.createVerticalStrut(8));
labe3=new Label("结果");
boxV1.add(labe3);
boxV2=Box.createVerticalBox();
text1=new TextField(20);
boxV2.add(text1);
boxV2.add(Box.createVerticalStrut(8));
text2=new TextField(20);
boxV2.add(text2);
boxV2.add(Box.createVerticalStrut(8));
text3=new TextField(20);
boxV2.add(text3);
boxV3=Box.createVerticalBox();
butt1=new Button("+");
boxV3.add(butt1);
butt1.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt2=new Button("-");
boxV3.add(butt2);
butt2.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt3=new Button("*");
boxV3.add(butt3);
butt3.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt4=new Button("/");
boxV3.add(butt4);
butt4.addActionListener(this);
/*butt1.addActionListener(this);
butt2.addActionListener(this);
butt3.addActionListener(this);
butt4.addActionListener(this);
*/
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(50));
baseBox.add(boxV3);
setLayout(new FlowLayout());
add(baseBox);
setBounds(120,125,350,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){
Integer m,n,p;
m=Integer.parseInt(text1.getText());
n=Integer.parseInt(text2.getText());
if(e.getSource()==butt1){
p=m+n;
text3.setText(m+"与"+n+"的和为:"+p);
}
if(e.getSource()==butt2){
p=m-n;
text3.setText(m+"与"+n+"的差为:"+p);
if(e.getSource()==butt3){
p=m*n;
text3.setText(m+"与"+n+"的积为:"+p);
if(e.getSource()==butt4){
p=m/n;
text3.setText(m+"与"+n+"的差为:"+p);
}
}
}
}
}
展开