-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonsAndText.java
More file actions
224 lines (177 loc) · 5.18 KB
/
ButtonsAndText.java
File metadata and controls
224 lines (177 loc) · 5.18 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Parth Upadhyay
// 3/25/22
// ButtonsAndText.java
// To use CardLayout
import java.awt.BorderLayout;
import java.awt.CardLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ButtonsAndText {
public ButtonsAndText()
{
JFrame frame = new JFrame();
frame.setSize(600, 600);
Practice pc = new Practice();
frame.setContentPane(pc);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
ButtonsAndText bat = new ButtonsAndText();
}
class Practice extends JPanel
{
private JButton button1, button2, button3;
private JTextArea t1, t2;
String sent = "Press A Button!";
Font ft = new Font("Serif", Font.BOLD, 40);
private JCheckBox checkBox1, checkBox2, checkBox3;
private JLabel name;
public Practice()
{
runIt();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
public void runIt()
{
JPanel panel = new JPanel();
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new BorderLayout());
JPanel panel3 = new JPanel();
JPanel btnPanel = new JPanel();
JPanel btnPanel2 = new JPanel();
JPanel tPanel = new JPanel();
JPanel check = new JPanel(new GridLayout(3,1,0,0));
JPanel tPanel2 = new JPanel();
JPanel boring = new JPanel();
JButton btn1 = new JButton("NEXT");
JButton btn2 = new JButton("PREVIOUS");
CardLayout cardLayout = new CardLayout();
panel.setLayout(cardLayout);
button1 = new JButton("Button 1");
type1 b1 = new type1();
button1.addActionListener( b1 );
btnPanel2.add( button1);
button2 = new JButton("Button 2");
type2 b2 = new type2();
button2.addActionListener( b2 );
btnPanel2.add( button2);
button3 = new JButton("Button 3");
type3 b3 = new type3();
button3.addActionListener( b3 );
btnPanel2.add( button3);
t1 = new JTextArea();
t1.setFont(ft);
t1.setText("\n\n\n " + sent);
t1.setOpaque(true);
t1.setBackground(Color.BLUE);
t1.setPreferredSize(new Dimension (580, 400));
tPanel.add( t1 );
checkBox1 = new JCheckBox("red");
CheckBoxListener cblistener = new CheckBoxListener();
checkBox1.addActionListener(cblistener);
checkBox1.setSelected(false);
check.add(checkBox1);
checkBox2 = new JCheckBox("orange");
checkBox2.addActionListener(cblistener);
checkBox2.setSelected(false);
check.add(checkBox2);
checkBox3 = new JCheckBox("yellow");
checkBox3.addActionListener(cblistener);
checkBox3.setSelected(false);
check.add(checkBox3);
t2 = new JTextArea();
t2.setFont(ft);
t2.setText("J\tJ\nT\tT\nE\tE\nX\tX\nT\tT\nA\tA\nR\tR\nE\tE\nA\tA\n");
t2.setOpaque(true);
t2.setBackground(Color.BLUE);
t2.setPreferredSize(new Dimension (400, 450));
tPanel2.add( t2 );
name = new JLabel();
name.setFont(ft);
name.setText("Parth U.");
name.setOpaque(true);
name.setBackground(Color.ORANGE);
name.setPreferredSize(new Dimension (240, 50));
boring.add( name );
panel.add(panel1, "link1");
panel.add(panel2, "link2");
panel.add(panel3, "link3");
btn1.addActionListener(e -> cardLayout.next(panel));
btn2.addActionListener(e -> cardLayout.previous(panel));
btnPanel.add(btn1);
btnPanel.add(btn2);
add(btnPanel, BorderLayout.NORTH);
panel1.add(tPanel, BorderLayout.SOUTH);
panel1.add(btnPanel2, BorderLayout.CENTER);
panel2.add(check, BorderLayout.WEST);
panel2.add(tPanel2, BorderLayout.CENTER);
panel3.add(name);
add(panel);
}
class type1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
sent = "Button 1";
t1.setText("\n\n\n " + sent);
repaint();
}
}
class type2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
sent = "Button 2";
t1.setText("\n\n\n " + sent);
repaint();
}
}
class type3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
sent = "Button 3";
t1.setText("\n\n\n " + sent);
repaint();
}
}
class CheckBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String color = e.getActionCommand();
if (color.equals("red")) {
checkBox1.setSelected(true);
checkBox2.setSelected(false);
checkBox3.setSelected(false);
t2.setBackground(Color.RED);
repaint();
}
if (color.equals("orange")) {
checkBox1.setSelected(false);
checkBox2.setSelected(true);
checkBox3.setSelected(false);
t2.setBackground(Color.ORANGE);
repaint();
}
if (color.equals("yellow")) {
checkBox1.setSelected(false);
checkBox2.setSelected(false);
checkBox3.setSelected(true);
t2.setBackground(Color.YELLOW);
repaint();
}
}
}
}
}