search
HomeJavajavaTutorialGUI programming in Java

GUI programming in Java

Jul 23, 2017 pm 01:46 PM
javaBaseprogramming

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("運行成功");// 验证程序是否被执行了    }
}
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("運行成功");// 验证程序是否被执行了    }
}
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("運行成功");// 验证程序是否被执行了    }
}
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!

Statement
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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool