Reputation: 43
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();
}
}
Upvotes: 4
Views: 242
Reputation: 2725
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
Upvotes: 0
Reputation: 10995
public void actionPerformed(ActionEvent e) {
Object menusource = e.getSource();
if(menusource == newgame){
new TicTacToe2(); //What are you doing with this object?
//Should really be resetGame();
}else if(menusource == exit){
System.exit(0);
}
}
Could be a problem.. I suspect that's where you get a new frame instance. Have a method that clears the arrays to a default value, resets the score and you should be good to go.
Upvotes: 2
Reputation: 159784
Instead of creating a new instance of your class
new TicTacToe2();
when the "New Game" menu item is clicked, you could clear the button array text and prepare for a new game:
for (int i=0; i < buttons.length; i++ ) {
for (int j=0; j < buttons[0].length; j++ ) {
buttons[i][j].setText(" ");
}
}
Upvotes: 2
Reputation: 47267
You are creating a new instance each time when going through the menu for "new game" by:
new TicTacToe2();
If you'd like to just reset the instance, write a reset()
method that sets the game state to initial state.
Upvotes: 4