GridLayout Instance Code (Notepad)

Source: Internet
Author: User

Import java.awt.*;
Import javax.swing.*;

public class Gridbagdemo extends JFrame {
public static void Main (String args[]) {
Gridbagdemo demo = new Gridbagdemo ();
}

Public Gridbagdemo () {
init ();
this.setsize (600,600);
this.setvisible (true);
}
Public void Init () {
J1 = new JButton ("open");
J2 = new JButton ("Save");
J3 = new JButton ("Save As");
J4 = new JPanel ();
string[] str = {"Java notes", "C # Notes", "HTML5 Notes"};
J5 = new JComboBox (str);
J6 = new JTextField ();
J7 = new JButton ("Empty");
J8 = new JList (str);
J9 = new JTextArea ();
J9.setbackground (Color.pink);//To see the effect, set the color
gridbaglayout layout = new GridBagLayout ();
this.setlayout (layout);
This.add (J1);//Add components into the JFrame
This.add (J2);
This.add (J3);
This.add (J4);
This.add (J5);
This.add (J6);
This.add (J7);
This.add (J8);
This.add (J9);
gridbagconstraints s= New gridbagconstraints ();//define a gridbagconstraints,
//is used to control the display position of the added component
S.fill = Gridbagconstraints.both;
//This method is to set the display if the area of the component is larger than the component itself
//none: The component size is not resized.
//horizontal: Widens the component so that it fills its display area horizontally, but does not change the height.
//vertical: The heightening component, which fills its display area vertically, but does not change the width.
//both: Causes the component to fully fill its display area.
s.gridwidth=1;//The method is to set the number of squares occupied by the component level, if 0, it means that the component is the last of the row
s.weightx = 0;//The method sets the horizontal stretch amplitude of the component, if 0 means no stretch, not 0 stretches as the window grows, between 0 and 1
s.weighty=0;//This method sets the vertical stretch amplitude of the component, if 0 means no stretch, not 0 stretches as the window grows, between 0 and 1
layout.setconstraints (J1, s);//Set up components
s.gridwidth=1;
s.weightx = 0;
s.weighty=0;
layout.setconstraints (J2, s);
s.gridwidth=1;
s.weightx = 0;
s.weighty=0;
layout.setconstraints (J3, s);
s.gridwidth=0;//The method is to set the number of squares occupied by the component level, if 0, it means that the component is the last of the row
S.WEIGHTX = 0;//cannot be 4 of a 1,j4, and can be stretched horizontally,
//But if 1, the column of the subsequent row will also be stretched, causing the column where the J7 is to be stretched
//So it should be stretched along with J6
s.weighty=0;
layout.setconstraints (J4, s)
; s.gridwidth=2;
s.weightx = 0;
s.weighty=0;
layout.setconstraints (J5, s);
; s.gridwidth=4;
s.weightx = 1;
s.weighty=0;
layout.setconstraints (J6, s);
; s.gridwidth=0;
s.weightx = 0;
s.weighty=0;
layout.setconstraints (J7, s);
; s.gridwidth=2;
s.weightx = 0;
S.weighty=1;
layout.setconstraints (J8, s);
; s.gridwidth=5;
s.weightx = 0;
S.weighty=1;
layout.setconstraints (J9, s);
}
JButton J1;
JButton J2;
JButton J3;
JPanel J4;
JComboBox J5;
JTextField J6;
JButton J7;
JList J8;
JTextArea J9;
}

GridLayout Instance Code (Notepad)

Related Article

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.