Use of JSeparator in the grid layout manager of JAVA and precautions for GridBagLayout

Source: Internet
Author: User

1. the following example shows how to add a split line to the grid layout panel: [java] GridBagConstraints grid4 = new GridBagConstraints (); grid4.gridx = 3; grid4.gridy = 0; grid4.fill = GridBagConstraints. VERTICAL; grid4.insets = new Insets (0, 5, 0, 5); JSeparator separator4 = new JSeparator (); // create a VERTICAL separator separator4.setOrientation (JSeparator. VERTICAL); [java] container. add (separator4, grid4); remove grid4.fill = GridBagConstraints. VERTICAL; the statement will Note that the split line cannot be displayed. Interested readers can try it out. 2. For the grid layout manager unit grid, when the components added to the unit grid are too large or too small, the grid will automatically adjust the size until they can accommodate the component. Of course, automatic adjustment will still ensure that the grid height of the same row is the same, and the grid width of the same column is the same. In addition, weightx. The default value of weighty is 0, that is, the blank panel area is not automatically expanded. The following code automatically expands the blank area when the weightx value of a column is 1 and the other columns are all 0 by default .. [Java] package Project; import java. awt. *; import java. util. date; import javax. swing. *; public class Table3 extends JFrame {public Table3 () {Container container = getContentPane (); container. setLayout (new GridBagLayout (); GridBagConstraints grid1 = new GridBagConstraints (); grid1.gridx = 0; grid1.gridy = 0; grid1.weightx = 1; // set all grids in this column to automatically expand grid1.insets = new Insets (0, 5, 0, 5) horizontally ); JLabel label1 = new JLabel ("no window opens"); GridBagConstraints grid2 = new GridBagConstraints (); grid2.gridx = 1; grid2.gridy = 0; grid2.fill = GridBagConstraints. VERTICAL; // remove this statement. The split line added to the Panel does not display grid2.insets = new Insets (0, 5, 0, 5); JSeparator separator2 = new JSeparator (); // create a vertical separator separator2.setOrientation (JSeparator. VERTICAL); GridBagConstraints grid3 = new GridBagConstraints (); grid3.gridx = 2; grid3.gridy = 0; // grid3.weighty = 1; // set all grids of the row to automatically expand grid3.insets = new Insets (0, 5, 0, 5) in the vertical direction; JLabel label3 = new JLabel ("OPERATOR :"); gridBagConstraints grid4 = new GridBagConstraints (); grid4.gridx = 3; grid4.gridy = 0; grid4.fill = GridBagConstraints. VERTICAL; grid4.insets = new Insets (0, 5, 0, 5); JSeparator separator4 = new JSeparator (); // create a VERTICAL separator separator4.setOrientation (JSeparator. VERTICAL); GridBagConstraints grid5 = new GridBagConstraints (); grid5.gridx = 4; grid5.gridy = 0; grid5.insets = new Insets (0, 5, 0, 5 ); date date = new Date (); JLabel label5 = new JLabel (String. format ("% tF", date); GridBagConstraints grid6 = new GridBagConstraints (); grid6.gridx = 5; grid6.gridy = 0; grid6.fill = GridBagConstraints. VERTICAL; grid6.insets = new Insets (0, 5, 0, 5); JSeparator separator6 = new JSeparator (); // create a VERTICAL separator separator6.setOrientation (JSeparator. VERTICAL); GridBagConstraints grid7 = new GridBagConstraints (); grid7.gridx = 6; grid7.gridy = 0; grid7.insets = new Insets (0, 5, 0, 5 ); JLabel label7 = new JLabel ("** Enterprise Inventory Management System"); container. add (label1, grid1); container. add (separator2, grid2); container. add (label3, grid3); container. add (separator4, grid4); container. add (label5, grid5); container. add (separator6, grid6); container. add (label7, grid7); setVisible (true); setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); setBounds (350,150,800,200);} public static void main (String [] args) {Table3 table = new Table3 ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.