When producing a GUI, we will need to create windows on the screen. The Swing API has a number of classes that enable a program to create a new window on the screen or to make use of an existing window.
0013: // Stage B: creating a window. // GetDateProg.java 0014: // Barry Cornelius, 22nd November 1999 0015: import javax.swing. JFrame; 0016: public class GetDateProg 0017: { 0018: public static void main(final String[] pArgs) 0019: { 0020: final JFrame tJFrame = new JFrame("GetDateProg: Stage B"); 0021: tJFrame.setLocation(50, 100); 0022: tJFrame.setSize(300, 200); 0023: tJFrame.setVisible(true); 0024: } 0025: }
The program creates an object of the class JFrame. One of JFrame's constructors allows you to choose the string that is put into the title bar of the window:
JFrame tJFrame = new JFrame("GetDateProg: Stage B");The use of this class instance creation expression just creates the JFrame object: it does not display the window on the screen. This is done by a call of the method setVisible:
tJFrame.setVisible(true);
Unless you specify otherwise, when the window is displayed, it will be positioned in the top left-hand corner of the screen. The call:
tJFrame.setLocation(50, 100);says that you want the top left-hand corner of the window to be positioned 50 pixels from the left-hand side of the screen and 100 pixels down from the top of the screen. And the call:
tJFrame.setSize(300, 200);says that you want the window to be 300 pixels wide and 200 pixels high.
When this program is executed, it just displays a blank window on the screen. The result of executing this program is shown in the Figure.
The program has no code to understand the removal of the window: so if you want to stop the execution of this program, you will need to press Ctrl/C in the window in which you typed the command:
java GetDateProg