Source of GUIStuff.java


  1: import java.util.Scanner;
  2: import javax.swing.*;
  3: import java.awt.*;

  5: /**
  6:  * A program to demonstrate how Swing GUIs are created.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class GUIStuff {

 12:     public static void main(String[] args) {
 13:         // introduce yourself
 14:         System.out.print("\n\n"
 15:                 + "GUI Stuff\n"
 16:                 + "---------\n\n"
 17:                 + "This program creates and manipulates windows.\n\n"
 18:                 + "After each time you press enter, "
 19:                 + "you need to click back in THIS window "
 20:                 + "to\ncontinue with the demonstration.\n\n\n");

 22:         // create a window -- but don't make it visible
 23:         JFrame win = new JFrame("My Win");
 24:         System.out.println("A new window has been created!");
 25:         System.out.println("\nWhaddaya mean you can't see it?\n");
 26:         pause();

 28:         // make it visible -- but tiny
 29:         win.setVisible(true);
 30:         System.out.println("\nOh, sorry!  Now it's not invisible.\n");
 31:         pause();

 33:         // make it larger
 34:         win.setSize(300, 200);
 35:         System.out.println("\nAnd now it's not so tiny.\n");
 36:         pause();

 38:         // make a label and add it to the window
 39:         JLabel lbl1 = new JLabel("Hello, World!");
 40:         win.add(lbl1);
 41:         win.setVisible(true);   // need to "refresh" the window
 42:         System.out.println("\nNow it has a label in it.\n");
 43:         pause();

 45:         // add a text field at the bottom of the window
 46:         JTextField tf = new JTextField("Initial Text");
 47:         win.getContentPane().add(tf, BorderLayout.PAGE_END);
 48:         win.setVisible(true);
 49:         System.out.println("\nNow it has a text field "
 50:                 + "(type in the text field).\n");
 51:         pause();

 53:         // add a button to the window -- covering the label
 54:         JButton b1 = new JButton("OK");
 55:         win.add(b1);
 56:         win.setVisible(true);
 57:         System.out.println("\nNow it's got a button (click the button).\n");
 58:         pause();

 60:         // create and show a window with flow layout
 61:         JFrame win2 = new JFrame("Flow Layout Window");
 62:         win2.setSize(300, 200);
 63:         win2.setLayout(new FlowLayout());
 64:         win2.add(new JLabel(">>>First Label<<<"));
 65:         win2.add(new JLabel(">>>Second Label<<<"));
 66:         win2.add(new JLabel(">>>Third Label<<<"));
 67:         win2.add(new JLabel(">>>Fourth Label<<<"));
 68:         win2.add(new JLabel(">>>Fifth Label<<<"));
 69:         win2.setVisible(true);
 70:         System.out.println("\nAnother window -- "
 71:                 + "resize it and watch how the labels move.\n");
 72:         pause();

 74:         // create and show a window with border layout
 75:         JFrame win3 = new JFrame("Border Layout Window");
 76:         win3.setSize(300, 200);
 77:         win3.setLayout(new BorderLayout());
 78:         win3.add(new JLabel(">>>North Label<<<"), BorderLayout.NORTH);
 79:         win3.add(new JLabel(">>>South Label<<<"), BorderLayout.SOUTH);
 80:         win3.add(new JLabel(">>>East Label<<<"), BorderLayout.EAST);
 81:         win3.add(new JLabel(">>>West Label<<<"), BorderLayout.WEST);
 82:         win3.add(new JLabel(">>>Center Label<<<"), BorderLayout.CENTER);
 83:         win3.setVisible(true);
 84:         System.out.println("\nAnother window -- "
 85:                 + "resize it and watch how the labels move.\n");
 86:         pause();

 88:         // create and show a window with grid layout
 89:         JFrame win4 = new JFrame("Grid Layout Window");
 90:         win4.setSize(300, 200);
 91:         win4.setLayout(new GridLayout(2, 3));
 92:         win4.add(new JLabel(">>>First Label<<<"));
 93:         win4.add(new JLabel(">>>Second Label<<<"));
 94:         win4.add(new JLabel(">>>Third Label<<<"));
 95:         win4.add(new JLabel(">>>Fourth Label<<<"));
 96:         win4.add(new JLabel(">>>Fifth Label<<<"));
 97:         win4.setVisible(true);
 98:         System.out.println("\nAnother window -- "
 99:                 + "resize it and watch how the labels move.\n");
100:         pause();

102:         // tell user how to end the program, because GUI programs don't end
103:         // automatically.
104:         System.out.println("\n\n\n"
105:                 + "Hit ^C or close this window to end the program\n\n\n");
106:     }

108:     /**
109:      * Prompt the user and wait for them to press the enter key.
110:      */
111:     public static void pause() {
112:         System.out.print("Press enter key to continue...");
113:         Scanner kbd = new Scanner(System.in);
114:         kbd.nextLine();
115:     }

117: }