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
import?java.awt.Frame;public?class?Test19?{public?static?void?main(String[]?args)?{ ????????demo(); ????}static?void?demo()?{ ????????Frame?f?=?new?Frame("這是我做的第一個(gè)窗口"); ????????f.setSize(400,?400); ????????f.setLocation(40,?60);//?左上角的坐標(biāo)System.out.println("運(yùn)行成功");//?驗(yàn)證程序是否被執(zhí)行了????} }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.
f.setVisible(true);
import?java.awt.Frame;public?class?Test19?{public?static?void?main(String[]?args)?{ ????????demo(); ????}static?void?demo()?{ ????????Frame?f?=?new?Frame("這是我做的第一個(gè)窗口"); ????????f.setSize(400,?400); ????????f.setLocation(40,?60);//?左上角的坐標(biāo)???????? ????????f.setVisible(true);//使得窗口可見(jiàn)???????? ????????System.out.println("運(yùn)行成功");//?驗(yàn)證程序是否被執(zhí)行了????} }
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("這是我做的第一個(gè)窗口"); ????????f.setSize(400,?400); ????????f.setLocation(40,?60);//?左上角的坐標(biāo)f.setVisible(true);//?使得窗口可見(jiàn)Button?b?=?new?Button("按鈕");//?需要導(dǎo)包f.add(b);//?添加該按鈕System.out.println("運(yùn)行成功");//?驗(yàn)證程序是否被執(zhí)行了????} }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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are three common methods to traverse Map in Java: 1. Use entrySet to obtain keys and values at the same time, which is suitable for most scenarios; 2. Use keySet or values to traverse keys or values respectively; 3. Use Java8's forEach to simplify the code structure. entrySet returns a Set set containing all key-value pairs, and each loop gets the Map.Entry object, suitable for frequent access to keys and values; if only keys or values are required, you can call keySet() or values() respectively, or you can get the value through map.get(key) when traversing the keys; Java 8 can use forEach((key,value)->

In Java, Comparable is used to define default sorting rules internally, and Comparator is used to define multiple sorting logic externally. 1.Comparable is an interface implemented by the class itself. It defines the natural order by rewriting the compareTo() method. It is suitable for classes with fixed and most commonly used sorting methods, such as String or Integer. 2. Comparator is an externally defined functional interface, implemented through the compare() method, suitable for situations where multiple sorting methods are required for the same class, the class source code cannot be modified, or the sorting logic is often changed. The difference between the two is that Comparable can only define a sorting logic and needs to modify the class itself, while Compar

To deal with character encoding problems in Java, the key is to clearly specify the encoding used at each step. 1. Always specify encoding when reading and writing text, use InputStreamReader and OutputStreamWriter and pass in an explicit character set to avoid relying on system default encoding. 2. Make sure both ends are consistent when processing strings on the network boundary, set the correct Content-Type header and explicitly specify the encoding with the library. 3. Use String.getBytes() and newString(byte[]) with caution, and always manually specify StandardCharsets.UTF_8 to avoid data corruption caused by platform differences. In short, by

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

InJava,thestatickeywordmeansamemberbelongstotheclassitself,nottoinstances.Staticvariablesaresharedacrossallinstancesandaccessedwithoutobjectcreation,usefulforglobaltrackingorconstants.Staticmethodsoperateattheclasslevel,cannotaccessnon-staticmembers,

HashMap implements key-value pair storage through hash tables in Java, and its core lies in quickly positioning data locations. 1. First use the hashCode() method of the key to generate a hash value and convert it into an array index through bit operations; 2. Different objects may generate the same hash value, resulting in conflicts. At this time, the node is mounted in the form of a linked list. After JDK8, the linked list is too long (default length 8) and it will be converted to a red and black tree to improve efficiency; 3. When using a custom class as a key, the equals() and hashCode() methods must be rewritten; 4. HashMap dynamically expands capacity. When the number of elements exceeds the capacity and multiplies by the load factor (default 0.75), expand and rehash; 5. HashMap is not thread-safe, and Concu should be used in multithreaded

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

ReentrantLock provides more flexible thread control in Java than synchronized. 1. It supports non-blocking acquisition locks (tryLock()), lock acquisition with timeout (tryLock(longtimeout, TimeUnitunit)) and interruptible wait locks; 2. Allows fair locks to avoid thread hunger; 3. Supports multiple condition variables to achieve a more refined wait/notification mechanism; 4. Need to manually release the lock, unlock() must be called in finally blocks to avoid resource leakage; 5. It is suitable for scenarios that require advanced synchronization control, such as custom synchronization tools or complex concurrent structures, but synchro is still recommended for simple mutual exclusion requirements.
