import java.awt.*; //windows, buttons, etc. need the "SWING" packages import java.awt.event.*; //needed for the "ActionListener" import javax.swing.*; //windows, buttons, etc. need the "SWING" packages /** * Class describing the creation of a simple window and a few object * components placed on.
* Attributes: Title,Size,Close function,Visible or not
* Additionally it shows simple ways how to add components to the window.
* Free to use, copy, change or make CREAM out of it!
* Prequisits: Basic knowlege of objects, classes & methods in Java * * @author Andreas Mastichis * @version 0.1 */ public class SimpleWindow extends JFrame { /** * Constructor of the Window class */ public SimpleWindow() { /** * WINDOW CREATION */ // Create title for window super("My window title..."); // Set the initial size of window: width, height setSize(300, 200); // Set the CLOSE operation of the window (X) setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set if the window is visible or not setVisible(true); /** * CONTAINER CREATION */ // Create a Java CONTAINER, name: "contentArea" // This will be the pane of the window... Container contentArea = getContentPane(); contentArea.setBackground(Color.lightGray); /** * LAYOUT CREATION This layout uses the Java "Flow" layout manager. This * means that it "flows" the components from left to right and when the * a component doesn't fit in a line of the "window", it is pushed to * the next line below (this depends on the window size). NOTE: */ // Create a Java LAYOUT MANAGER, name: "layoutManagerA" // for components to be placed on FlowLayout layoutManagerA = new FlowLayout(); /** * CONNECT CONTAINER with LAYOUT (must be created below the layout * representing it) */ contentArea.setLayout(layoutManagerA); /** * LABEL CREATION */ // Create a Java LABEL, name: "nameLabel", caption: "Name: " JLabel nameLabel = new JLabel("Name: "); contentArea.add(nameLabel); /** * TEXTFIELD/BOX CREATION (create after the label representing the * textbox) */ // Create a Java TEXTFIELD, name: "nameField", caption show: "enter name // here" JTextField nameField = new JTextField("enter name here", 15); contentArea.add(nameField); /** * COMBOBOX CREATION */ // Create a Java COMBOBOX, name: "comboCity" JComboBox comboCity = new JComboBox(); comboCity.addItem("Nicosia"); comboCity.addItem("Limassol"); comboCity.addItem("Larnaca"); comboCity.addItem("Paphos"); // Add the COMBOBOX: "comboCity" to the CONTAINER: "contentArea" contentArea.add(comboCity); /** * TEXTAREA CREATION */ // Create a Java TEXTAREA, name: "address", caption show: "enter address // here", // rows: 4 lines, colums: 20 characters width JTextArea address = new JTextArea("enter address here", 4, 20); // Add the TEXTAREA: "address" to the CONTAINER: "contentArea" contentArea.add(address); /** * SCROLLPANE CREATION (must be directly below the relevant component to * be scrolled) */ // Create a Java SCROLLPANE, name: "addressScroll", vertical: always // show, // horizontal: always show JScrollPane addressScroll = new JScrollPane(address, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // Add the SCROLLPANE: "addressScroll" to the CONTAINER: "contentArea" contentArea.add(addressScroll); /** * BUTTON CREATION */ // Create a Java BUTTON, name: "closeButton", caption: "Close" JButton closeButton = new JButton("Close"); // Add Java ACTIONLISTENER: "CloseHandler" (to "handle" the event) closeButton.addActionListener(new CloseHandler()); // Add BUTTON: "closeButton" to CONTAINER: "contentArea" contentArea.add(closeButton); // Apply/show the contents (buttons,textBoxes,etc.) // which are binded to the CONTAINER specified here setContentPane(contentArea); } /** * ACTIONLISTENER HANDLER -CLASS (create a method to "capture" the * "closeButton" event) */ class CloseHandler implements ActionListener { public void actionPerformed(ActionEvent e) { // Exit the program System.exit(0); } } /** * MAIN METHOD CREATION */ public static void main(String[] args) { // Create an instance of the widow, so it can be displayed Window simpleWindow = new SimpleWindow(); } }