课后作业-----窗口的切换

1.获取两个文本域的输入并求和,然后显示在第三个文本域中。
程序如下:

1 package WindowBuilder;
2 import java.awt.;
3 import java.awt.event.
;
4 import javax.swing.*;
5 import javax.swing.border.EmptyBorder;
6
7 public class MyJFrame extends JFrame
8 {
9 private JPanel contentPane;
10 private final JTextPane textPane_1 = new JTextPane();
11 public static void main(String[] args)
12 {
13 try
14 {
15 MyJFrame frame = new MyJFrame();
16 frame.setVisible(true);
17 }
18 catch (Exception e)
19 {
20 e.printStackTrace();
21 }
22
23 }
24 public MyJFrame() {
25 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26 setBounds(100, 100, 450, 300);
27 contentPane = new JPanel();
28 contentPane.setBackground(Color.LIGHT_GRAY);
29 contentPane.setBorder(BorderFactory.createEmptyBorder());//窗口设置为无边框
30 setContentPane(contentPane); //把contentPane对象设置成为MyFrame的内容面板
31 contentPane.setLayout(null);
32
33 JButton btnNewButton = new JButton("\u786E\u5B9A");
34 btnNewButton.setFont(new Font("楷体", Font.PLAIN, 12));
35 btnNewButton.setBackground(Color.RED);
36 btnNewButton.addActionListener(new ActionListener()
37 {
38 public void actionPerformed(ActionEvent e)
39 {
40 System.exit(EXIT_ON_CLOSE);
41 }
42 });
43 btnNewButton.setBounds(343, 234, 89, 24);
44 contentPane.add(btnNewButton);
45 btnNewButton.setVerticalAlignment(SwingConstants.BOTTOM);//垂直窗口底端
46
47 JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
48 btnNewButton_1.setFont(new Font("楷体", Font.PLAIN, 12));
49 btnNewButton_1.setBackground(Color.CYAN);
50 btnNewButton_1.setBounds(8, 233, 89, 24);
51 contentPane.add(btnNewButton_1);
52
53 JLabel label = new JLabel("\u6587\u672C\u57DF1\uFF1A");
54 label.setFont(new Font("楷体", Font.PLAIN, 12));
55 label.setBounds(14, 51, 58, 15);
56 contentPane.add(label);
57
58 JTextArea textArea = new JTextArea();
59 textArea.setLineWrap(true);
60 textArea.setWrapStyleWord(true);
61 textArea.setBounds(72, 47, 110, 80);
62 contentPane.add(textArea);
63
64 JLabel label_1 = new JLabel("\u6587\u672C\u57DF2\uFF1A");
65 label_1.setFont(new Font("楷体", Font.PLAIN, 12));
66 label_1.setBounds(207, 51, 58, 15);
67 contentPane.add(label_1);
68
69 JLabel label_2 = new JLabel("\u6587\u672C\u57DF3\uFF1A");
70 label_2.setFont(new Font("楷体", Font.PLAIN, 12));
71 label_2.setBounds(109, 170, 58, 15);
72 contentPane.add(label_2);
73
74 JTextArea textArea_2 = new JTextArea();
75 textArea_2.setLineWrap(true);//设置自动换行
76 textArea_2.setBounds(265, 49, 110, 80);
77 contentPane.add(textArea_2);
78
79 JPanel panel = new JPanel();
80 panel.setBounds(162, 156, 158, 80);
81 contentPane.add(panel);
82 panel.setLayout(null);
83
84 JScrollPane scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);//滑动条设置为需要时出现
85 scrollPane.setBounds(0, 0, 158, 80);
86 panel.add(scrollPane);
87
88 JTextArea textArea_1 = new JTextArea(0,0);
89 textArea_1.setEditable(false);
90 textArea_1.setWrapStyleWord(true);
91 scrollPane.setViewportView(textArea_1); //scrollPane显示可滚动的子元素
92
93 JButton btnSum = new JButton("Sum");
94 btnSum.addActionListener(new ActionListener() {
95 public void actionPerformed(ActionEvent e)
96 {
97 textArea_1.setEditable(true);
98 String s=textArea.getText()+textArea_2.getText();
99 textArea_1.setText(s);
100 textArea.setText("");
101 textArea_2.setText("");
102 textArea_1.setEditable(false);
103 btnSum.setEnabled(false);
104 }
105 });
106 btnSum.setBounds(335, 156, 97, 23);
107 contentPane.add(btnSum);
108 }
109 }

posted @ 2019-06-09 20:04  什么!  阅读(157)  评论(0编辑  收藏  举报