Window is the basis of GUI programming. The visual components of applets or graphical interfaces are placed in windows. In the GUI, windows are part of the user's screen and play the role of a small screen in the screen. There are three types of windows:
Applet window: The Applet class manages this window, which is created and processed by the system when the application program is started;
Frame window (JFrame): This is a window in the usual sense, which supports the frame, title bar around the window, as well as minimize, maximize and close buttons;
A kind of borderless window (JWindow): no title bar, no frame, just an empty rectangle.
The object created with the JFrame class or its subclass in Swing is the JFrame window.
The main constructor of JFrame class:
Other common methods of JFrame class:
All containers in Swing can add components. Except for JPanel and its subclass (JApplet), other Swing containers do not allow components to be added directly. There are two ways to add components to other containers:
One is to use the getContentPane() method to obtain the content panel and then add the components. For example, the code in the program 5.1:
mw.getContentPane().add(button);
The meaning of this code is to get the content panel of the container and add the button button to this content panel.
Another way is to create an intermediate container of JPanel object, add the components to this container, and then use setContentPane() to set the container as a content panel. For example, code:
JPanel contentPane = new JPanel(); … mw.setContentPane(contentPane);
The above code places contentPane as a content panel.
[Example] A Java application that creates windows using the JFrame class. There is only one button in the window.
import javax.swing.*;public class Example5_1{ public static void main(String args[]){ JFrame mw = new JFrame("My first window"); mw.setSize(250,200 ); JButton button = new JButton ("I am a button"); mw.getContentPane().add(button); mw.setVisible(true); }}When writing GUI programs with Swing, you usually do not directly use JFrame to create window objects, but use JFrame-derived subclasses to create window objects. Specific requirements and special content of windows can be added to the subclass.
[Example] Define a JFrame-derived subclass MyWindowDemo to create a JFrame window. The constructor of MyWindowDemo class has five parameters: the title name of the window, the component that adds the window, the background color of the window, and the height and width of the window. In the main method, use the class MyWindowDemo to create two similar windows.
import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Example5_2{ public static MyWindowDemo mw1; public static MyW indowDemo mw2; public static void main(String args[]){ JButton static butt1 = new JButton("I am a button"); String name1 = "My first window"; String name2 = "My second window"; mw1 = new MyWindowDemo(name1,butt1,Color.blue, 350,450); mw1.setVisible(true); JButton butt2 = new JButton("I am another button"); mw2 = new MyWindowDemo(name2,butt2,Color.magenta,300,400); mw2.se tVisible(true); }} class MyWindowDemo extends JFrame{ public MyWindowDemo(String name,JButton button,Color c,int w,int h){ super(); setTitle(name); setSize(w,h) ; Container con = getContentPane(); con.add (button); con.setBackground(c); }} The display color is managed by the Color class of the java.awt package. Some commonly used colors are reserved in the Color class, see Table 11-3. Some common methods of the JFrame class are shown in the table below.
Common colors defined in the Color class
Some common methods of JFrame class