Swing layout manager
The original source, author information, and statement of the document must be indicated in hyperlink form during reprinting. Otherwise, legal liability will be held. Http://zhangjunhd.blog.51cto.com/111083/128174when you choose to use jpaneland content pane of the container, you need to consider layout management. By default, JPanel initializes a FlowLayout, while content pane initializes a BorderLayout by default. The following describes several common layout managers: FlowLayout, BorderLayout, BoxLayout, CardLayout, GridLayout, and GridBagLayout.Code demoEach layout manager has a code demonstration, xxxLayoutDemo. java (see the attachment ). These files mainly consist of three methods: addComponentsToPane () provides layout logic (based on different layout manager and UI content ).
| Public Static VoidAddComponentsToPane (Container pane ){...} |
CreateAndShowGUI () instantiate a JFrame and load the layout logic content through its ContentPane.
| Private Static VoidCreateAndShowGUI () {// Create and set up the window. JFrame frame =NewJFrame ("FlowLayoutDemo"); frame. setdefaclocloseoperation (JFrame.EXIT_ON_CLOSE); // Set up the content pane.AddComponentsToPane(Frame. getContentPane (); // Display the window. frame. pack (); frame. setVisible (True);} |
Main () program entry, starts a separate thread and instantiates the UI.
| Public Static VoidMain (String [] args) {javax. swing. SwingUtilities.InvokeLater(NewRunnable (){Public VoidRun (){CreateAndShowGUI();}});} |
FlowLayoutThe FlowLayout class is the simplest layout manager. It arranges components in a similar way as arranging words on the page-from left to right, until there is no extra space, and then goes to the next line. Result: Content panel code:
| Public Static VoidAddComponentsToPane (Container pane) {pane. setLayout (NewFlowLayout (); pane. add (NewJButton ("Button 1"); pane. add (NewJButton ("Button 2"); pane. add (NewJButton ("Button 3"); pane. add (NewJButton ("Long-Named Button 4"); pane. add (NewJButton ("5 "));} |
BorderLayoutA BorderLayout object divides the interface into five areas, respectively using static constants of the BorderLayout class to specify:-PAGE_START-PAGE_END-LINE_START-LINE_END-CENTER effect: Content panel code:
| Public Static VoidAddComponentsToPane (Container pane) {JButton button =NewJButton ("Button 1 (PAGE_START)"); pane. add (button, BorderLayout.PAGE_START); Button =NewJButton ("Button 2 (CENTER)"); button. setPreferredSize (NewDimension (200,100); pane. add (button, BorderLayout.CENTER); Button =NewJButton ("Button 3 (LINE_START)"); pane. add (button, BorderLayout.LINE_START); Button =NewJButton ("Long-Named Button 4 (PAGE_END)"); pane. add (button, BorderLayout.PAGE_END); Button =NewJButton ("5 (LINE_END)"); pane. add (button, BorderLayout.LINE_END);} |
BoxLayoutBoxLayout can add components from top to bottom or from left to right to the current Panel. Result: Content panel code:
| Public Static VoidAddComponentsToPane (Container pane) {JPanel xPanel =NewJPanel (); xPanel. setLayout (NewBoxLayout (xPanel, BoxLayout.X_AXIS));AddButtons(XPanel); JPanel yPanel =NewJPanel (); yPanel. setLayout (NewBoxLayout (yPanel, BoxLayout.Y_AXIS));AddButtons(YPanel); pane. add (yPanel, BorderLayout.PAGE_START); Pane. add (xPanel, BorderLayout.PAGE_END);}Private Static VoidAddAButton (String text, Container container) {JButton button =NewJButton (text); button. setAlignmentX (Component.CENTER_ALIGNMENT); Container. add (button );}Private Static VoidAddButtons (Container container ){AddAButton("Button 1", container );AddAButton("Button 2", container );AddAButton("Button 3", container );AddAButton("Long-Named Button 4", container );AddAButton("5", container );} |
CardLayoutThe card layout is different from other la s because it hides some components. A card layout is a group of containers or components. Each container in a group is called a card. Result: Content panel code:
| Public VoidAddComponentToPane (Container pane ){FinalJPanel contentPanel =NewJPanel (); JPanel controlPanel =NewJPanel ();FinalCardLayout cardLayout =NewCardLayout (); pane. setLayout (NewBorderLayout (); pane. add (contentPanel, BorderLayout.CENTER); Pane. add (controlPanel, BorderLayout.PAGE_END); ControlPanel. setLayout (NewFlowLayout (); JButton [] B =NewJButton [10];For(IntI = 0; I <10; I ++) {B [I] =NewJButton ("No." + I); contentPanel. add (B [I]);} contentPanel. setLayout (cardLayout); JButton nextButton =NewJButton ("next"); nextButton. addActionListener (NewActionListener (){Public VoidActionreceivmed (ActionEvent e) {cardLayout. next (contentPanel) ;}}); controlPanel. add (nextButton );} |
GridLayoutGridLayout allows you to create a component table. When a component is added, it is sequentially left to right, from top to bottom filled with each grid, you cannot specify the lattice you want to put. The result is as follows: Content panel code:
| Public Static VoidAddComponentsToPane (Container pane) {JButton [] buttons =NewJButton [9]; pane. setLayout (NewGridLayout (3, 3 ));For(IntI = 0; I <buttons. length; I ++) {buttons [I] =NewJButton (I + ""); pane. add (buttons [I]) ;}} |
GridBagLayoutGridBagLayout is the most complex among all AWT layout managers, and its functions are also the most powerful. Like GridLayout, GridBagLayout manages components in a grid in a container. However, GridBagLayout is much more powerful. 1. All rows and columns managed by GridBagLayout can be of different sizes. 2. GridLayout limits each component to one cell, while GridBagLayout does not: the component can occupy any rectangular area in the container. GridBagLayout is usually restricted by a dedicated class, which is called GridBagConstraints. There are 11 Public member variables, and GridBagConstraints can control and manipulate these 11 aspects. The content is: 1. gridx-component horizontal coordinates; 2. girdy-component vertical coordinates; 3. gridwidth-component horizontal width, that is, the number of columns occupied by the component; 4. gridheight-vertical length of the component, that is, the number of lines occupied by the component; 5. weightx-indicates the weight of the row and shows the layout manager how to allocate additional horizontal space; 6. weighty indicates the weight of a column and tells the layout manager how to allocate additional vertical space. 7. anchor: this field is used when the component is smaller than the display area; 8. fill-if the display area is larger than the component area, it can be used to control the behavior of the component. Controls whether a widget is vertically filled or horizontally filled or both directions are filled. 9. insets-indicates the size of the blank area around the widget and the edge of the table space; 10. ipadx-horizontal spacing between components. The component width is the minimum width of the component plus the ipadx value. 11. ipady-Vertical spacing between components, the height of the component is the minimum height of the component plus the ipady value. Note: 1. gridx and gridy: in fact, they are set to the component columns. Note that they all start from 0. For example, if gridx is set to 0 and gridy is set to 1, they are placed in the column of 0 rows and 1; 2. gridwidth and gridheight: The default value is 1. GridBagConstraints. REMAINDER constant, which indicates that this component occupies all the remaining space for this row or the last component of this column. 3. weightx and weighty: when the window is larger, set the ratio of each component to a larger one. For example, if weightx = 0.5 of component A and weightx = 1 of component B, the remaining space will be allocated to component A and B at when the X axis of the window increases. 4. anchor: where to place a widget when the widget space is larger than the widget space. The options include CENTER (default), NORTH, NORTHEAST, EAST, SOUTHEAST, WEST, and NORTHWEST. 5. insets: sets the spacing between components. It has four parameters: Top, left, bottom, and right. The default value is (0, 0, 0 ). Result: Content panel code:
| Public Static VoidAddComponentsToPane (Container pane) {JButton button; pane. setLayout (NewGridBagLayout (); GridBagConstraints c =NewGridBagConstraints (); button =NewJButton ("Button 1"); c. fill = GridBagConstraints.HORIZONTAL; C. gridx = 0; c. gridy = 0; pane. add (button, c); button =NewJButton ("Button 2"); c. fill = GridBagConstraints.HORIZONTAL; C. weightx = 0.5; c. gridx = 1; c. gridy = 0; pane. add (button, c); button =NewJButton ("Button 3"); c. fill = GridBagConstraints.HORIZONTAL; C. weightx = 0.5; c. gridx = 2; c. gridy = 0; pane. add (button, c); button =NewJButton ("Long-Named Button 4"); c. fill = GridBagConstraints.HORIZONTALC. ipady = 40; // make this component tall c. weightx= 0.0; c. gridwidth = 3; c. gridx = 0; c. gridy = 1; pane. add (button, c); button =NewJButton ("5"); c. fill = GridBagConstraints.HORIZONTAL; C. ipady = 0; // reset to default c. weighty = 1.0; // request any extra vertical space c. anchor = GridBagConstraints.PAGE_END; // Bottom of space c. insets =NewInsets (10, 0, 0, 0); // top padding c. gridx = 1; // aligned with button 2 c. gridwidth = 2; // 2 columns wide c. gridy = 2; // third row pane. add (button, c );} |
A left and right choice box for GardBagLayout layout. For the code GridBagLayoutFrame. java, see the attachment. effect:
This article is from the "sub-blog", please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/128174