gpt4 book ai didi

java - 网格布局不起作用

转载 作者:行者123 更新时间:2023-11-30 02:46:11 25 4
gpt4 key购买 nike

这是我的代码:

PresidentVote(){
setName("PresCandidate");
setBorder(BorderFactory.createTitledBorder("Candidates for President"));

for(int row=0; row<PresidentTable.tblNatPresident.getRowCount(); row++){
setLayout(new GridLayout(row, 2));
String name=PresidentTable.tblNatPresident.getValueAt(row, 0).toString();

JLabel lblName=new JLabel(name);
JRadioButton radioVote=new JRadioButton();
lblName.setBorder(BorderFactory.createLineBorder(Color.RED));

add(lblName); add(radioVote);
}
}

但是出现的看起来像这样:

enter image description here

我正在努力实现这样的目标:

name | radiobutton

name | radiobutton

行数取决于 jtable 的行数。并且只有两列。

我真的不知道我的意思是我违反了代码中的某些内容吗?或者我应该放些什么才能让它正常工作?请帮助非常感谢:)

最佳答案

对于初学者,您需要在循环之前移动 setLayout,因为它只是无缘无故地在每次迭代中覆盖它,因此您需要将 y 值更改为行数。其次更改网格布局中的交换值,使其看起来像这样......

setLayout(new GridLayout(2, PresidentTable.tblNatPresident.getRowCount()));

关于java - 网格布局不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141838/

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