Вопрос

I'm trying to make some radio buttons appear the same way the normal buttons appear,but it seems like what I used for the normal buttons isn't working for the radio buttons.Graduate Student and Phd Student should appear under the Add new student but they appear as below,how can I make them a single nice column?

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);
        }
        }
Это было полезно?

Решение

You forgot to pass the gridbag constraints when adding the radio buttons:

p.add(jAdd2);

should be

p.add(jAdd2, gbc);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top