动作听众和按钮

时间:2014-02-02 10:45:32

标签: java swing actionlistener

我在JPanel中有一个按钮和3个单选按钮,但是单选按钮被隐藏了。当单击按钮时我们应该会看到它,我认为ActionListener会这样做。转发它没有,当点击按钮时,所有按钮都调整为自然大小。我做错了什么?

Expected Result Actual result 预期结果/实际结果

public class Menu extends JPanel implements ActionListener{
        static JPanel p =new JPanel(new GridBagLayout());
        public static void Butty(){
        JButton bAdd;
        JRadioButton jAdd1,jAdd2,jAdd3;
        ButtonGroup group = new ButtonGroup();    
        bAdd= new JButton("Add a new student");
        bAdd.setPreferredSize(new Dimension(200,30));
        jAdd1= new JRadioButton("Undergraduate Student");
        jAdd1.setPreferredSize(new Dimension(200,30));
        jAdd1.setSelected(true);
        group.add(jAdd1);

        jAdd2= new JRadioButton("Graduate Student");
        jAdd2.setPreferredSize(new Dimension(200,30));
        group.add(jAdd2);

        jAdd3= new JRadioButton("Phd Student");
        jAdd3.setPreferredSize(new Dimension(200,30));
        group.add(jAdd3);

        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(jAdd1,gbc);
        gbc.gridy++; 
        p.add(jAdd2,gbc);
        gbc.gridy++;
        p.add(jAdd3,gbc);
        gbc.gridy++;
        jAdd1.setVisible(false);
        jAdd2.setVisible(false);
        jAdd3.setVisible(false);

        bAdd.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    jAdd1.setVisible(true);
                    jAdd2.setVisible(true);
                    jAdd3.setVisible(true);
                    p.repaint();
                    p.revalidate();
                }
            });
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);
            }
 public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() { createAndShowGUI();}});
        }
    }

2 个答案:

答案 0 :(得分:1)

下面

public void actionPerformed(ActionEvent e)
        {
            jAdd1.setVisible(true);
            jAdd2.setVisible(true);
            jAdd3.setVisible(true);
        }

这样做:

public void actionPerformed(ActionEvent e)
        {
            jAdd1.setVisible(true);
            jAdd2.setVisible(true);
            jAdd3.setVisible(true);
            p.repaint();
            p.revalidate();
        }

答案 1 :(得分:0)

问题是我的窗口大小太多了。我增加了窗口的大小,按钮显示出来了。