2

我的按钮有问题Play Again,它是将面板重置为initial状态。

按钮应该重置面板,Play Again它会重新洗牌gameList,但不会重置面板,这意味着所有按钮都保留并失去了ActionListener功能。

这是我的程序:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import static java.util.Collections.*;

public class MemoryGame extends JFrame
{
    private JButton exitButton, replayButton;
    private JButton[] gameButton = new JButton[16];
    private ArrayList<Integer> gameList = new ArrayList<Integer>();
    private int counter = 0;
    private int[] buttonID = new int[2];
    private int[] buttonValue = new int[2];

    public static Point getCenterPosition(int frameWidth, int frameHeight)
    {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimension = toolkit.getScreenSize();

        int x = (dimension.width - frameWidth)/2;
        int y = (dimension.height - frameHeight)/2;
        return (new Point(x,y));
    }

    public MemoryGame(String title)
    {
        super(title);

        initial();      
    }

    public void initial()
    {
        for (int i = 0; i < gameButton.length; i++) 
        {
            gameButton[i] = new JButton();
            gameButton[i].setFont(new Font("Serif", Font.BOLD, 28));
            gameButton[i].addActionListener(new ButtonListener());
        }
        exitButton = new JButton("Exit");
        replayButton = new JButton("Play Again");
        exitButton.addActionListener(new ButtonListener());
        replayButton.addActionListener(new ButtonListener());

        Panel gamePanel = new Panel();
        gamePanel.setLayout(new GridLayout(4, 4));
        for (int i = 0; i < gameButton.length; i++) 
        {
            gamePanel.add(gameButton[i]);
        }

        Panel buttonPanel = new Panel();
        buttonPanel.add(replayButton);
        buttonPanel.add(exitButton);
        buttonPanel.setLayout(new GridLayout(1, 0));

        add(gamePanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        for (int i = 0; i < 2; i++) 
        {
            for (int j = 1; j < (gameButton.length / 2) + 1; j++) 
            {
                gameList.add(j);
            }
        }
        shuffle(gameList);

        int newLine = 0;
        for (int a = 0; a < gameList.size(); a++) 
        {
            newLine++;
            System.out.print(" " + gameList.get(a));
            if (newLine == 4) 
            {
                System.out.println();
                newLine = 0;
            }
        }  
    }

    public boolean sameValues() 
    {
        if (buttonValue[0] == buttonValue[1]) 
        {
            return true;
        }
        return false;
    }

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            if (exitButton == e.getSource()) 
            {
                System.exit(0);
        }
        else if (replayButton == e.getSource()) 
        {
            initial();
        }
        for (int i = 0; i < gameButton.length; i++) 
        {
            if (gameButton[i] == e.getSource()) 
            {
                gameButton[i].setText("" + gameList.get(i));
                gameButton[i].setEnabled(false);
                counter++;

                if (counter == 3) 
                {
                    if (sameValues()) 
                    {
                        gameButton[buttonID[0]].setEnabled(false);
                        gameButton[buttonID[1]].setEnabled(false);
                    } 
                    else 
                    {
                        gameButton[buttonID[0]].setEnabled(true);
                        gameButton[buttonID[0]].setText("");
                        gameButton[buttonID[1]].setEnabled(true);
                        gameButton[buttonID[1]].setText("");
                    }
                    counter = 1;
                }
                if (counter == 1) 
                {
                    buttonID[0] = i;
                    buttonValue[0] = gameList.get(i);
                }
                if (counter == 2) 
                {
                    buttonID[1] = i;
                    buttonValue[1] = gameList.get(i);
                }
            }
        }
    }
    }

    public static void main(String[] args)
    {
        int x, y;
        int width = 500;
        int height = 500;

        Point position = getCenterPosition(width, height);
        x = position.x;
        y = position.y;

        JFrame frame = new MemoryGame("Memory Game");
        frame.setBounds(x, y, width, height);

        frame.setVisible(true);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }   
}

我怎么解决这个问题?

编辑 :

另一个问题是之后reset(),所有的按钮似乎都变成了sameValue()。我已经在按钮上插入了一些音频,它发生在ButtonListener. 这是部分:

