gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:53:36 26 4
gpt4 key购买 nike

我创建了一个包含 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 列展开。谁能建议我。提前致谢。

最佳答案

gc.weightx = 1.0;

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

您希望值是:

  1. 第 0、2 和 0.0
  2. 第 1 列为 1.0。

阅读 How to Use GridBagLayout 上的 Swing 教程部分有关约束的更多信息。

关于java - 使用 java swings 使用 gridbaglayout 增加列的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33932251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com