Java GUI Framework 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:
①applet window
② frame window (JFrame)
③ a borderless window (JWindow)

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:

JFrame();//创建无标题的窗口对象;JFrame(String s);//创建一个标题名是字符串s的窗口对象。

Other common methods of the JFrame class:

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.  setSize (int  width,int  height); //sets the size of the window, the width and height of the parameters specify the window widths and heights, in pixels.  SetBackground (Color c); //sets the background color of the window with parameter C.  setvisible (boolean  b); //parameter B sets the window to be visible or invisible. JFrame is not visible by default.  settitle (String name); //sets the name of the window with the parameter name.  GetTitle (); //gets the name of the window. 

The containers in swing can add components, except for JPanel and its subclasses (JApplet), and other swing containers do not allow the 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 components. 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 a 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.

//一个用JFrame类创建窗口的Java应用程序。窗口只有一个按钮。import javax.swing.*;publicclass Test{    publicstaticvoidmain(String args[]){        new JFrame(“我的第一个窗口”);        mw.setSize(250,200);        new JButton(“我是一个按钮”);        mw.getContentPane().add(button);        mw.setVisible(true);    }}

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.

//define JFrame derived subclass Mywindowdemo Create 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, two similar windows are created using the class Mywindowdemo. Import Javax.swing.*;import Java.awt.*;import java.awt.event.*; Public  class Test{     Public StaticMywindowdemo MW1; Public StaticMywindowdemo MW2; Public Static voidMain (String args[]) {JButtonStaticBUTT1 =NewJButton ("I am a button");        String name1 = "my first window";        String name2 = "my second window"; MW1 =NewMywindowdemo (Name1,butt1,color.blue, -, the); Mw1.setvisible (true); JButton BUTT2 =NewJButton ("I am another button"); MW2 =NewMywindowdemo (Name2,butt2,color.magenta, -, -); Mw2.setvisible (true); }} class mywindowdemo extends JFrame{     PublicMywindowdemo (String Name,jbutton button,color C,intWinth) {Super();        Settitle (name);        SetSize (W,H);        Container con = Getcontentpane ();        Con.add (button);    Con.setbackground (c); }}

Java GUI Framework 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.