Java - Simple program with empty frame

In java, top-level windows (that is, windows that are not contained in other windows) are called frames.
In the AWT library there is a class called Frame that describes the top-level window. The Swing version of this class is named JFrame, and it extends the Frame class. A JFrame is one of the very few Swing components that doesn't just sit on the canvas, so its decorative parts (buttons, title bars, icons, etc.) are drawn by the user's windowing system, not by Swing.

Here is a simple program that displays an empty frame on the screen:

import java.awt.*;
import javax.swing.*;
/*
 * Swing类位于javax.swing包中,包名javax表示这是一个java扩展包
 * 而不是核心包。*/
public class SimpleFrameTest {
public static void main(String[] args)
{
    /*在每个Swing程序中,有两个技术问题需要强调
     * 首先,所有的Swing组件必须由事件分派线程进行配置
     * 线程将鼠标点击和按键控制转移到用户接口组件
     * 下面的代码片段是事件分派线程中的执行代码*/
    EventQueue.invokeLater(() ->
    {
        SimpleFrame frame = new SimpleFrame();
        /*在这里定义一个用户关闭这个框架时的相应动作,对于这个程序而言
         * 只让程序简单地退出即可,选择这个响应动作的语句是下面这句*/
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    });
}
}
class SimpleFrame extends JFrame
{
    /*构造器将框架大小设置为300 * 200像素
     * 这是SimpleFrame和JFrame之间唯一的差别*/
    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 200;
    public SimpleFrame()
    {
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    }

}

write picture description here

After the initialization statement ends, the main method exits
* Note that exiting main does not terminate
the program* only the main thread is terminated
* The event dispatch thread keeps the program active
* until the framework is closed or the System.exit method is called to terminate the program
*
* This program What is shown is just a very boring top-level window
* The title bar and outline decorations you see in this picture are drawn by the operating system, not the Swing library
* The Swing library is responsible for drawing everything inside the frame
* In this program , just filled the frame with the default background color*

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325769866&siteId=291194637