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