/*忘得差不多了、熟悉一下Swing*/
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class ComputerAdd extends JFrame{
JPanel p;
JLabel l1,l2;
JTextField tf1, tf2, tf3;
JButton bt;
ComputerAdd(){
p = new JPanel();
l1 = new JLabel("please input two number:");
l2 = new JLabel(" + ");
tf1 = new JTextField(10); //如果不设置参数就会出现一条线的情况
tf2 = new JTextField(10);
tf3 = new JTextField(10);
bt = new JButton("=");
ButtonListener b =new ButtonListener(tf1, tf2, tf3);
bt.addActionListener(b);
p.setSize(200,100);
p.setLayout(new FlowLayout());
p.add(tf1);
p.add(l2);
p.add(tf2);
p.add(bt);
p.add(tf3);
setLayout(new BorderLayout());
add(l1, "North");
add(p, "Center");
setTitle("ComputerAdd");
setSize(500,150);
setVisible(true);
}
public static void main(String args[]){
ComputerAdd c = new ComputerAdd();
}
}
class ButtonListener implements ActionListener{
JTextField tf1, tf2, tf3;
public ButtonListener(JTextField tf1, JTextField tf2, JTextField tf3){
this.tf1 = tf1;
this.tf2= tf2;
this.tf3 = tf3;
}
public void actionPerformed(ActionEvent e){
int n1, n2, sum;
String s;
n1 = Integer.parseInt(tf1.getText());
n2 = Integer.parseInt(tf2.getText());
sum = n1 + n2;
s =String.valueOf(sum);
tf3.setText(s);
}
}
/*
结果:
*/