Java GUI Java Framework window basics

Source: Internet
Author: User

Windows are the basis for GUI changes, and the visual components of applications that are small applications or graphical interfaces are placed in Windows. In the GUI, a window is a part of the user's screen that acts as a small screen in the screen. There are three types of windows:
①applet window: Applet class manages this window, which is created and processed by the system when the application is started.
② frame window (JFrame): This is the usual window, it supports the frame around the window, the title bar and the Minimize maximize and close buttons.
③ 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:

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

Other common methods of the JFrame class:

setBounds(int x,int y,int width,int height);//x y指定窗口出现在屏幕的位置,width和height指定长宽(像素)。setSize(int width,int height);setBackground(Color c);setVisible(boolean b);//窗口是否可见pack();用紧凑方式显示窗口。setTitle(String name);//设置窗口的名字getTitle();//获取窗口的名字setResiable(boolean m);//设置当前窗口是否可调整大小

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:
① uses the Getcontentpane () method to get the content panel, and then joins the component.
② creates an intermediate container for a JPanel object, adds the component to the container, and then uses Setcontentpane () to place the container as a content panel.

//一个用JFrame类创建窗口的Java应用程序,窗口只有一个按钮import java.swing.*;publicclass Example {    publicstaticvoidmain(String []args) {        new JFrame("My first window");        mw.setSize(250,200);        new JButton("My first button");        mw.getContentPane().add(button);        mw.setVisible(true);    }}

Tips: When writing GUI programs with swing, you typically do not create window objects directly with JFrame, but with JFrame-derived subclasses. You can include specific requirements and special content for a window in a subclass.

//define JFrame derived subclass Mywindowdemo Create JFrame window. The method of constructing a class Mywindowdemo has five parameters: the title name, the component to be placed, 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 Java.awt.*;import javax.swing.*; Public  class Test {     Public StaticMywindowdemo MW1; Public StaticMywindowdemo MW2; Public Static voidMain (String []args) {JButton butt1 =NewJButton ("button"); String name1 ="My first window"; String name2 ="My second window"; MW1 =NewMywindowdemo (Name1,butt1,color.blue, -, the); Mw1.setvisible (true); JButton BUTT2 =NewJButton ("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 Java Framework window basics

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.