import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class LX10 extends JFrame implements ActionListener {
p P;
p1 P1;
p2 P2;
LX10(){
P=new p();
P1=new p1();
P2=new p2();
setSize(300,120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
getContentPane().add(P,BorderLayout.NORTH);
getContentPane().add(P1,BorderLayout.EAST);
getContentPane().add(P2,BorderLayout.CENTER);
P1.B.addActionListener(this);
}
public static void main(String[] args)
{
LX10 a=new LX10();
a.setVisible(true);
}
class p extends JPanel
{ JTextField t1;
JTextField t2;
Label l1;
Label l2;
p()
{
t1=new JTextField(5);
t2=new JTextField(5);
l1=new Label("加數:");
l2=new Label("被加數:");
setLayout(new FlowLayout());
add(l1);
add(t1);
add(l2);
add(t2);
}
}
class p1 extends JPanel
{
JButton B=new JButton("運算");
p1()
{
setLayout(new FlowLayout());
add(B);
}
}
class p2 extends JPanel
{
JTextField r;
Label l3;
p2()
{
r=new JTextField(6);
l3=new Label("結果");
setLayout(new FlowLayout());
add(l3);
add(r);
}
}
public void actionPerformed(ActionEvent e) {
if(P.t1.getText()!=""||P.t2.getText()!="")
{
int d1=Integer.parseInt(P.t1.getText());
int d2=Integer.parseInt(P.t2.getText());
P2.r.setText(Integer.toString(d1+d2));
}
}
}