加法器

编写程序:用BoxLayout的布局方式设计一个界面,实现一个加法器的功能。被加数和加数用文本框输入,点击按钮则产生结果。

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 import javax.swing.*;
 4 
 5 class MyWin extends JFrame implements ActionListener
 6 { 
 7     TextField text1,text2,text3; 
 8     MyWin()
 9     { 
10         setLayout(new FlowLayout());
11         text1=new TextField(8);
12         text2=new TextField(8);
13         text3=new TextField(15);
14         JLabel lb1 = new JLabel(" + ");
15         JLabel lb2 = new JLabel(" = ");
16         JButton btn = new JButton("计算");
17         btn.addActionListener(this);
18         add(text1);
19         add(lb1);
20         add(text2);
21         add(lb2);
22         add(text3);
23         add(btn);
24         text1.addActionListener(this); 
25         text2.addActionListener(this);
26         setBounds(100,100,650,150);
27         setVisible(true);
28         validate();
29   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
30     }  
31     public void actionPerformed(ActionEvent e) 
32     {
33         int a = Integer.parseInt(text1.getText());
34         int b = Integer.parseInt(text2.getText());
35         int c = a+b;
36         String d=String.valueOf(c);
37         text3.setText(d);
38     }
39 }
40 public class LX9_17
41 {  
42     public static void main(String args[])
43     { 
44         new MyWin();
45     }
46 }

原文地址:https://www.cnblogs.com/acm-bingzi/p/3499263.html