gpt4 book ai didi

java - GridLayout 列数错误

转载 作者:搜寻专家 更新时间:2023-11-01 01:27:06 26 4
gpt4 key购买 nike

我正在尝试使用 GridLayout(7,2) 创建面板

membersPanel = new JPanel(new GridLayout(7,2));

但是,当我添加组件(标签、组合框、文本字段等)时,组件显示在 3 列中,如下所示:

Image

我尝试将列数更改为 1 甚至 0,但面板保持不变。我能做什么?

编辑:

下面是更多代码:

 p1 = new JPanel();
membersPanel = new JPanel(new GridLayout(7,0));

resourcesLabel = new JLabel("Resources");
membersPanel.add(resourcesLabel);

emptyLabel5 = new JLabel("");
membersPanel.add(emptyLabel5);

emptyLabel6 = new JLabel("");
membersPanel.add(emptyLabel6);

comboBoxResource = new JComboBox(configs.XMLreaderDOM4J.readResourceID());
membersPanel.add(comboBoxResource);

slider1 = new SliderWithTextField(1,10);
textSli1 = new TextFieldFromSlider(this, slider1);
slider1.setTextField(textSli1);

slider1.setValue(1);
membersPanel.add(slider1);

membersPanel.add(textSli1);

emptyLabel2 = new JLabel();
membersPanel.add(emptyLabel2);

addButton1 = new JButton("Add");
addButton1.addActionListener(new TrataEvento());
membersPanel.add(addButton1);



agregator1Label = new JLabel("Agretagor1");
membersPanel.add(agregator1Label);

comboBoxAgregator1 = new JComboBox(configs.XMLreaderDOM4J.readAgregator1ID());
membersPanel.add(comboBoxAgregator1);

slider2 = new SliderWithTextField(1,10);
textSli2 = new TextFieldFromSlider(this, slider2);
slider2.setTextField(textSli2);

slider2.setValue(1);
membersPanel.add(slider2);

membersPanel.add(textSli2);
addButton2 = new JButton("Add");
addButton2.addActionListener(new TrataEvento());
membersPanel.add(addButton2);

emptyLabel3 = new JLabel();
membersPanel.add(emptyLabel3);


agregator0Label = new JLabel("Agregator0");
membersPanel.add(agregator0Label);

comboBoxAgregator0 = new JComboBox(configs.XMLreaderDOM4J.readAgregator0ID());
membersPanel.add(comboBoxAgregator0);

slider3 = new SliderWithTextField(1,10);
textSli3 = new TextFieldFromSlider(this, slider3);
slider3.setTextField(textSli3);

slider3.setValue(1);
membersPanel.add(slider3);

membersPanel.add(textSli3);

addButton3 = new JButton("Add");
addButton3.addActionListener(new TrataEvento());
membersPanel.add(addButton3);

emptyLabel4 = new JLabel();
membersPanel.add(emptyLabel4);

p1.add(membersPanel);

最佳答案

如果添加的组件数量超过指定的原始数量,Swing 会调整用于 GridLayout 的列数。使用 0 指定可调整的行数:

membersPanel = new JPanel(new GridLayout(0, 2));

这将使任何 future 的重构变得更加容易。

关于java - GridLayout 列数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17220515/

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