In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what Swing components are, and the editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The Swing package is part of JFC (Java Foundation Classes) and consists of many packages, as shown in the following table:
Com.sum.swing.plaf.motif user interface representative classes, which implement Motif interface style Com.sum.java.swing.plaf.windows user interface representative classes They implement Windows interface style Javax.swingSwing components and the border Javax.swing.colorchooserJcolorChooser that uses the tool Javax.swing.borderSwing lightweight components to support class / interface Javax.swing.event events and listener classes Javax.swing.filechooserJFileChooser support classes / interfaces Javax.swing.pending Swing components Javax.swing.plaf abstract classes that are not fully implemented Define the behavior represented by UI the base class Javax.swing.plaf.basic user interface representative class that implements the common functions of all standard interface styles They implement Metal interface styles Javax.swing.tableJtable components Javax.swing.text support for document display and editing Javax.swing.text.html support for display and editing of HTML documents parsers for Javax.swing.text.html.parserHtml documents Javax.swing.text.rtf support for display and editing of RTF files Javax.swing.undo support for Javax.swing.treeJtree components support cancel operation
(in jdk1.3, the *, the second and pending packages are gone, and the plaf.multi package is added. The main function is to add additional LawF to the default Lobf, for example, a MultiButtonUI instance can handle both MotifButtonUI and AudioButtonUI.)
The Swing package is a * * package provided by Swing, which contains nearly 100 classes and 25 interfaces. Almost all Swing components are in the Swing package, with the exception of JtableHeader and JtextComponent, which are in Swing.table and Swing.text, respectively.
Events and event listener classes are defined in the ◆ Swing.border package, similar to AWT's event package. They all include event classes and listener interfaces.
The ◆ Swing.pending package contains Swing components that are not fully implemented.
The ◆ Swing.table package mainly includes support classes for table building (JTable).
◆ Swing.tree is also a supporting class for JTree.
◆ Swing.text, Swing.text.html, Swing.text.html.parser, and Swing.text.rtf are all packages for displaying and editing documents.
Jcomponent is an abstract class that defines general methods for all subclass components, but not all Swing components inherit from the JComponent class, and the JComponent class inherits from the Container class, so any component of this class can be used as a container.
Functionally, components can be divided into:
1) Top-level containers: 4 JFrame,JApplet,JDialog,JWindow
2) Intermediate container: JPanel,JScrollPane,JSplitPane,JToolBar
3) Special container: the middle layer that plays a special role on GUI, such as JInternalFrame,JLayeredPane,JRootPane.
4) basic controls: components that implement interpersonal interaction, such as Jbutton, JComboBox, JList, JMenu, JSlider, JtextField.
5) display of non-editable information: components that display non-editable information to the user, such as JLabel, JProgressBar, ToolTip.
6) display of editable information: components that display formatted information that can be edited to the user, such as JColorChooser, JFileChoose, JFileChooser, Jtable, JtextArea.
Basic rules for using Swing
Unlike the AWT component, the Swing component cannot be added directly to the top-level container; it must be added to a content panel (content pane) associated with the Swing top-level container. The content panel is a common container contained in the top-level container, which is a lightweight component. The basic rules are as follows:
(1) place the Swing component on the content panel of a top-level Swing container
(2) avoid non-Swing heavyweight components.
There are two ways to add components to JFrame:
1) use the getContentPane () method to obtain the content panel of JFrame, and then add a component to it: frame.getContentPane (). Add (childComponent)
2) create an intermediate container such as Jpanel or JDesktopPane, add components to the container, and use the setContentPane () method to set the container as the content panel of JFrame:
Jpanel contentPane=new Jpanel ()
…… / / add other components to Jpanel
Frame.setContentPane (contentPane)
/ / set the contentPane object to the content panel of frame
Root panel
The root panel consists of a glass panel (glassPane), a content panel (contentPane), and an optional menu bar (JMenuBar), while the content panel and the selectable menu bar are placed in the same layer. The glass panel is completely transparent and the default value is invisible, making it easy to receive mouse events and draw on all components.
The methods provided by the root panel:
Container getContentPane (); / / get the content panel
SetContentPane (Container); / / set the content side
JMenuBar getMenuBar (); / / active menu bar
SetMenuBar (JMenuBar); / / set the menu bar
JLayeredPane getLayeredPane (); / / get the layered panel
SetLayeredPane (JLayeredPane); / / set the layered panel
Component getGlassPane (); / / get the glass panel
SetGlassPane (Component); / / set the glass panel
Hierarchical panel (JLayeredPane)
Swing provides two hierarchical panels: JlayeredPane and JDesktopPane. JDesktopPane is a subclass of JLayeredPane and is specifically set up to accommodate the internal framework (JInternalFrame). To add a component to a hierarchical board, you need to indicate which layer to add it to, and indicate the location of the component in that layer: add (Component c, Integer Layer, int position).
Panel (JPanel)
The JPanel is a lightweight container component that is used in the same way as Panel and is used to accommodate interface elements so that more components can be accommodated under the settings of the layout manager, enabling nesting of containers. Jpanel, JscrollPane, JsplitPane and JinteralFrame are commonly used intermediate containers and are lightweight components. The default layout manager for Jpanel is FlowLayout.
Scroll window (JScrollPane)
The JscrollPane is a panel with scroll bars, mainly by moving the JViewport (viewport). JViewport is a special object used to view the base component, and the scroll bar actually moves the viewport along the component, while depicting what it "sees" below.
Divider (JSplitPane)
JSplitPane provides splittable windows that support split horizontally and vertically with slider bars. The common methods are:
◆ addImpl (Component comp,Object constraints,int index) / / add specified components
◆ setTopComponent (Component comp) / / sets the components at the top
◆ setDividerSize (int newSize) / / sets the size of the split
◆ setUI (SplitPaneUI ui) / / set the look and feel
Palettes (JTabbedPane)
JTabbedPane provides a set of open keys with labels or icons for users to choose from. Common methods:
Add (String title,Component component) / / add a component with a specific label
AddChangeListener (ChangeListener l) / / palette registers a change listener
Toolbar (JToolBar)
JtoolBar is a container for displaying common tool controls. Users can drag and drop out of a separate window that displays tool controls.
The common methods are:
JToolBar (String name) / / Construction method
GetComponentIndex (Component c) / / returns the serial number of a component
GetComponentAtIndex (int I) / / get a component with a specified serial number
Internal Framework (JInternalFrame)
The internal framework JInternalFrame is like a window inside another window with the following characteristics:
1) the internal frame must be added to a container (usually JDesktopPane), otherwise it will not be displayed
2) there is no need to call the show () or setVisible () method. The internal frame is displayed with the container in which it is located.
3) the frame size must be set with setSize () or pack () or setBounds method, otherwise the size is zero and the frame cannot be displayed
4) you can use the setLocation () or setBounds () method to set the position of the internal frame in the container. The default value is 0 ~ 0, that is, the upper left corner of the container.
5) like the top-level JFrame, adding components to the internal framework should also be added to its content panel
6) create a dialog box in the internal frame. You can't use JDialog as the top-level window. You must use JOptionPane or JInternalFrame.
7) the internal framework cannot listen for window events, and you can handle the operation of the internal frame window by listening to the internal frame (JInternalFrameEvent) similar to the window event.
◆ JFrame frame=new JFrame ("InternalFrameDemo"); / / instantiate the window
◆ JDesktopPane desktop=new JDesktopPane (); / / instantiate container JDesktopPane
◆ MyInternalFrame myframe=new MyInternalFrame (); / / instantiate the internal window
◆ desktop.add (myframe); / / add internal windows to the container
◆ myframe.setSelected (true); / / Internal panel is optional
◆ frame.setContentPane (desktop); / / set desktop to the content panel of frame
This is the end of this article on "what are the Swing components?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.