Windows are the basis of gui programming,The visual components of an applet or graphical application are placed in a window,In gui, the window is part of the user's screen,Plays the role of a small screen in the screen.There are three types of windows:

applet window:the applet class manages this window,When the application program starts,Created and processed by the system;

Frame window (jframe):This is a window in the usual sense.It supports frames around the window, title bar, and minimize, maximize, and close buttons;

A borderless window (jwindow):no title bar,No frame,Just an empty rectangle.

Objects created using the jframe class in swing or its subclasses are jframe windows.

The main constructor of the jframe class:

jframe ():create an untitled window object; jframe (string s):Creates a window object with a title name of string s.

Other common methods of the jframe class:

setbounds (int x, int y, int width, int height):The parameters x, y specify the position where the window appears on the screen;The parameters width and height specify the width and height of the window.The unit is pixels. setsize (int width, int height):set the size of the window,The parameters width and height specify the width and height of the window,The unit is pixels. setbackground (color c):Set the background color of the window with parameter c. setvisible (boolean b):parameter b sets the window to be visible or invisible.The jframe is invisible by default. pack ():Display the window in a compact way.If this method is not used,The components in the window may not be visible when the window initially appears,When the user resizes the window,It may be possible to see these components. settitle (string name):Set the name of the window with the parameter name. gettitle ():Get the name of the window. setresiable (boolean m):Set whether the current window is resizable (default resizable).

Containers in swing can add components,Except for jpanel and its subclasses (japplet), other swing containers are not allowed to add components directly.There are two ways to add components to other containers:

One is to get the content panel using the getcontentpane () method.Add the components.For example, the code in the program of Example 5.1:

 mw.getcontentpane (). add (button);

The significance of this code is to get the content panel of the container,And add the button button to this content panel.

The other is to create an intermediate container for jpanel objects,Add components to this container,Use setcontentpane () to set this container as the content panel.For example, the code:

 jpanel contentpane=new jpanel ();
  mw.setcontentpane (contentpane);

The above code makes the contentpane the content panel.

[Example] A java application that uses the jframe class to create a window.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 writing a gui program with swing, usually do not directly create a window object with jframeAnd create a window object with a subclass derived from jframe,In the subclasses, you can add specific requirements and special content for windows.

[Example] Define the jframe derived subclass mywindowdemo to create a jframe window. The constructor of the class mywindowdemo has five parameters:the title name of the window,Add window components,The background color of the window and the height and width of the window.In the main method,Create two similar windows using the class mywindowdemo.

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. Some commonly used colors are reserved in the color class.See Table 11-3. See the table below for some common methods of the jframe class.

Common colors defined in the color class

Some common methods of the jframe class

  • Previous php survey results statistics
  • Next Multi-level menu effect with horizontal slideout in JavaScript