为什么JButton不起作用?

时间:2013-05-25 22:53:54

标签: java swing jpanel jbutton

我正在创造游戏pacman,如果你被怪物杀死,它会带你到问题小组

所以我问了一些问题,我有JButton,它们是A,B,C等多选按钮......

但是当你点击按钮时,它会在终端上显示一堆错误......有什么问题,我该如何解决?

public class QuestionPanel extends JPanel {
        JRadioButton aButt, bButt, cButt, dButt, eButt; // buttons for the different colors the user can choose.

        int fontsize = 15;
        int ans = -1;
        int rand;
        MyPanel panel;

        public QuestionPanel() {
            panel = new MyPanel();
            this.add(panel);
        }

        class MyPanel extends JPanel
        {
            UserPanel user; // used for all controls on the left
            TextPanel text; // jtextarea for the quotation

            public MyPanel()
            {
                this.setLayout(new GridLayout(0, 2));
                user = new UserPanel();
                text = new TextPanel();
                this.add(user);
                this.add(text);
            }

            // User panel has Jradiobutton, Jbutton, and scroll bar
            class UserPanel extends JPanel implements ActionListener, AdjustmentListener // this is for the buttons and the scroll bar
            {
                JButton randbutton; // this is the random button

                RadioButton radioB;
                JScrollBar size; // size of the jScrollBar

                public UserPanel()
                {
                    setLayout(new GridLayout(3, 0));
                    setBackground(Color.lightGray);
                    randbutton = new JButton("PRESS FOR RANDOM QUESTION"); // random quote button
                    randbutton.addActionListener(this);
                    this.add(randbutton);

                    radioB = new RadioButton();
                    this.add(radioB);

                    size = new JScrollBar(JScrollBar.HORIZONTAL, 15, 1, 10, 20); // size of the scroll bar

                    size.addAdjustmentListener(this); // adding the scroll bar
                    this.add(size);
                }        

                public void adjustmentValueChanged(AdjustmentEvent e) // this is for the size of the scroll bar
                {
                    randommove = false;
                    fontsize = e.getValue(); // get the value of the scroll bar depending where you keep it or turn it
                    text.repaint();
                }

                // seperate class for radio button to allow three buttons
                // next to each other
                class RadioButton extends JPanel implements ActionListener
                {
                    ButtonGroup answers; // what color it is
                                        JButton a,b,c,d;

                    public RadioButton()
                    {
                        a = new JButton("A"); // A BUTTON
                                                a.addActionListener(this);
                                                this.add(a);

                                                b = new JButton("B"); //b button
                                                b.addActionListener(this);
                                                this.add(b);

                                                c = new JButton("C");
                                                c.addActionListener(this);
                                                this.add(c);

                                                d = new JButton("D");
                                                d.addActionListener(this);
                                                this.add(d);


                    }

                    public void actionPerformed(ActionEvent e)
                    {
                        randommove = false;
                                                String command = e.getActionCommand();

                                                if(rand == 0 && command.equals("A"))
                                                {
                                                    cards.show(c,"Game");
                                                }
                                                else
                                                {
                                                    cards.show(c,"Wrong");
                                                    System.out.println("hello");
                                                }

                                                if(rand == 1 && command.equals("B"))
                                                {
                                                    cards.show(c,"Game");
                                                }
                                                else
                                                {
                                                    cards.show(c,"Wrong");
                                                                                                        System.out.println("hello");

                                                }
                                                if(rand == 2 && command.equals("D"))
                                                {
                                                    cards.show(c,"Game");
                                                }
                                                else
                                                {
                                                    cards.show(c,"Wrong");
                                                                                                        System.out.println("hello");

                                                }
                                                if(rand == 3 && command.equals("B"))
                                                {
                                                    cards.show(c,"Game");
                                                }
                                                else{
                                                    cards.show(c,"Wrong");
                                                                                                        System.out.println("hello");

                                                }






                        text.repaint(); 
                    }
                }

                public void actionPerformed(ActionEvent e)
                {
                    randommove = true;
                    text.repaint();
                }
            }

1 个答案:

答案 0 :(得分:2)

您实际上并未设置按钮的操作命令,而只是设置文本。

当您尝试在动作侦听器中访问动作命令时,值为null

您需要调用JButton#setActionCommand来实际设置动作命令