复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JTextArea; public class Frame extends JFrame { private JButton b[]; private JTextArea show1,show2,show3; // 构造方法 public Frame(String s) { // 设置标题栏内容 setTitle(s); // 设置初始化窗口位置 setBounds(100, 100, 500, 350); show1 = new JTextArea(1,8); add(show1); show2 = new JTextArea(1,8); add(show2); show3 = new JTextArea(1,8); add(show3); // 设置窗口布局 setLayout(new FlowLayout()); // 创建按钮对象 b=new JButton[14]; FlowLayout flow=new FlowLayout(); flow.setAlignment(FlowLayout.LEFT); flow.setHgap(2); flow.setVgap(8); setLayout(flow); b[1]=new JButton("X"); b[2]=new JButton("q"); b[3]=new JButton("x"); b[4]=new JButton("0"); b[5]=new JButton("1"); b[6]=new JButton("b"); b[7]=new JButton("o"); b[8]=new JButton("I"); b[9]=new JButton("J"); b[10]=new JButton("j"); b[11]=new JButton("Q"); b[12]=new JButton("O"); b[13]=new JButton("l"); for(int i=1;i<b.length;i++){ // b[i]=new JButton(""+i); // 把按钮容器添加到JFrame容器上 add(b[i]); } // 添加事件监听器对象(面向对象思想) // b[1].addActionListener(new EventListener1()); // b[12].addActionListener(new EventListener2()); // b[13].addActionListener(new EventListener3()); // for(int i=2;i<=11;i++){ // b[i].addActionListener(new EventListener4()); // } for(int i=1;i<=13;i++){ b[i].addActionListener(new EventListener1()); } validate(); // 设置窗口可视化 setVisible(true); // 设置窗口关闭 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class EventListener1 implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==b[1]) { show1.setText("Success"); b[1].setBackground(Color.RED); b[1].setForeground(Color.red); } else if(e.getSource()==b[12]) { show2.setText("Success"); b[12].setBackground(Color.RED); b[12].setForeground(Color.red); } else if(e.getSource()==b[13]) { show3.setText("Success"); b[13].setBackground(Color.RED); b[13].setForeground(Color.red); } else { show1.setText("Fault"); show2.setText("Fault"); show3.setText("Fault"); } } } // class EventListener1 implements ActionListener { // @Override // public void actionPerformed(ActionEvent e) { // show.setText("Success"); // b[1].setBackground(Color.RED); b[1].setForeground(Color.red); // } // class EventListener2 implements ActionListener { // @Override // public void actionPerformed(ActionEvent e) { // show.setText("Success"); // b[12].setBackground(Color.RED); b[12].setForeground(Color.red); // } // } // class EventListener3 implements ActionListener { // @Override // public void actionPerformed(ActionEvent e) { // show.setText("Success"); // b[13].setBackground(Color.RED); b[13].setForeground(Color.red); // } // } // class EventListener4 implements ActionListener { // @Override // public void actionPerformed(ActionEvent e) { // show.setText("Fault"); // } // } //} public static void main(String[] args) { new Frame("验证"); } }
最后
以上就是调皮鸡最近收集整理的关于java的Swing button和textarea的运用的全部内容,更多相关java的Swing内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复