GridBagLayout takes care of multiple lines of effect

Source: Internet
Author: User

If you want the effect of button 2 to be two lines , you must press 3, Button 4 is present and visible at the same time.

    • If button 4 is missing, Button 2 does not occupy two lines;
    • If button 3, 4 is missing, then button 2 does not take up two lines.

1  PackageCOM.WST.BJ;2 3 Importjava.awt.GridBagConstraints;4 Importjava.awt.GridBagLayout;5 6 ImportJavax.swing.JButton;7 ImportJavax.swing.JFrame;8 9 classGridbagtext {Ten       OneJFrame JFrame =NewJFrame ("Search"); A  -JButton JButton =NewJButton ("button 1"); -JButton JButton2 =NewJButton ("button 2"); theJButton JButton3 =NewJButton ("button 3"); -JButton JButton4 =NewJButton ("button 4"); -  -      PublicGridbagtext () { +  - init (); +     } A      at     Private voidInit () -     { -Frameutil.initfram (JFrame, 500, 700); -          -GridBagLayout GridBagLayout =Newgridbaglayout (); - jframe.setlayout (gridbaglayout); ingridbagconstraints constraints =Newgridbagconstraints (); -          toConstraints.fill =Gridbagconstraints.both; +CONSTRAINTS.WEIGHTX = 1.0; -Constraints.weighty = 1.0; the          *          $Constraints.gridheight = 1;Panax NotoginsengConstraints.gridwidth =Gridbagconstraints.remainder; - gridbaglayout.setconstraints (JButton, constraints); the Jframe.add (JButton); +          AConstraints.gridheight = 2; theConstraints.gridwidth = 1; + gridbaglayout.setconstraints (JButton2, constraints); - Jframe.add (jButton2); $  $Constraints.gridheight = 1; -Constraints.gridwidth =Gridbagconstraints.remainder; - gridbaglayout.setconstraints (JButton3, constraints); the Jframe.add (jButton3); - gridbaglayout.setconstraints (JButton4, constraints);Wuyi Jframe.add (jButton4); the          -  Wu jframe.setsize (Jframe.getpreferredsize ()); -Jframe.setvisible (true); About //Jframe.pack (); $     } -}

1  PackageCOM.WST.BJ;2 3 Importjava.awt.GridBagConstraints;4 Importjava.awt.GridBagLayout;5 6 ImportJavax.swing.JButton;7 ImportJavax.swing.JFrame;8 9 classGridbagtext {Ten       OneJFrame JFrame =NewJFrame ("Search"); A  -JButton JButton =NewJButton ("button 1"); -JButton JButton2 =NewJButton ("button 2"); theJButton JButton3 =NewJButton ("button 3"); - //JButton jButton4 = new JButton ("button 4"); -  -      PublicGridbagtext () { +  - init (); +     } A      at     Private voidInit () -     { -Frameutil.initfram (JFrame, 500, 700); -          -GridBagLayout GridBagLayout =Newgridbaglayout (); - jframe.setlayout (gridbaglayout); ingridbagconstraints constraints =Newgridbagconstraints (); -          toConstraints.fill =Gridbagconstraints.both; +CONSTRAINTS.WEIGHTX = 1.0; -Constraints.weighty = 1.0; the          *          $Constraints.gridheight = 1;Panax NotoginsengConstraints.gridwidth =Gridbagconstraints.remainder; - gridbaglayout.setconstraints (JButton, constraints); the Jframe.add (JButton); +          AConstraints.gridheight = 2; theConstraints.gridwidth = 1; + gridbaglayout.setconstraints (JButton2, constraints); - Jframe.add (jButton2); $  $Constraints.gridheight = 1; -Constraints.gridwidth =Gridbagconstraints.remainder; - gridbaglayout.setconstraints (JButton3, constraints); the Jframe.add (jButton3); - //gridbaglayout.setconstraints (JButton4, constraints);Wuyi //Jframe.add (jButton4); the          -  Wu jframe.setsize (Jframe.getpreferredsize ()); -Jframe.setvisible (true); About //Jframe.pack (); $     } -}

1  PackageCOM.WST.BJ;2 3 Importjava.awt.GridBagConstraints;4 Importjava.awt.GridBagLayout;5 6 ImportJavax.swing.JButton;7 ImportJavax.swing.JFrame;8 9 classGridbagtext {Ten       OneJFrame JFrame =NewJFrame ("Search"); A  -JButton JButton =NewJButton ("button 1"); -JButton JButton2 =NewJButton ("button 2"); the //JButton JButton3 = new JButton ("button 3"); - //JButton jButton4 = new JButton ("button 4"); -  -      PublicGridbagtext () { +  - init (); +     } A      at     Private voidInit () -     { -Frameutil.initfram (JFrame, 500, 700); -          -GridBagLayout GridBagLayout =Newgridbaglayout (); - jframe.setlayout (gridbaglayout); ingridbagconstraints constraints =Newgridbagconstraints (); -          toConstraints.fill =Gridbagconstraints.both; +CONSTRAINTS.WEIGHTX = 1.0; -Constraints.weighty = 1.0; the          *          $Constraints.gridheight = 1;Panax NotoginsengConstraints.gridwidth =Gridbagconstraints.remainder; - gridbaglayout.setconstraints (JButton, constraints); the Jframe.add (JButton); +          AConstraints.gridheight = 2; theConstraints.gridwidth = 1; + gridbaglayout.setconstraints (JButton2, constraints); - Jframe.add (jButton2); $  $ //constraints.gridheight = 2; - //constraints.gridwidth = Gridbagconstraints.remainder; - //gridbaglayout.setconstraints (JButton3, constraints); the //Jframe.add (jButton3); - //gridbaglayout.setconstraints (JButton4, constraints);Wuyi //Jframe.add (jButton4); the          -  Wu jframe.setsize (Jframe.getpreferredsize ()); -Jframe.setvisible (true); About //Jframe.pack (); $     } -}

GridBagLayout takes care of multiple lines of effect

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.