-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUIApplication.java
167 lines (133 loc) · 5.42 KB
/
GUIApplication.java
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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class GUIApplication implements ActionListener {
private JFrame frame;
private Container contentPane;
private JTextField textA;
private JTextField textB;
private JTextField textC;
private JRadioButton one;
private JRadioButton two;
private JRadioButton three;
private JCheckBox yes;
private JCheckBox no;
public static void main(String[] args) {
GUIApplication gui = new GUIApplication();
}
public GUIApplication() {
frame = new JFrame("Best Application");
contentPane = (JPanel)frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.setLayout(new GridLayout(2,2));
makeMenus();
makeOptions();
frame.pack();
frame.setVisible(true);
}
private void makeMenus() {
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menuStuffz = new JMenu("Stuffz");
menuStuffz.setMnemonic(KeyEvent.VK_S);
menuBar.add(menuStuffz);
JMenuItem menuItemSave = new JMenuItem("Save");
menuItemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
menuItemSave.addActionListener(this);
menuStuffz.add(menuItemSave);
JMenuItem menuItemExit = new JMenuItem("Exit");
menuItemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
menuItemExit.addActionListener(this);
menuStuffz.add(menuItemExit);
}
private void makeOptions() {
makeTextFields();
makeRadioButtons();
makeCheckBoxes();
makeImage();
}
private void makeTextFields() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JPanel subPanel = new JPanel();
subPanel.setLayout(new BoxLayout(subPanel, BoxLayout.Y_AXIS));
subPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
subPanel.add(new JLabel("A:"));
subPanel.add(new JLabel("B:"));
subPanel.add(new JLabel("C:"));
panel.add(subPanel);
subPanel = new JPanel();
subPanel.setLayout(new BoxLayout(subPanel, BoxLayout.Y_AXIS));
subPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
textA = new JTextField("ljahgjlwaekgnaelswdmhawekhawuoitbjqwkuetgjkadsbgjkobw4qejg");
textB = new JTextField("");
textC = new JTextField("...");
JScrollPane scrollBarA = new JScrollPane(textA);
scrollBarA.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBarA.setMaximumSize(new Dimension(100,30));
subPanel.add(scrollBarA);
JScrollPane scrollBarB = new JScrollPane(textB);
scrollBarB.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollBarB.setMaximumSize(new Dimension(100,30));
subPanel.add(scrollBarB);
JScrollPane scrollBarC = new JScrollPane(textC);
scrollBarC.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollBarC.setMaximumSize(new Dimension(100,30));
subPanel.add(scrollBarC);
panel.add(subPanel);
contentPane.add(panel);
}
private void makeRadioButtons() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
ButtonGroup buttonGroup = new ButtonGroup();
one = new JRadioButton("one", true);
two = new JRadioButton("two", false);
three = new JRadioButton("three", false);
buttonGroup.add(one);
buttonGroup.add(two);
buttonGroup.add(three);
panel.add(one);
panel.add(two);
panel.add(three);
contentPane.add(panel);
}
private void makeCheckBoxes() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
yes = new JCheckBox("yes", true);
no = new JCheckBox("no", false);
panel.add(yes);
panel.add(no);
contentPane.add(panel);
}
private void makeImage() {
JPanel panel = new JPanel();
JLabel image = new JLabel(new ImageIcon("cute_cat.jpg"), JLabel.CENTER);
panel.add(image);
contentPane.add(panel);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Save")) printToText();
else if (e.getActionCommand().equals("Exit")) System.exit(0);
}
public void printToText() {
try {
PrintStream fileWriter = new PrintStream("no_u.txt");
String output = "| Choice List |\n Text Fields:\n " + textA.getText() + "\n " + textB.getText() + "\n " + textC.getText() + "\n Radio Button:\n";
if (one.isSelected()) output += " one\n";
else if (two.isSelected()) output += " two\n";
else if (three.isSelected()) output += " three\n";
output += " Check Boxes:\n";
if (yes.isSelected()) output += " yes\n";
if (no.isSelected()) output += " no\n";
output += "\nDid you like the cat?";
fileWriter.print(output);
fileWriter.close();
}
catch (IOException e) {
System.out.println("IO Exception: " + e);
}
}
}