jsugar

Renamed the Main.java file to Window.java, which suits its supposed goal better. Also, Window.java got a constructor method, so other classes can now create new Windows as they please.

Author
Vngngdn
Date
July 14, 2016, 5 p.m.
Hash
8e620b5003a81c18ce079a26eb0785f853178d67
Parent
f17774d8fa11121751d81e9bc6a701e3e00fe276
Modified file
Main.java β†’ Window.java

Main.java β†’ Window.java ΒΆ

36 additions and 24 deletions.

View changes Hide changes
1
1
 * Main.java - (Temporary) Main file in which the functionality is put.
2
-
 * Copyright Β© 2016 Maarten "Vngngdn" Vangeneugden
+
2
 * Copyright Β© 2016 Maarten "Vngngdn" Vangeneugden
3
3
 * 
4
4
 * This program is free software: you can redistribute it and/or modify
5
5
 * it under the terms of the GNU General Public License as published by
6
6
 * the Free Software Foundation, either version 3 of the License, or
7
7
 * (at your option) any later version.
8
8
 * 
9
9
 * This program is distributed in the hope that it will be useful,
10
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
12
 * GNU General Public License for more details.
13
13
 * 
14
14
 * You should have received a copy of the GNU General Public License
15
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
16
 */
17
17
18
18
/**
19
19
 * Main class for the program.
20
-
 *
+
20
 *
21
21
 * In this class, all parts of the program are being brought together, and their
22
-
 * functionality is 'activated', if you will.
23
-
 * @author Maarten Vangeneugden
+
22
 * he's familiar with as being a "window". To make it functional, the developer
+
23
 * can make use of a series of methods to add components to said window, remove
+
24
 * components, and so on.
+
25
 * Currently, Window also contains methods to show dialogs. This will be cleaned
+
26
 * in the near future.
+
27
 * @author Maarten Vangeneugden
24
28
 */
