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 8753161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:22:32+00:00 2026-06-13T13:22:32+00:00

I’m having difficulty kicking off a new session. When I use the menu option

  • 0

I’m having difficulty kicking off a new session. When I use the menu option new game, I get a new instance – when really, I just want to replace the current instance with a new one. I have tried many different things and still can’t solve it…

Here is my code:

package tictactoe;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TicTacToe2 extends JFrame implements ActionListener {

char[][] game = new char[3][3];
JButton[][] buttons = new JButton[3][3]; 

JButton menuItem      = new JButton();   
JMenu     menu        = new JMenu  ("TicTacToe");
JMenuItem newgame     = new JMenuItem("Start New Game"), 
          exit        = new JMenuItem("Exit"); 

TicTacToe2()
{
    super("Tic Tac Toe");
    setSize(500, 600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setLayout( new BorderLayout());

    JPanel northPanel= new JPanel();
    northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));  
    add("North", northPanel);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(3,3, 4, 4));
    add("Center", buttonPanel);
    Font font = new Font("Serif", Font.BOLD, 32);

    for (int row=0; row < game.length; row++)
    {
        for (int col =0; col < game[row].length; col++)
        {
            game[row][col] = ' ';
            JButton b = new JButton(" ");
            b.setFont(font);
            b.setBackground(Color.green);
            b.addActionListener(this);
            buttons[row][col] = b;
            buttonPanel.add(b);
        }
    } 

    menu.add(newgame);  
    menu.add(exit);
    newgame.addActionListener(this); 
    exit.addActionListener(this); 

    JMenuBar bar = new JMenuBar( ); 
    bar.add(menu);  
    setJMenuBar(bar); 

} 
public void actionPerformed(ActionEvent e) {
    Object  menusource = e.getSource();   
    if(menusource ==  newgame){ 
        new TicTacToe2();   
    }else if(menusource ==  exit){
        System.exit(0);
    }  
} 
public static void main(String[] args) {
    TicTacToe2 ttt = new TicTacToe2();

}
}
  • 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-13T13:22:33+00:00Added an answer on June 13, 2026 at 1:22 pm

    As above answer already mentioned that you do not have to create a new instance like

    new TicTacToe2()
    

    So, i will continue from there. Your if statement should be something like

    if (menusource == newgame) {
         getContentPane.removeAll();
         setContentPane(aFunc);
    }
    

    Create a function which looks like something below which sets layout and adds the component to it. Put your drawing logic there and give it as an argument to setContent pane. For example

     private JPanel aFunc() {
        custSelectPanel.setLayout(null);
    
        customerTable.setDragEnabled(false);
        customerTable.setFillsViewportHeight(true);
    
        ......
    
        cancelButton.setLocation(350, 0);
        cancelButton.setSize(100, 40);
        buttonPanel.add(cancelButton);
    
        return custSelectPanel;
    }
    

    I hope you are getting the logic here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.