The gridbaglayout does not change with the change of the window label, it can be fixed.
----------------------------------------------------------
Import Java.awt.Button;
Import Java.awt.Font;
Import java.awt.GridBagConstraints;
Import Java.awt.GridBagLayout;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
public class GridBagEx2 extends JPanel
{
Private static final long serialversionuid = -5214441555967215113l;
protected void Makebutton (String name, GridBagLayout gridbag,
Gridbagconstraints c)
{
Button button = New button (name);
Gridbag.setconstraints (button, c);
Add (button);
}
public void Init ()
{
GridBagLayout gridbag = new GridBagLayout ();
Gridbagconstraints C = new gridbagconstraints ();
SetFont (New Font ("Sansserif", Font.plain, 14));
SetLayout (Gridbag);
C.fill = Gridbagconstraints.both;
C.WEIGHTX = 1.0;
Makebutton ("Button1", Gridbag, C);
Makebutton ("Button2", Gridbag, C);
Makebutton ("Button3", Gridbag, C);
C.gridwidth = Gridbagconstraints.remainder; End row
Makebutton ("Button4", Gridbag, C);
c.weightx = 0.0; Reset to the default
Makebutton ("Button5", Gridbag, C); Another row
C.gridwidth = gridbagconstraints.relative; Next-to-last in row
Makebutton ("Button6", Gridbag, C);
C.gridwidth = Gridbagconstraints.remainder; End row
Makebutton ("Button7", Gridbag, C);
C.gridwidth = 1; Reset to the default
C.gridheight = 2;
C.weighty = 1.0;
Makebutton ("Button8", Gridbag, C);
C.weighty = 0.0; Reset to the default
C.gridwidth = Gridbagconstraints.remainder; End row
C.gridheight = 1; Reset to the default
Makebutton ("Button9", Gridbag, C);
Makebutton ("Button10", Gridbag, C);
SetSize (300, 100);
}
public static void Main (String args[])
{
JFrame f = new JFrame ("Gridbag Layout Example");
F.setlocation (400, 200);
GridBagEx2 Ex1 = new GridBagEx2 ();
Ex1.init ();
F.add ("Center", EX1);
F.pack ();
F.setsize (F.getpreferredsize ());
F.setvisible (TRUE);
F.addwindowlistener (New Windowadapter ()
{
@Override
public void windowclosing (WindowEvent e)
{
System.exit (0);
}
});
}
}
Java graphical interface Design--GridBagLayout of layout manager