In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand the hierarchical structure of Java Swing". In daily operation, I believe many people have doubts about how to understand the hierarchical structure of Java Swing. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to understand the hierarchical structure of Java Swing". Next, please follow the editor to study!
The hierarchical structure of Java Swing what is Java Swing
Swing is a development kit for developing Java graphical interface applications. It is based on the Abstract window Toolkit (AWT:Abstract Window Toolkit) so that cross-platform applications can use any pluggable look and feel.
AWT controls are usually called heavyweight controls, while Swing controls are called lightweight controls.
Hierarchical structure of Swing components
Swing components can be divided into two types, one is the JComponent class and the other is the Window class. The difference between the two is that the JComponent component class mainly includes some components that can not be displayed independently (that is, it must rely on the intermediate container to display), while the Window component class mainly includes some components that can be displayed independently. In view of this, Swing components can be divided into three types: top-level components, intermediate components, and basic components, as shown in the following figure:
Top-level containers (i.e. Window components): JFrame, JApplet, JDialog, JWindow
Intermediate containers: JPanel, JScrollPane, JSplitOPane and JToolBar; can act as carriers, but they are also components that cannot be displayed independently and must be attached to the top-level container.
Special containers: middle layers that play a special role on GUI, such as JInternalFrame, JLayeredPane, etc.
Basic components: components that realize human-computer interaction, such as JButton, JComboBox, JList, JMenu, JTextField.
About three-tier components
1. When carrying out graphics programming, you need a container that can provide graphics drawing (that is, the top-level container). You can think of it as a window, which is the basis of graphic programming. all graphics must be included in the top-level container.
2. There are three components that can be used as top-level containers:
JFrame: an application similar to the form of a window in a Windows system
JDialog: similar to JFrame, used for design dialogs
JApplet: used to design Mini Program that can be embedded in web pages.
3. The Swing-based graphical interface must have at least one top-level container, each top-level container has a content panel, and menu components can be added to the top-level container.
The rest is to add some basic components to the content panel or menu components, which are relatively easy to use, of which JTree and JTable are more difficult.
Java Swing Foundation (hierarchy, components, layout Management) Swing Foundation hierarchy
Drawing environment and drawing objects
Coordinate
The upper-left coordinate of the GUI component defaults to (0pc0)
From the upper left corner to the lower right corner, horizontal coordinates x and vertical coordinates y increase
The units of coordinates are pixels
Graphics object
Specializing in managing the graphics environment, the Graphics class is an abstract class
The abstract class Graphics provides a platform-independent drawing interface
Java systems implemented on each platform will create a subclass of the Graphics class to implement the drawing function, which is transparent to programmers.
When the paint method is executed, the system passes a graphical object g that points to the Graphics subclass of a specific platform
A simple example.
Import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import javax.swing.JFrame;public class GraphicsTester extends JFrame {private static final long serialVersionUID = 1L; public GraphicsTester () {super ("Show fonts, colors, drawings"); setVisible (true); setSize (480250);} public void paint (Graphics g) {super.paint (g) G.setFont (new Font ("SansSerif", Font.BOLD,12)); g.setColor (Color.blue); g.drawString ("Font ScanSerif, Bold, 14, Red", 250 50);} public static void main (String [] args) {/ / TODO Auto-generated method stub GraphicsTester g = new GraphicsTester (); g.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);}}
The above program needs to pay attention to three pieces of code
Public class GraphicsTester extends JFrame
We need to inherit a main window to implement swing programming
Public void paint (Graphics g)
The Graphics g here does not need to be defined by ourselves, it is automatically generated by Java.
G.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)
Set the default action for closing the window
Note: the paint method will be called automatically
For other drawing functions, just look at the API document.
We used Graphics to draw, but we can actually draw with a more powerful Graphics2D, and of course, we need to transform the passed g up to Graphics2D, because the passed g is a subclass of Graphics2D.
Graphics2D g2d = (Graphics2D) gten swing Foundation
Swing: a package for booking various components. JFrame is a component of swing.
JFC and Swing
JFC (Java Foundation Classes)
Is a complete collection of GUI components and services
As an organic part of JAVA SE, it mainly consists of five parts
AWT
Java2D
Accessibility
Drag & Drop
Swing
Swing
Part of JFC
Provide all the components such as buttons, windows, tables, etc.
Pure Java components (components written entirely in Java)
AWT component
In the Java.awt package, including Button, Checkbox, Scrollbar, etc., are all subclasses of the Component class
Most of them contain native code, so they will show different appearance according to different operating system platform, but cannot be changed, so they are heavyweight components.
Swing component
Their names are all preceded by J before the original AWT component name, such as JButton, JCheckBox, JScrollbar and so on. They are all subclasses of the JComponent class.
Architecture on AWT is an extension of AWT, not a replacement
Written entirely in the Java language, its appearance and function do not depend on any code provided by the window system of the host platform, and is a lightweight component
Can provide a richer visual experience.
Application of Swing in Applet and Application
To apply Swing in Applet, you load the Swing component onto the Applet container (usually JApplet), which is usually done in the init method
To apply Swing in the Application (the local application, that is, the main function) is to load the Swing component into the top-level container of the Application (usually JFrame).
That is, if you want to use the Swing component, you have to add it to the container
Where did the container come from? What is the container?
Swing hierarchy
The inheritance hierarchy of components is as above, and most Swing components inherit from JComponent
Component class
Contains paint, repaint methods to draw components on the screen
Most GUI components extend Component directly or indirectly
Container class
Accommodate related components
Includes the add method to add components
Includes the setLayout method, which is used to set the layout and help the Container object to locate and size the components in it
JComponent class-superclass of most Swing components
Customizable look and feel, components can be customized according to requirements
Keyboard shortcuts (direct access to GUI components through the keyboard)
General event handling function
Component and Container hierarchy of Swing
Top container
Have to deal with the operating system, so they are all heavyweight components,
Every Java program that uses Swing components must have at least one top-level container, and other components must be placed on this top-level container to be displayed.)
JFrame implements a single main window
JDialog implements a second-level window
JApplet implements an applet display area in the browser window.
Intermediate container
To accommodate other components
Atomic component
Components that interact directly with the user
Example: three-tier container structure
Package swing;import javax.swing.*;import java.awt.*;import javax.swing.JFrame;public class ComponentTester {@ SuppressWarnings ("deprecation") public static void main (String [] args) {/ / TODO Auto-generated method stub JFrame.setDefaultLookAndFeelDecorated (true); JFrame frame = new JFrame ("Swing Frame"); Container contentPane=frame.getContentPane (); JPanel panel = new JPanel (); panel.setBorder (BorderFactory.createLineBorder (Color.black, 5)) Panel.setLayout (new GridLayout (2)); JLabel label = new JLabel ("Label", SwingConstants.CENTER); JButton button = new JButton ("Button"); panel.add (label); panel.add (button); contentPane.add (panel); frame.pack (); frame.show (); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);}}
Analyze the code of the key parts
First, you need a top-level container, and then you get a content panel Container from the container
JFrame.setDefaultLookAndFeelDecorated (true); JFrame frame = new JFrame ("Swing Frame"); Container contentPane=frame.getContentPane ()
Next, define an intermediate container and complete some initialization operations
JPanel panel = new JPanel (); panel.setBorder (BorderFactory.createLineBorder (Color.black, 5)); panel.setLayout (new GridLayout (2))
Define some atomic components
JLabel label = new JLabel ("Label", SwingConstants.CENTER); JButton button = new JButton ("Button")
Finally, add the atomic components to the middle-tier container, and then add the middle-tier container to the top-level container
Panel.add (label); panel.add (button); contentPane.add (panel)
That's the basic hierarchy of Swing components.
Layout management
Usage: just call the container's setLayout method
For example:
Container contentPane = frame.getContentPane (); ContentPane.setLayout (new FlowLayout ())
Where FlowLayout is the layout manager object
There are many commonly used layout manager classes, just look up the API document directly
At this point, the study on "how to understand the hierarchical structure of Java Swing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.