Java graphical interface Design--GridBagLayout of layout manager

Source: Internet
Author: User

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

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.