if (counter == 3) 
    {
        if (sameValues()) 
        {
            gameButton[buttonID[0]].setEnabled(false);
            gameButton[buttonID[1]].setEnabled(false);
        } 

似乎满足条件sameValue(),但为什么呢?

最新更新 :

问题解决了。最新代码未上传。

4

2 回答 2

3

在该initial()方法中,代码正在创建一个新的 GUI。最好保留原始控件的句柄,并将它们重置为“开始游戏”状态和正确的值(对于数字)。

您当前方法的替代方案是每个组件remove()add()新方法(不推荐)或使用CardLayout(不必要 - 请参阅第一个建议)。

于 2012-05-12T15:50:44.853 回答
2

好的,我已经修复了你的代码。

请务必查看我对您的问题的评论,并让我知道它是否有效(我做了一个快速测试,一切正常)

package varie;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import static java.util.Collections.shuffle;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MemoryGame extends JFrame {

private JButton exitButton, replayButton;
private JButton[] gameButton = new JButton[16];
private ArrayList<Integer> gameList = new ArrayList<>();
private int counter = 0;
private int[] buttonID = new int[2];
private int[] buttonValue = new int[2];

public static Point getCenterPosition(int frameWidth, int frameHeight) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dimension = toolkit.getScreenSize();

    int x = (dimension.width - frameWidth) / 2;
    int y = (dimension.height - frameHeight) / 2;
    return (new Point(x, y));
}

public MemoryGame(String title) {
    super(title);

    initial();
}

public void initial() {        
    for (int i = 0; i < gameButton.length; i++) {
        gameButton[i] = new JButton();
        gameButton[i].setFont(new Font("Serif", Font.BOLD, 28));
        gameButton[i].addActionListener(new ButtonListener());
    }
    exitButton = new JButton("Exit");
    replayButton = new JButton("Play Again");
    exitButton.addActionListener(new ButtonListener());
    replayButton.addActionListener(new ButtonListener());

    Panel gamePanel = new Panel();
    gamePanel.setLayout(new GridLayout(4, 4));
    for (int i = 0; i < gameButton.length; i++) {
        gamePanel.add(gameButton[i]);
    }

    Panel buttonPanel = new Panel();
    buttonPanel.add(replayButton);
    buttonPanel.add(exitButton);
    buttonPanel.setLayout(new GridLayout(1, 0));

    add(gamePanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);

    for (int i = 0; i < 2; i++) {
        for (int j = 1; j < (gameButton.length / 2) + 1; j++) {
            gameList.add(j);
        }
    }
    shuffle(gameList);

    int newLine = 0;
    for (int a = 0; a < gameList.size(); a++) {
        newLine++;
        System.out.print(" " + gameList.get(a));
        if (newLine == 4) {
            System.out.println();
            newLine = 0;
        }
    }

}

public boolean sameValues() {
    if (buttonValue[0] == buttonValue[1]) {
        return true;
    }
    return false;
}

public void reset() {        
    for(int i = 0; i< gameButton.length; i++){
        gameButton[i].setEnabled(true);
        gameButton[i].setText("");
        for(ActionListener al : gameButton[i].getActionListeners()){
            gameButton[i].removeActionListener(al);
        }
        gameButton[i].addActionListener(new ButtonListener());
    }
    buttonID = new int[2];
    buttonValue = new int[2];
    counter = 0;
    shuffle(gameList);
    int newLine = 0;
    for (int a = 0; a < gameList.size(); a++) {
        newLine++;
        System.out.print(" " + gameList.get(a));
        if (newLine == 4) {
            System.out.println();
            newLine = 0;
        }
    }
}

private class ButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        if (exitButton.equals(e.getSource())) {
            System.exit(0);
        } else if (replayButton.equals(e.getSource())) {
            reset();
        } else {
            for (int i = 0; i < gameButton.length; i++) {
                if (gameButton[i].equals(e.getSource())) {
                    gameButton[i].setText("" + gameList.get(i));
                    gameButton[i].setEnabled(false);
                    counter++;

                    if (counter == 3) {
                        if (sameValues()) {
                            gameButton[buttonID[0]].setEnabled(false);
                            gameButton[buttonID[1]].setEnabled(false);
                        } else {
                            gameButton[buttonID[0]].setEnabled(true);
                            gameButton[buttonID[0]].setText("");
                            gameButton[buttonID[1]].setEnabled(true);
                            gameButton[buttonID[1]].setText("");
                        }
                        counter = 1;
                    }
                    if (counter == 1) {
                        buttonID[0] = i;
                        buttonValue[0] = gameList.get(i);
                    }
                    if (counter == 2) {
                        buttonID[1] = i;
                        buttonValue[1] = gameList.get(i);
                    }
                }
            }
        }
    }
}

public static void main(String[] args) {
    int x, y;
    int width = 500;
    int height = 500;

    Point position = getCenterPosition(width, height);
    x = position.x;
    y = position.y;

    JFrame frame = new MemoryGame("Memory Game");
    frame.setBounds(x, y, width, height);

    frame.setVisible(true);
    frame.setResizable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
于 2012-05-12T16:13:37.030 回答