记忆游戏动作侦听器调试

时间:2018-10-17 09:46:54

标签: java swing debugging

在花了很多时间试图让我的动作侦听器执行我想要的事情之后,我遇到了一些调试问题,这些问题只是我不理解。

在游戏中找到正确的配对后,第一个正确的配对将正常工作,禁用两个选定的按钮并继续前进。此后,比较将停止工作。我似乎找不到原因。设置System.out.print后,检查编程是否再次通过单击进行,但是不允许进行其他比较或从else语句重置按钮。

在下面,我列出了我的主要动作监听器和暂停动作监听器。谢谢,如果您需要更多数据ID,请随时更新。

  ActionListener timerPause = new ActionListener() {

        public void actionPerformed(ActionEvent e) {



            for (int i = 0; i < jToggleLength; i++) {

                jToggleButtons[i].setEnabled(true);
                tempIconThree = ((ImageIcon) 
        jToggleButtons[i].getSelectedIcon()).getDescription();

                    //flip the selected buttons back over
                if(jToggleButtons[i].isSelected()){
                       jToggleButtons[i].doClick();

                    }
                }
        }
    };

    /**
     * sets the pause timer period to 1.5 seconds
     * sets it to the actionListener Timerpause
     */

    pause = new Timer(1500, timerPause);


   public void actionPerformed(ActionEvent ae) {

    //starts timer
    //increments click on each action
    pause.stop();
    timer.start();
    click++;

    //if jtogglelength = 0 the user has completed the game and the timer stops
    if (jToggleLength == 0) {

        timer.stop();
        click = 0;

    }

    //on first click the jtoggle button source is set to temp and the temp icon gets description
    if (click == 1) {

        temp = (JToggleButton) ae.getSource();
        tempIcon = ((ImageIcon) temp.getSelectedIcon()).getDescription();

    }
    // on click two the jtoggle button source is set to tempTwo and the tempTwo icon gets description
    if (click == 2) {

        tempTwo = (JToggleButton) ae.getSource();
        tempIconTwo = ((ImageIcon) tempTwo.getSelectedIcon()).getDescription();

        //if the button is the same button set click to zero and do nothing else
        if (temp == tempTwo) {

            click = 0;

        //if the temp icon descriptions are equivalent move forward
        }  else if (tempIcon.equals(tempIconTwo)) {

                click = 0;

                //this for loop sets tempIconThree in every loop to compare to my first two icon descriptions
                //if they match, disable that button and set the button to null
                //if the button is not null add it to an arraylist
                for (int j = 0; j < jToggleLength; j++) {

                    tempIconThree = ((ImageIcon) jToggleButtons[j].getSelectedIcon()).getDescription();

                    if (tempIcon.equals(tempIconThree) || tempTwo.equals(tempIconThree)) {

                        jToggleButtons[j].setEnabled(false);
                        jToggleButtons[j] = null;

                        if (jToggleButtons[j] != null) {

                            arrayEdit.add(jToggleButtons[j]);
                        }

                    }

                }
                //reduce the length of the array by 2
                //make a new version of the main jtogglebutton array with reduced length
                //add the non null elements from the arrayList to the array
                jToggleLength = -2;
                for (int k = 0; k < jToggleLength; k++) {

                    jToggleButtons = new JToggleButton[jToggleLength];
                    jToggleButtons[k] = arrayEdit.get(k);

                }

                click = 0;
            //if the icon descriptions did not match
            //disable all buttons, pause for 1.5 seconds, flip the 
            selected buttons back up
            } else {

                for (int i = 0; i < jToggleLength; i++) {

                    jToggleButtons[i].setEnabled(false);


                }

                pause.start();
                click = 0;


            }
        }

0 个答案:

没有答案