下午好!
我有两种方法可以创建5行,每行是一个标签,按钮组上有五个单选按钮:
private JPanel criarPainelAvaliacoes() {
JPanel painel = new JPanel();
BoxLayout box = new BoxLayout(painel, BoxLayout.Y_AXIS);
painel.setLayout(box);
painel.add(criarPainelAvaliacoes("text1"));
painel.add(criarPainelAvaliacoes("text2"));
painel.add(criarPainelAvaliacoes("text3"));
painel.add(criarPainelAvaliacoes("text4"));
painel.add(criarPainelAvaliacoes("text5"));
return painel;
}
private JPanel criarPainelAvaliacoes(String texto) {
JPanel avaliacoes = new JPanel();
avaliacoes.setLayout(new FlowLayout());
ButtonGroup btg1 = new ButtonGroup();
JRadioButton rd0 = new JRadioButton("0");
JRadioButton rd1 = new JRadioButton("1");
JRadioButton rd2 = new JRadioButton("2");
JRadioButton rd3 = new JRadioButton("3");
JRadioButton rd4 = new JRadioButton("4");
JRadioButton rd5 = new JRadioButton("5");
btg1.add(rd0);
btg1.add(rd1);
btg1.add(rd2);
btg1.add(rd3);
btg1.add(rd4);
btg1.add(rd5);
avaliacoes.add(new JLabel(texto));
avaliacoes.add(rd0);
avaliacoes.add(rd1);
avaliacoes.add(rd2);
avaliacoes.add(rd3);
avaliacoes.add(rd4);
avaliacoes.add(rd5);
return avaliacoes;
}
我的问题是这样RadioButtons没有正确对齐。我怎么能这样做才能水平对齐?我已经尝试过使用gridbag和constraints.gridx以及gridy但没有成功。
提前致谢