forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson30.java
More file actions
229 lines (136 loc) · 6.15 KB
/
JavaLesson30.java
File metadata and controls
229 lines (136 loc) · 6.15 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
225
226
227
228
229
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Calendar;
import java.util.Date;
public class JavaLesson30 extends JFrame{
JLabel nameLabel, streetLabel, stateLabel, dateLabel,
ageLabel, sexLabel, optionsLabel, aboutLabel;
JTextField nameText, streetText;
JComboBox stateList;
JSpinner dateSpin;
JSlider ageSlider;
JRadioButton maleRadio, femaleRadio;
ButtonGroup sexGroup;
JCheckBox morningCheck, afterNCheck, eveningCheck;
JTextArea aboutYou;
public static void main(String[] args){
new JavaLesson30();
}
public JavaLesson30(){
// Create the frame, position it and handle closing it
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Random Layout");
JPanel thePanel = new JPanel();
thePanel.setLayout(new GridBagLayout());
nameLabel = new JLabel(" Name:");
addComp(thePanel, nameLabel, 0, 0, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
nameText = new JTextField(30);
addComp(thePanel, nameText, 1, 0, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
streetLabel = new JLabel(" Street:");
addComp(thePanel, streetLabel, 0, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
streetText = new JTextField(30);
addComp(thePanel, streetText, 1, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
// Create a set of radio buttons and wrap them in a group
Box sexBox = Box.createVerticalBox();
maleRadio = new JRadioButton("Male");
femaleRadio = new JRadioButton("Female");
ButtonGroup sexGroup = new ButtonGroup();
sexGroup.add(maleRadio);
sexGroup.add(femaleRadio);
sexBox.add(maleRadio);
sexBox.add(femaleRadio);
sexBox.setBorder(BorderFactory.createTitledBorder("Sex"));
addComp(thePanel, sexBox, 3, 0, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
// Create a flow layout panel and space components 10px apart
JPanel statePanel = new JPanel();
statePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
stateLabel = new JLabel("State");
statePanel.add(stateLabel);
// Create a combo box that will hold the states
String[] states = {"PA", "NY", "OH", "WV", "NJ"};
stateList = new JComboBox(states);
statePanel.add(stateList);
dateLabel = new JLabel("Appointment Date");
statePanel.add(dateLabel);
// Get todays date
Date todaysDate = new Date();
// Create a date spinner & set default to today, no minimum, or max
// Increment the days on button presses
// Can also increment YEAR, MONTH, or DAY_OF_MONTH
dateSpin = new JSpinner(new SpinnerDateModel(todaysDate, null, null,
Calendar.DAY_OF_MONTH));
// DateEditor is an editor that handles displaying & editing the dates
JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(dateSpin, "dd/MM/yy");
dateSpin.setEditor(dateEditor);
statePanel.add(dateSpin);
ageLabel = new JLabel("Age: 50");
statePanel.add(ageLabel);
// Creates a slider with a min value of 1 thru 100
// and an initial value of 1
ageSlider = new JSlider(1, 99, 50);
// Create an instance of ListenForEvents to handle events
ListenForSlider lForSlider = new ListenForSlider();
// Tell Java that you want to be alerted when an event
// occurs on the slider
ageSlider.addChangeListener(lForSlider);
statePanel.add(ageSlider);
addComp(thePanel, statePanel, 1, 2, 5, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
// Create check boxs and assign them to a group
JCheckBox morningCheck, afterNCheck, eveningCheck;
Box optionBox = Box.createVerticalBox();
morningCheck = new JCheckBox("Morning");
afterNCheck = new JCheckBox("Afternoon");
eveningCheck = new JCheckBox("Evening");
optionBox.add(morningCheck);
optionBox.add(afterNCheck);
optionBox.add(eveningCheck);
optionBox.setBorder(BorderFactory.createTitledBorder("Time of Day"));
addComp(thePanel, optionBox, 1, 3, 2, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE);
// Create a text area that is 6 columns high and 40 long
aboutYou = new JTextArea(6, 40);
// Set the default text for the text area
aboutYou.setText("Tell us something about you");
// If text doesn't fit on a line, jump to the next
aboutYou.setLineWrap(true);
// Makes sure that words stay intact if a line wrap occurs
aboutYou.setWrapStyleWord(true);
// Adds scroll bars to the text area ----------
JScrollPane scrollbar1 = new JScrollPane(aboutYou, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Other options: VERTICAL_SCROLLBAR_ALWAYS, VERTICAL_SCROLLBAR_NEVER
addComp(thePanel, scrollbar1, 2, 3, 3, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
this.add(thePanel);
// Adjusts the size of the frame to best work for the components
this.pack();
this.setVisible(true);
// Define if the user can resize the frame (true by default)
this.setResizable(false);
}
// Sets the rules for a component destined for a GridBagLayout
// and then adds it
private void addComp(JPanel thePanel, JComponent comp, int xPos, int yPos, int compWidth, int compHeight, int place, int stretch){
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = xPos;
gridConstraints.gridy = yPos;
gridConstraints.gridwidth = compWidth;
gridConstraints.gridheight = compHeight;
gridConstraints.weightx = 100;
gridConstraints.weighty = 100;
gridConstraints.insets = new Insets(5,5,5,5);
gridConstraints.anchor = place;
gridConstraints.fill = stretch;
thePanel.add(comp, gridConstraints);
}
// Implements ActionListener so it can react to events on components
private class ListenForSlider implements ChangeListener{
// Called when the spinner is changed
public void stateChanged(ChangeEvent e) {
// Check if the source of the event was the button
if(e.getSource() == ageSlider){
// Change the value for the label next to the spinner
ageLabel.setText("Age: " + ageSlider.getValue() );
}
}
}
}