歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java使用網格組布局管理器

Java使用網格組布局管理器:

  1. package com.han;  
  2.   
  3. import java.awt.Container;  
  4. import java.awt.GridBagConstraints;  
  5. import java.awt.GridBagLayout;  
  6. import java.awt.Insets;  
  7.   
  8. import javax.swing.JButton;  
  9. import javax.swing.JFrame;  
  10.   
  11. /** 
  12.  * 使用網格組布局管理器 
  13.  * @author HAN 
  14.  * 
  15.  */  
  16. public class GridBagLayout_1 extends JFrame {  
  17.   
  18.     /** 
  19.      *  
  20.      */  
  21.     private static final long serialVersionUID = 5558640733909970067L;  
  22.   
  23.     public GridBagLayout_1() {  
  24.         // TODO Auto-generated constructor stub   
  25.         Container container = getContentPane();  
  26. //      container.setLayout(new GridBagLayout());   
  27.         GridBagLayout gridBagLayout = new GridBagLayout();  
  28.         container.setLayout(gridBagLayout);  
  29.         JButton button = new JButton("A");  
  30.         GridBagConstraints gridBagConstraints = new GridBagConstraints();  
  31.         gridBagConstraints.gridx = 0;  
  32.         gridBagConstraints.gridy = 0;  
  33.         gridBagConstraints.weightx = 10;  
  34.         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;  
  35.         container.add(button, gridBagConstraints);  
  36.         JButton button_1 = new JButton("B");  
  37.         GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();  
  38.         gridBagConstraints_1.gridx = 1;  
  39.         gridBagConstraints_1.gridy = 0;  
  40.         gridBagConstraints_1.insets = new Insets(0500);  
  41.         gridBagConstraints_1.weightx = 20;  
  42.         gridBagConstraints_1.fill = GridBagConstraints.HORIZONTAL;  
  43.         container.add(button_1, gridBagConstraints_1);  
  44.         JButton button_2 = new JButton("C");  
  45.         GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();  
  46.         gridBagConstraints_2.gridx = 2;  
  47.         gridBagConstraints_2.gridy = 0;  
  48.         gridBagConstraints_2.gridheight = 2;  
  49.         gridBagConstraints_2.insets = new Insets(0500);  
  50.         gridBagConstraints_2.weightx = 30;  
  51.         gridBagConstraints_2.fill = GridBagConstraints.BOTH;  
  52.         container.add(button_2, gridBagConstraints_2);  
  53.         JButton button_3 = new JButton("D");  
  54.         GridBagConstraints gridBagConstraints_3 = new GridBagConstraints();  
  55.         gridBagConstraints_3.gridx = 3;  
  56.         gridBagConstraints_3.gridy = 0;  
  57.         gridBagConstraints_3.gridheight = 4;  
  58. //      gridBagConstraints_3.insets = new Insets(0, 5, 0, 5);   
  59.         gridBagConstraints_3.insets = new Insets(0555);  
  60.         gridBagConstraints_3.weightx = 40;  
  61. //      gridBagConstraints_3.fill = GridBagConstraints.BOTH;   
  62.         gridBagConstraints_3.fill = GridBagConstraints.VERTICAL;  
  63.         container.add(button_3, gridBagConstraints_3);  
  64.         JButton button_4 = new JButton("E");  
  65.         GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();  
  66.         gridBagConstraints_4.gridx = 0;  
  67.         gridBagConstraints_4.gridy = 1;  
  68.         gridBagConstraints_4.gridwidth = 2;  
  69.         gridBagConstraints_4.insets = new Insets(5000);  
  70.         gridBagConstraints_4.fill = GridBagConstraints.HORIZONTAL;  
  71.         container.add(button_4, gridBagConstraints_4);  
  72.         JButton button_5 = new JButton("F");  
  73.         GridBagConstraints gridBagConstraints_5 = new GridBagConstraints();  
  74.         gridBagConstraints_5.gridx = 0;  
  75.         gridBagConstraints_5.gridy = 2;  
  76.         gridBagConstraints_5.gridwidth = 1;  
  77.         gridBagConstraints_5.insets = new Insets(5000);  
  78.         gridBagConstraints_5.fill = GridBagConstraints.HORIZONTAL;  
  79.         container.add(button_5, gridBagConstraints_5);  
  80.         JButton button_6 = new JButton("G");  
  81.         GridBagConstraints gridBagConstraints_6 = new GridBagConstraints();  
  82.         gridBagConstraints_6.gridx = 1;  
  83.         gridBagConstraints_6.gridy = 2;  
  84.         gridBagConstraints_6.gridwidth = 2;  
  85.         gridBagConstraints_6.gridheight = 2;  
  86.         gridBagConstraints_6.insets = new Insets(5500);  
  87.         gridBagConstraints_6.fill = GridBagConstraints.NONE;  
  88. //      gridBagConstraints_6.anchor = GridBagConstraints.EAST;   
  89.         gridBagConstraints_6.anchor = GridBagConstraints.NORTHEAST;  
  90.         gridBagConstraints_6.ipadx = 30;  
  91.         container.add(button_6, gridBagConstraints_6);  
  92.         JButton button_7 = new JButton("H");  
  93.         GridBagConstraints gridBagConstraints_7 = new GridBagConstraints();  
  94.         gridBagConstraints_7.gridx = 0;  
  95.         gridBagConstraints_7.gridy = 3;  
  96.         gridBagConstraints_7.insets = new Insets(5050);  
  97.         gridBagConstraints_7.fill = GridBagConstraints.BOTH;  
  98.         container.add(button_7, gridBagConstraints_7);  
  99.     }  
  100.   
  101.     /** 
  102.      * @param args 
  103.      */  
  104.     public static void main(String[] args) {  
  105.         // TODO Auto-generated method stub   
  106.         GridBagLayout_1 frame = new GridBagLayout_1();  
  107.         frame.setTitle("使用網格組布局管理器");  
  108.         frame.setVisible(true);  
  109.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  110.         frame.pack();  
  111.     }  
  112.   
  113. }  
Copyright © Linux教程網 All Rights Reserved