Java know how much (97) 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.


Example 11-1 a Java application that creates a window with the JFrame class. The window has only one button.

1 Importjavax.swing.*;2  Public classexample5_1{3      Public Static voidMain (String args[]) {4JFrame MW =NewJFrame ("My first Window");5Mw.setsize (250,200);6JButton button =NewJButton ("I am a button");7 Mw.getcontentpane (). Add (button);8Mw.setvisible (true);9     }Ten}

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.


Example 11-2 defines a jframe derived subclass Mywindowdemo Create a jframe window. 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, the use of the class

1 Mywindowdemo creates two similar windows. 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 commonly 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 basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxesJava know how many (89) lists and combo boxesJava know how much (90) menuJava know how much (91) dialog boxJava know how much (92) scroll barJava know how much (93) mouse EventsJava know how much (94) keyboard EventsJava know how much (95) Drawing BasicsJava know how much (96) set the font and color of the drawing

Java know how much (97) 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.