Java know how much (81) frame window basics

Source: Internet
Author: User

Windows are the basis of GUI programming, the visual components of applications that are small applications or graphical interfaces are placed in Windows, and in the GUI, Windows are part of the user's screen and act as a small screen in the screen. The following three types of Windows are available:

    1. Applet window: The Applet class manages this window, which is created and processed by the system when the application program starts;
    2. frame window (JFrame): This is the usual window, it supports the frame around the window, the title bar, as well as the minimize, maximize and close buttons;
    3. 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 methods of the JFrame class are:

    1. JFrame (): Creates an untitled Window object;
    2. JFrame (String s): Creates a Window object with a header name that is the string s.


Other common methods of the JFrame class:

    1. SetBounds (int x,int y,int width,int height): parameter x, y specifies where the window appears on the screen, and the parameter width,height specifies the width and height of the window. Units are pixels.
    2. setSize (int width,int height): Sets the size of the window, parameter width and height specify the width and height of the window, in pixels.
    3. SetBackground (color C): Sets the background color of the window with parameter C.
    4. SetVisible (Boolean B): Parameter B Sets whether the window is visible or invisible. JFrame is not visible by default.
    5. Pack (): Displays the window in a compact manner. If you do not use this method, you may not see the components in the window when the window initially appears, and you may be able to see these components when the user resizes the window.
    6. Settitle (String name): Sets the name of the window with the parameter name.
    7. GetTitle (): Gets the name of the window.
    8. Setresiable (boolean m): Sets whether the current window is resizable (the default is resizable).


A container in swing can add components, except for JPanel and its subclasses (JApplet), and other swing containers do not allow components to be joined 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 the code is to get the contents panel of the Container and Add button buttons to the 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 a content panel. For example, code:
      JPanel ContentPane = new JPanel ();
      ...
      Mw.setcontentpane (ContentPane);
      The above code puts contentpane into the content panel.

1"Example 11-1"A Java application that creates windows with the JFrame class. The window has only one button. 2 Importjavax.swing.*;3  Public classexample5_1{4      Public Static voidMain (String args[]) {5JFrame MW =NewJFrame ("My first Window");6Mw.setsize (250,200);7JButton button =NewJButton ("I am a button");8 Mw.getcontentpane (). Add (button);9Mw.setvisible (true);Ten     } One}

When writing GUI programs with swing, it is not usually necessary to create a Window object directly with JFrame, but to create a Window object with a subclass derived from JFrame, and to include the specific requirements and special contents of the window in the subclass.

1"Example 11-2"defines jframe derived subclasses Mywindowdemo create jframe windows. The method of constructing a class Mywindowdemo 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, two similar windows are created using the class Mywindowdemo. 2 Importjavax.swing.*;3 Importjava.awt.*;4 Importjava.awt.event.*;5  Public classexample5_2{6      Public StaticMywindowdemo mw1;7      Public StaticMywindowdemo mw2;8      Public Static voidMain (String args[]) {9JButtonStaticBUTT1 =NewJButton ("I am a button");TenString name1 ="My first window"; OneString name2 ="My second window"; AMW1 =NewMywindowdemo (name1,butt1,color.blue,350,450); -Mw1.setvisible (true); -JButton BUTT2 =NewJButton ("I am another button"); theMW2 =NewMywindowdemo (name2,butt2,color.magenta,300,400); -Mw2.setvisible (true); -     } - } + classMywindowdemoextendsjframe{ -      PublicMywindowdemo (String Name,jbutton button,color C,intWinth) { +         Super(); A settitle (name); at 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 commonly used colors are scheduled in the Color class, see table 11-3. Some common methods of the JFrame class are described in table 11-4.

table 11-3 color common colors defined in the color class
Field Summary Color
Static Color Black/black Black
Static Color Blue/blue Blue
Static Color Cyan/cyan Cyan
Static Color Darkgray/dark_gray Dark grey
Static Color Gray/gray Grey
Static Color Green/green Green
Static Color Lightgray/lightgray Light Grey
Static Color Magenta/magenta Magenta
Static Color Orange/orange Orange Yellow
Static Color Pink/pink Pink
Static Color red/red Red
Static Color White/white White
Static Color Yellow/yellow Yellow

Table 11-4 Some common methods of JFrame class
Method meaning
JFrame () Construction method, creating a JFrame object
JFrame (String title) Create a JFrame object with title
Add () To add a window element to a window from a method inherited from a parent class
void Addwindowlistener (WindowListener ear) Registering the monitor to listen for events fired by the JFrame object
Container Getcontentpane () Returns the contents panel of the JFrame object
void SetBackground (Color c) Set the background color to C
void Setforeground (Color c) Set foreground color to C
void setSize (int w,int h) Sets the width of the window to W, height to H
Vid settitle (String title) Set the caption in the window
void SetVisible (Boolean b) Set window visibility, true visible, false invisible

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basics

Java know how much (81) frame window basics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.