Windows are the foundation of GUI programming, and the visual components of applications with small applications or graphical interfaces are placed in Windows, and in the GUI, Windows are part of the user's screen, playing a small screen in the screen. There are three types of windows:
Applet window: The Applet class manages this window and is created and processed by the system when the application program is started;
frame window (JFrame): This is the usual sense of the window, it supports the window surrounding the frame, title bar, as well as minimize, maximize and close the button;
A borderless window (JWindow): no title bar, no frame, just an empty rectangle.
The object created with the JFrame class in swing or its subclasses is the JFrame window.
The main construction method of the JFrame class:
- JFrame (): Create Untitled Window object;
- JFrame (String s): Creates a Window object with a header name that is a string s.
Other common ways to JFrame classes:
- SetBounds (int x,int y,int width,int height): parameter x,y Specifies where the window appears on the screen; parameter width,height specifies the width and height of the window. Units are pixels.
- setSize (int width,int height): Sets the window size, parameter width and height specifies the width and height of the window, in pixels.
- SetBackground (color C): Sets the background color of the window in Parameter C.
- SetVisible (Boolean B): Parameter B sets the window to be visible or invisible. JFrame is not visible by default.
- Pack (): Displays the window in a compact manner. If you do not use this method, the components in the window may not be visible when the window is initially present, and they may be visible when the user resizes the window.
- Settitle (String name): Sets the name of the window with the parameter name.
- GetTitle (): Gets the name of the window.
- Setresiable (boolean m): Sets whether the current window can be resized (by default resizable).
A container in swing can add components, except JPanel and its subclasses (JApplet), and 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 get the content panel, and then add the component. For example, the code in the example 5.1 program:
Mw.getcontentpane (). Add (button);
The meaning of this code is to get the content panel of the container and Add button buttons to this content panel.
The other is to create an intermediate container for the JPanel object, add the component to the container, and then use Setcontentpane () to place the container as the content panel. For example, code:
JPanel ContentPane = new JPanel ();
... Mw.setcontentpane (ContentPane);
The above code puts contentpane into the content panel.
"Example" a Java application that creates a window with the JFrame class. The window has only one button.
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 you write GUI programs with swing, you usually do not create window objects directly with JFrame, and you create window objects with subclasses derived from JFrame, and in subclasses you can add specific requirements and special content to the window.
The example defines jframe derived subclasses Mywindowdemo create jframe windows. The constructor method for class Mywindowdemo has five parameters: the caption Name of the window, the component of 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 Mywindowdemo 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.setvisible (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, and some common colors are reserved in the color class, as shown in table 11-3. Some commonly used methods of the JFrame class are shown in the following table.
Common colors defined in a color class
Some common methods of JFrame class