25
29
import javax.swing.*; // FIXME: Maybe namespacing it to "javax.swing;" is a better idea.
26
30
import java.util.NoSuchElementException;
27
31
28
32
public class Main {
29
-
	private JPanel panel;
30
-
+
33
	private JPanel panel; // The panel that contains all the components.
+
34
	private JFrame frame; // The "window" being presented to the user.
+
35
+
36
	/**
+
37
	 * Constructor of Window.
+
38
	 * By creating a new Window instance, this constructor will automatically
+
39
	 * start the initialization of the GUI. After doing so, the caller can
+
40
	 * start adding components to the window as pleased.
+
41
	 * @param title The title to be shown in the window's title bar.
+
42
	 */
+
43
	public Window() {
+
44
		this.panel = new JPanel();
+
45
		// TODO: The current title is "Hello world!" but that will become caller
+
46
		// defined soon.
+
47
		JFrame frame = new JFrame("Hello world!");
+
48
		// Makes it so that if the user clicks the X in the titlebar, the window
+
49
		// closes:
+
50
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
51
		//frame.getContentPane().add(lblHelloWorld); // So you use a get() in order to set() data? #JavaWTF
+
52
		frame.setContentPane(this.panel); // Connecting the component panel to the window.
+
53
		// Makes the window fit to the necessary width and height, so it can show all "subcomponents".
+
54
		frame.pack(); 	
+
55
		frame.setVisible(true); // Makes the window visible to the user.
+
56
		this.frame = frame;
+
57
	}
+
58
+
59
+
60
31
61
	/**
32
62
	 * Creates a button in the GUI for interaction.
33
63
	 * This function offers a convenient way to create a button, that can be
34
64
	 * directly interacted with by the user. After creation, the button itself
35
65
	 * is returned to the caller, if he wishes to do something else with it.
36
66
	 * @param text The text that will be displayed in the button.
37
67
	 * @param action The action that will be returned to the action listener.
38
68
	 * @return The button that was created.
39
69
	 */
40
70
	public JButton createButton(String text, String action) {
41
71
		JButton button = new JButton(text);
42
72
		button.setActionCommand(action);
43
73
		this.panel.add(button);
44
74
		return button;
45
75
	}
46
76
47
77
	/**
48
78
	 * Ask the user for input through a dialog box.
49
79
	 * This method presents the user with an input field, that can accept
50
80
	 * textual input. The method will return the given input after the user's
51
81
	 * clicked a button to send.
52
82
	 * @param text The text/question to be asked to the user.
53
83
	 * @return A String, equal to what the user entered.
54
84
	 * @throws NullPointerException if text is a null pointer.
55
85
	 */
56
86
	public String inputDialog(String text) {
57
87
		if (text == null) {
58
88
			throw new NullPointerException("The given text/question was a null pointer.");
59
89
		}
60
90
		return JOptionPane.showInputDialog(text);
61
91
	}
62
92
63
93
	/**
64
94
	 * Give the user a dialog box.
65
95
	 * This method can be used to provide a simple dialog to the user.
66
96
	 * This will show the user the given question, after which a boolean value
67
97
	 * is returned, holding the choice.
68
98
	 * @param text The text/question to be asked to the user.
69
99
	 * @return True if the user confirms, False if he denies.
70
100
	 * @throws NullPointerException if text is a null pointer.
71
101
	 */
72
102
	public boolean confirmDialog(String text) {
73
103
		if (text == null) {
74
104
			throw new NullPointerException("The given text/question was a null pointer.");
75
105
		}
76
106
		final int ACCEPTED = 0;
77
107
		final int DENIED = 1;
78
108
		int result = this.choiceDialog(text, new String[]{"Confirm", "Deny"});
79
109
		if (result == ACCEPTED) {
80
110
			return true;
81
111
		}
82
112
		else {
83
113
			return false;
84
114
		}
85
115
	}
86
116
87
117
	/**
88
118
	 * Give the user a choice dialog box.
89
119
	 * This method gives the user a simple dialog with predefined choices.
90
120
	 * These choices are to be provided by the caller in a simple array.
91
121
	 * Tip: This method works extremely well with arbitrary created choices.
92
122
	 * That is: if the outcome of the dialog is trivial (e.g. Only 1 choice),
93
123
	 * then that value is immediately returned.
94
124
	 * @param text The text/question to be asked to the user.
95
125
	 * @param choices An array of Strings, containing the choices the user can
96
126
	 * pick.
97
127
	 * @return The index value of the picked choice, or -1 if no choices were
98
128
	 * given.
99
129
	 * @throws NullPointerException if text is a null pointer.
100
130
	 */
101
131
	public int choiceDialog(String text, String[] choices) {
102
132
		if (text == null) {
103
133
			throw new NullPointerException("The given text/question was a null pointer.");
104
134
		}
105
135
		// First: handling the trivial cases:
106
136
		if (choices.length == 0) {
107
137
			return -1;
108
138
		}
109
139
		else if (choices.length == 1) {
110
140
			return 0;
111
141
		}
112
142
		int answer = JOptionPane.CLOSED_OPTION;
113
143
		// The dialog needs to be shown again until the user has made a possible
114
144
		// choice, i.e. Chickening out using the close button is not possible
115
145
		// (Because that returns CLOSED_OPTION).
116
146
		while (answer == JOptionPane.CLOSED_OPTION) {
117
147
				JOptionPane.showOptionDialog(
118
148
					null, // The parent component. May become the panel?
119
149
					text, // The text/question to describe the goal
120
150
					"Dialog", // The text in the title bar
121
151
					JOptionPane.DEFAULT_OPTION, // The kind of available options
122
152
					JOptionPane.QUESTION_MESSAGE, // The type of message
123
153
					null, // The icon to show
124
154
					choices, // The possible choices
125
155
					choices[0] // The standard choice
126
156
					);
127
157
		}
128
158
		return answer;
129
159
	}
130
160
		
131
161
132
162
	/**
133
163
	 * Creates a label in the GUI for interaction.
134
164
	 * This function offers a convenient way to create a label, that can be
135
165
	 * directly interacted with by the user. After creation, the label itself
136
166
	 * is returned to the caller, if he wishes to do something else with it.
137
167
	 * @param text The text that will be displayed in the label.
138
168
	 * @return The label that was created.
139
169
	 */
140
170
	public JLabel createLabel(String text) {
141
171
		JLabel label = new JLabel(text);
142
172
		this.panel.add(label);
143
173
		return label;
144
174
	}
145
175
146
176
	/**
147
177
	 * Removes the given component from the GUI.
148
178
	 * This method allows its caller to remove a component from the GUI.
149
179
	 * @param component The component to be removed.
150
180
	 * @throws NoSuchElementException if the given component does not exist in
151
181
	 * the GUI.
152
182
	 * @throws NullPointerException if the given component is a null pointer.
153
183
	 */
154
184
	public void removeComponent(JComponent component) {
155
185
		int originalSize = this.panel.getComponentCount();
156
186
		this.panel.remove(component);
157
187
		int newSize = this.panel.getComponentCount();
158
188
		if (originalSize != newSize+1) {
159
189
			throw new NoSuchElementException("The given component does not exist in the GUI.");
160
190
		}
161
191
	}
162
192
	/**
163
-
	 * Main function.
164
-
	 * 
165
-
	 * This is the first function that is being called in the program.
166
-
	 */
167
-
	public static void main(String[] args) {
168
-
		// CreatΒ©s2016eyournamewith a double buffer and flow layout.
169
-
		JPanel panel = new JPanel();
170
-
		JFrame frame = new JFrame("Hello world!");
171
-
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
172
-
		JLabel lblHelloWorld = new JLabel("Hello world... :D");
173
-
		//frame.getContentPane().add(lblHelloWorld); // So you use a get() in order to set() data? #JavaWTF
174
-
		panel.add(lblHelloWorld);
175
-
		frame.setContentPane(panel);
176
-
177
-
		frame.pack();
178
-
		frame.setVisible(true);
179
-
	}
180
-
}
181
193