Home > Java > javaTutorial > GUI programming in Java

GUI programming in Java

巴扎黑
Release: 2017-07-23 13:46:04
Original
4308 people have browsed it

1. Definition

The full name of GUI is Graphical User Interface, that is, graphical user interface. JDK provides two packages, AWT and Swing, for the design and development of GUI programs.

1.java .awt abstract Window Toolkit (Abstract Window Toolkit), which is an early version of java, has limited types of components and needs to call local system methods to implement functions. It is heavyweight and somewhat platform-dependent.

2.javax.SWing is a graphical user interface system established by Sun Company on the basis of AWT. It provides more components and is completely implemented in Java, which enhances portability and is lightweight. magnitude.

SWing is equivalent to an upgraded version of AWT, which solves the problem of cross-platform operation. However, it does not mean that AWT has been completely eliminated. It is the foundation, and it is still important to understand its performance.

2. AWT class hierarchy

##Container: is the container, It is a special component into which other components can be added through the add method.

Simple code example:

import java.awt.Frame;public class Test19 {public static void main(String[] args) {
        demo();
    }static void demo() {
        Frame f = new Frame("这是我做的第一个窗口");
        f.setSize(400, 400);
        f.setLocation(40, 60);// 左上角的坐标System.out.println("運行成功");// 验证程序是否被执行了    }
}
Copy after login
If you do the above code, judging from the console results, the program has not hung up and has been executed. The desired dialog box just doesn't appear.

The above program is missing a piece of code:

f.setVisible(true);

import java.awt.Frame;public class Test19 {public static void main(String[] args) {
        demo();
    }static void demo() {
        Frame f = new Frame("这是我做的第一个窗口");
        f.setSize(400, 400);
        f.setLocation(40, 60);// 左上角的坐标        
        f.setVisible(true);//使得窗口可见        
        System.out.println("運行成功");// 验证程序是否被执行了    }
}
Copy after login
Execution results: (It is worth noting that clicking the X in the upper right corner of this small window will not close the window. We need to set it ourselves)

The following demonstrates the operation of adding a button:

import java.awt.Button;import java.awt.Frame;public class Test19 {public static void main(String[] args) {
        demo();
    }static void demo() {
        Frame f = new Frame("这是我做的第一个窗口");
        f.setSize(400, 400);
        f.setLocation(40, 60);// 左上角的坐标f.setVisible(true);// 使得窗口可见Button b = new Button("按钮");// 需要导包f.add(b);// 添加该按钮System.out.println("運行成功");// 验证程序是否被执行了    }
}
Copy after login
Execution results: (Note that if there is a button, the button will fill the border by default when the size and position are not set)

Summary:

Many GUI components can be divided into two major categories according to their functions, basic components and containers .

Basic components: buttons, text boxes, etc., cannot accommodate other components.

Container: can accommodate other components, such as windows, dialog boxes, etc. All containers are direct or indirect subclasses of java.awt.Container (the Frame above is a container)

Attachment, thoughts:

The above program ends when it is executed to System.out.println("Run successfully"); In fact, the main function ends at this time. But the window is still there. We can even do it. Then there must be other foreground threads running. It can be understood that when Frame is created, another thread is started.

The above is the detailed content of GUI programming in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template