Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7932581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:03:44+00:00 2026-06-03T21:03:44+00:00

I have problem with the Play Again button, which is to reset the panel

  • 0

I have problem with the Play Again button, which is to reset the panel to initial state.

The Play Again button should reset the panel, it does reshuffle the gameList, but not reset the panel, which means all the buttons remain and lost the ActionListener function.

Here is my program :

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);
    }   
}

How can I solve this problem?

EDIT :

Another problem is after reset(), all buttons seem like become sameValue(). I have insert some audio to the buttons, and it’s happen in the condition of the ButtonListener. Here is the part :

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

Seems like it’s satisfy the condition sameValue(), but why?

LATEST UPDATE :

Problem Solved. Latest code is not uploaded.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:03:46+00:00Added an answer on June 3, 2026 at 9:03 pm

    Ok, I’ve fixed your code.

    Be sure to check out my comment to your question and let me know if it works (I did a quick test and it went fine)

    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);
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I have a button that, when pressed, should animate/play a png image sequence.
I have a form with the following button: <input id=test2 class=formbutton type=submit value=Play again
I have this problem, I have some audio I wish to play... And I
Good day once again. I have a (seemingly) simple problem. I'm trying to display
I have an app which plays a few short sound clips. To play them
I use MPMoviePlayer to play my video, the problem I have is when the
I searched everywhere but I couldn't find an answer for my problem. I play
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with fancybox. I want to write a function that will run
i have problem with autorotate on iphone i set up in all classes -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.