使用java swings使用gridbaglayout增加列的大小

时间:2015-11-26 06:50:49

标签: java swing

我创建了一个由3列组成的程序。现在我只希望第二列增加它的大小。有谁能建议我正确的代码?这是我的代码

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class PizzaGridBagLayout extends JFrame {
  public static void main(String[] args) {
    new PizzaGridBagLayout();
  }

  JTextField name = new JTextField(10), phone = new JTextField(10), pup=new JTextField(10);JComboBox address = new JComboBox(new String[]{"ComboBox 1","hi","hello"});
  JComboBox address1 = new JComboBox(new String[]{"ComboBox 2","hi","hello"});
  JButton labels=new JButton("labels");
  JLabel label1 = new JLabel("label1"); 
          JLabel  label2 = new JLabel("label2");
                  JLabel label3 = new JLabel("Label3");
                  JLabel label4 = new JLabel("Label4");
                  JLabel label5 = new JLabel("Label5");


    JTextArea area=new JTextArea(1,10);
    JTextArea area1=new JTextArea(1,10);    

    JRadioButton  yes = new JRadioButton("yes"),
      no = new JRadioButton("no");

  JCheckBox box1 = new JCheckBox("box1"), box2 = new JCheckBox("box2"),
      box3 = new JCheckBox("box3");


  JButton okButton = new JButton("OK"), closeButton = new JButton("Close");

  public PizzaGridBagLayout() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 // HERE I WANT TO INCREASE THE SIZE OF 2nd COLUMN CONSISTING OF     NAME,PHONE,ADDRESS,ADDRESS1,OKBUTTON //

    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());
    addItem(panel1, name, 1, 0,1);
    addItem(panel1, phone, 1, 2,1);
    addItem(panel1, address, 1, 1,1);
    addItem(panel1, address1, 1, 4,1);
    addItem(panel1, okButton, 1, 5,1);

   Box sizeBox = Box.createVerticalBox();
    addItem(panel1, label1, 0, 0,1);
    addItem(panel1, label2, 0, 1,1);
    addItem(panel1, label3, 0, 2,1);
    addItem(panel1, label4, 0, 3,1);
    addItem(panel1, label5, 0, 4,1);




   Box styleBox = Box.createHorizontalBox();
   styleBox.add(yes);
   styleBox.add(no);
   styleBox.setBorder(BorderFactory.createTitledBorder(""));
    addItem(panel1, styleBox, 1, 3,1);


    Box topBox = Box.createVerticalBox();
    ButtonGroup topGroup = new ButtonGroup();
    topBox.add(box1);
    topBox.add(box2);

    topBox.setBorder(BorderFactory.createTitledBorder(""));
    addItem(panel1, topBox, 2, 0,2);
    addItem(panel1, pup,2,2,1 );


    addItem(panel1, area, 2, 4,1);  
    addItem(panel1,closeButton,2,5,1);

    this.add(panel1);
    this.pack();
    this.setVisible(true);
  }


  private void addItem(JPanel p, JComponent c, int x, int y,int height) {
    GridBagConstraints gc = new GridBagConstraints();
    gc.gridx = x;
    gc.gridy = y;
    gc.gridheight=height;
    gc.weightx = 1.0;
    gc.weighty = 1.0;
    gc.insets = new Insets(2, 2, 2, 2);

    gc.fill = GridBagConstraints.BOTH;
    p.add(c, gc);
  }
  private void addItem1(JPanel p, JComponent c, int x, int y, int z, int a){
      GridBagConstraints gc1 = new GridBagConstraints();
        gc1.gridwidth=z;
        gc1.gridheight=a;
        gc1.fill=GridBagConstraints.NONE;
        p.add(c,gc1);



        }
}

This is the output I'm getting

如果我放大第1栏正在扩大但我希望第2栏扩大。任何人都可以建议我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

gc.weightx = 1.0;

weightx约束控制了这一点。您将所有列的值设置为1.0,以便每列获得额外的空间。

您希望值为:

    对于列0,2和,
  1. 0.0 第1栏
  2. 1.0。
  3. 阅读How to Use GridBagLayout上Swing教程中的部分,了解有关约束的更多信息。