Grid layout of java swing Programming

Source: Internet
Author: User

/*** Grid layout manager of java; * The grid layout manager divides a form into a table. You can specify rows and columns of the table when creating the grid layout manager. By default, * is added to the control in the form from left to right, from top to bottom, and each control is filled with the entire cell, that is, changing the size of the * Form widget also changes the size. * @ Author gao */package com. gao; import java. awt. gridLayout; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JPanel; import javax. swing. border. extends; public class extends JFrame {private JPanel contentPane; private JButton button1; private JButton button2; private JButton button3; private JButton button4; private JButton button5; private JButton button6; private JButton button7; private JButton button8; private JButton button9; public GridLayoutDemo () {contentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5); // you can specify the upper, left, lower, and right sides of the Panel. setContentPane (contentPane); contentPane. setLayout (new GridLayout (3, 3, 5); button1 = new JButton ("button 1"); button2 = new JButton ("button 2 "); button3 = new JButton ("button 3"); button4 = new JButton ("button 4"); button5 = new JButton ("button 5 "); button6 = new JButton ("button 6"); button7 = new JButton ("button 7"); button8 = new JButton ("button 8 "); button9 = new JButton ("button 9"); contentPane. add (button1); contentPane. add (button2); contentPane. add (button3); contentPane. add (button4); contentPane. add (button5); contentPane. add (button6); contentPane. add (button7); contentPane. add (button8); contentPane. add (button9); this. setTitle ("border layout"); this. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); this. setBounds (100,100,450,300); this. setVisible (true);} public static void main (String [] args) {GridLayoutDemo example = new GridLayoutDemo ();}}

Running result:



If you are interested, you can change the size.

Check the effect.

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.