gpt4 book ai didi

java - 单选按钮位置

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

我试图让一些单选按钮以与普通按钮相同的方式出现,但我用于普通按钮的方法似乎不适用于单选按钮。研究生Phd Student 应该出现在 Add new student 下,但它们如下所示,我怎样才能使它们成为一个漂亮的列?

Menu

public class Menu extends JPanel implements ActionListener{
static JPanel p =new JPanel(new GridBagLayout());
public static void Butty(){
JButton bAdd, bDelete;
bAdd= new JButton("Add a new student");
bAdd.setPreferredSize(new Dimension(200,30));
bDelete= new JButton("Delete till geek");
bDelete.setPreferredSize(new Dimension(200,30));

JRadioButtonMenuItem jAdd1,jAdd2,jAdd3;
ButtonGroup group = new ButtonGroup();
jAdd2= new JRadioButtonMenuItem("Graduate Student");
jAdd2.setPreferredSize(new Dimension(200,30));
group.add(jAdd2);

jAdd3= new JRadioButtonMenuItem("Phd Student");
jAdd3.setPreferredSize(new Dimension(200,30));
group.add(jAdd3);
p.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
p.add(bAdd,gbc);
gbc.gridy++;

p.add(jAdd2);
gbc.gridy++;
p.add(jAdd3);
gbc.gridy++;


p.add(bDelete,gbc);
gbc.gridy++;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Something");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cp2=new JPanel(new BorderLayout());
JPanel main=new JPanel(new GridLayout(1, 0));
cp2.setOpaque(true);
Butty();
cp2.add(p);
main.add(cp2);
frame.setContentPane(main);
frame.pack();
frame.setVisible(true);
}
}

最佳答案

添加单选按钮时忘记传递 gridbag 约束:

p.add(jAdd2);

应该是

p.add(jAdd2, gbc);

关于java - 单选按钮位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21508770/

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