簡體   English   中英

為什么JButton不起作用?

[英]Why Do the JButtons not work?

我正在創造游戲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();
                }
            }

你實際上並沒有設置按鈕的動作命令,你只是設置文本。

當您嘗試在操作偵聽器中訪問操作命令時,該值為null

你需要調用JButton#setActionCommand來實際設置動作命令

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM