jsugar

Added the first tiny functions for the software.

Author
Vngngdn
Date
July 11, 2016, 5 p.m.
Hash
eeef80237f004b52370f8eef5dabe6c5abc2dc67
Parents
Modified files
.gitignore
Main.java

.gitignore

1 addition and 0 deletions.

View changes Hide changes
+
1
*.class

Main.java

96 additions and 0 deletions.

View changes Hide changes
+
1
 * Main.java - (Temporary) Main file in which the functionality is put.
+
2
 * Copyright © 2016 Maarten "Vngngdn" Vangeneugden
+
3
 * 
+
4
 * This program is free software: you can redistribute it and/or modify
+
5
 * it under the terms of the GNU General Public License as published by
+
6
 * the Free Software Foundation, either version 3 of the License, or
+
7
 * (at your option) any later version.
+
8
 * 
+
9
 * This program is distributed in the hope that it will be useful,
+
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
+
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+
12
 * GNU General Public License for more details.
+
13
 * 
+
14
 * You should have received a copy of the GNU General Public License
+
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
16
 */
+
17
+
18
/**
+
19
 * Main class for the program.
+
20
 *
+
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
+
24
 */
+
25
import javax.swing.*; // FIXME: Maybe namespacing it to "javax.swing;" is a better idea.
+
26
import java.util.NoSuchElementException;
+
27
+
28
public class Main {
+
29
	private JPanel panel;
+
30
+
31
	/**
+
32
	 * Creates a button in the GUI for interaction.
+
33
	 * This function offers a convenient way to create a button, that can be
+
34
	 * directly interacted with by the user. After creation, the button itself
+
35
	 * is returned to the caller, if he wishes to do something else with it.
+
36
	 * @param text The text that will be displayed in the button.
+
37
	 * @param action The action that will be returned to the action listener.
+
38
	 * @return The button that was created.
+
39
	 */
+
40
	public JButton createButton(String text, String action) {
+
41
		JButton button = new JButton(text);
+
42
		button.setActionCommand(action);
+
43
		this.panel.add(button);
+
44
		return button;
+
45
	}
+
46
+
47
	/**
+
48
	 * Creates a label in the GUI for interaction.
+
49
	 * This function offers a convenient way to create a label, that can be
+
50
	 * directly interacted with by the user. After creation, the label itself
+
51
	 * is returned to the caller, if he wishes to do something else with it.
+
52
	 * @param text The text that will be displayed in the label.
+
53
	 * @return The label that was created.
+
54
	 */
+
55
	public JLabel createLabel(String text) {
+
56
		JLabel label = new JLabel(text);
+
57
		this.panel.add(label);
+
58
		return label;
+
59
	}
+
60
+
61
	/**
+
62
	 * Removes the given component from the GUI.
+
63
	 * This method allows its caller to remove a component from the GUI.
+
64
	 * @param component The component to be removed.
+
65
	 * @throws NoSuchElementException if the given component does not exist in
+
66
	 * the GUI.
+
67
	 * @throws NullPointerException if the given component is a null pointer.
+
68
	 */
+
69
	public void removeComponent(JComponent component) {
+
70
		int originalSize = this.panel.getComponentCount();
+
71
		this.panel.remove(component);
+
72
		int newSize = this.panel.getComponentCount();
+
73
		if (originalSize != newSize+1) {
+
74
			throw new NoSuchElementException("The given component does not exist in the GUI.");
+
75
		}
+
76
	}
+
77
	/**
+
78
	 * Main function.
+
79
	 * 
+
80
	 * This is the first function that is being called in the program.
+
81
	 */
+
82
	public static void main(String[] args) {
+
83
		// Creat©s2016eyournamewith a double buffer and flow layout.
+
84
		JPanel panel = new JPanel();
+
85
		JFrame frame = new JFrame("Hello world!");
+
86
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
87
		JLabel lblHelloWorld = new JLabel("Hello world... :D");
+
88
		//frame.getContentPane().add(lblHelloWorld); // So you use a get() in order to set() data? #JavaWTF
+
89
		panel.add(lblHelloWorld);
+
90
		frame.setContentPane(panel);
+
91
+
92
		frame.pack();
+
93
		frame.setVisible(true);
+
94
	}
+
95
}
+
96