简体   繁体   English

GridLayout不起作用

[英]GridLayout doesn't work

here's my code: 这是我的代码:

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);
        }
    }

but the one that will appear looks like this: 但是将出现的内容如下所示:

在此处输入图片说明

and i'm trying to achieve like this: 我正在努力实现这样的目标:

name | 名称| radiobutton 单选按钮

name | 名称| radiobutton 单选按钮

and the number of rows will depend on how many rows the jtables was. 行数将取决于jtables的行数。 and only two columns. 只有两列。

i don't really know i mean did i violate something in the codes? 我真的不知道我的意思是我违反了守则中的某些规定吗? or is there something that i should put for it to properly work? 还是我应该放一些东西使其正常工作? please help thank you so much :) 请帮助非常感谢你:)

If you want the radio button and a label to be put together in a single cell, wrap them in a panel and put the panel into the cell. 如果要将单选按钮和标签放到一个单元格中,请将它们包裹在面板中,然后将面板放入单元格中。 Remember that the panel needs it's own layout (although the default flow layout may work for you) 请记住,面板需要自己的布局(尽管默认流程布局可能适合您)

As an alternative, I think setting the text (label?) of the radio button might be cleaner (although slightly less flexible). 作为替代方案,我认为设置单选按钮的文本(标签?)可能更简洁(尽管灵活性稍差)。

For starters you need to move your setLayout before the loop as it is just overriding it each iteration for no reason, therefore you need to change the y value to the amount of rows. 对于初学者,您需要将setLayout移到循环之前,因为它每次循环都会无缘无故地覆盖它,因此您需要将y值更改为行数。 Secondly change swap the values within the grid layout so it looks something like this... 其次,更改交换网格布局中的值,使其看起来像这样...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM