forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson40.java
More file actions
230 lines (144 loc) · 6.02 KB
/
JavaLesson40.java
File metadata and controls
230 lines (144 loc) · 6.02 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
230
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.*;
// In swing you use a JFrame, to make the program
// work online you use JApplet
public class JavaLesson40 extends JApplet{
// The main panel that holds everything
JPanel thePanel;
// The panels that hold the radio buttons
JPanel ques1Panel, ques2Panel, ques3Panel, ques4Panel;
// When clicked the personality report is displayed
JButton getResultBut;
// The radio buttons for each personality quirk
JRadioButton extravertRadio, introvertRadio,
sensorRadio, intuitiveRadio, feelerRadio,
thinkerRadio, judgingRadio, perceivingRadio;
// Holds the personality report
JEditorPane yourReport;
// You use init() instead of main() with Applets
public void init(){
// Sets the size of the frame
this.setSize(675, 870);
// Creates the main panel and makes everything justify left
thePanel = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEFT));
// The panels for the radio buttons
ques1Panel = new JPanel();
ques2Panel = new JPanel();
ques3Panel = new JPanel();
ques4Panel = new JPanel();
// Creates borders that surround the radio buttons
Border border1 = BorderFactory.createTitledBorder("Do you prefer to work");
Border border2 = BorderFactory.createTitledBorder("Which is most important");
Border border3 = BorderFactory.createTitledBorder("Do you act on");
Border border4 = BorderFactory.createTitledBorder("Which do you prefer");
// Attaches the border to the right panels
ques1Panel.setBorder(border1);
ques2Panel.setBorder(border2);
ques3Panel.setBorder(border3);
ques4Panel.setBorder(border4);
// Makes are only one radio button can be selected
ButtonGroup group1 = new ButtonGroup();
ButtonGroup group2 = new ButtonGroup();
ButtonGroup group3 = new ButtonGroup();
ButtonGroup group4 = new ButtonGroup();
// Creates and sets text for radio buttons
extravertRadio = new JRadioButton("In groups");
introvertRadio = new JRadioButton("On your own");
sensorRadio = new JRadioButton("The specifics");
intuitiveRadio = new JRadioButton("The big picture");
feelerRadio = new JRadioButton("What feels right");
thinkerRadio = new JRadioButton("List of facts");
judgingRadio = new JRadioButton("To plan");
perceivingRadio = new JRadioButton("To adapt");
// Sets some radio buttons to true by default
extravertRadio.setSelected(true);
sensorRadio.setSelected(true);
feelerRadio.setSelected(true);
judgingRadio.setSelected(true);
// Adds radio buttons to their panels
ques1Panel.add(extravertRadio);
ques1Panel.add(introvertRadio);
ques2Panel.add(sensorRadio);
ques2Panel.add(intuitiveRadio);
ques3Panel.add(feelerRadio);
ques3Panel.add(thinkerRadio);
ques4Panel.add(judgingRadio);
ques4Panel.add(perceivingRadio);
// Assigns radio buttons to be in groups together
group1.add(extravertRadio);
group1.add(introvertRadio);
group2.add(sensorRadio);
group2.add(intuitiveRadio);
group3.add(feelerRadio);
group3.add(thinkerRadio);
group4.add(judgingRadio);
group4.add(perceivingRadio);
// Adds the radio button panels to the main panel
thePanel.add(ques1Panel);
thePanel.add(ques2Panel);
thePanel.add(ques3Panel);
thePanel.add(ques4Panel);
// Creates a button
getResultBut = new JButton("Get Result");
// Creates an object that will monitor button clicks
GetResultsListener butListener = new GetResultsListener();
// Assigns an object that will monitor this buttons clicks
// When clicked a method will execute
getResultBut.addActionListener(butListener);
// Add the button & panel to the frame and show the frame
thePanel.add(getResultBut);
this.add(thePanel);
this.setVisible(true);
}
class GetResultsListener implements ActionListener{
// Called when the button is clicked
public void actionPerformed(ActionEvent e) {
// Define strings that will make the html that the
// JEditorPane will display
String pageToOpen = "",
directoryLoc = "file:///Users/rickeyhrabowskie/Documents/workspace/Java Code/src/";
String textToDisplay ="<html><div><img src=\"" + directoryLoc;
// Check if the result button was clicked
if (e.getSource() == getResultBut){
// Add to the string based on selected radio button
if (extravertRadio.isSelected()) pageToOpen += "E";
if (introvertRadio.isSelected()) pageToOpen += "I";
if (sensorRadio.isSelected()) pageToOpen += "S";
if (intuitiveRadio.isSelected()) pageToOpen += "N";
if (feelerRadio.isSelected()) pageToOpen += "F";
if (thinkerRadio.isSelected()) pageToOpen += "T";
if (judgingRadio.isSelected()) pageToOpen += "J";
if (perceivingRadio.isSelected()) pageToOpen += "P";
// Remove panels to make way for a report
thePanel.remove(ques1Panel);
thePanel.remove(ques2Panel);
thePanel.remove(ques3Panel);
thePanel.remove(ques4Panel);
// Finish the html file that JEditorPane will display
textToDisplay += pageToOpen + ".png" + "\" /></html>";
// Define what JEditorPane will display
yourReport = new JEditorPane("text/html", textToDisplay);
// Shut off editing and size the JEditorPane
yourReport.setEditable(false);
yourReport.setSize(650, 825);
// Add the JEditorPane to a scroller and define how
// to handle scrollbars
JScrollPane scroller = new JScrollPane(yourReport,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// Size the scroll pane
scroller.setPreferredSize(new Dimension(650, 825));
// Add Scroll pane and JEditorPane to the frame
thePanel.add(scroller);
getResultBut.setVisible(false);
// Redraw the frame after the changes are made
thePanel.revalidate();
thePanel.repaint();
}
}
}
}