tags:

views:

96

answers:

2
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class tictac2 implements ActionListener{
    static boolean blue = true; //used to keep track of turns. if true, blue's turn, else, red's turn. Blue is x, red is o
    static int bWins = 0, rWins = 0;
    JFrame mainWindow;
    JPanel board;
    JButton[] buttons;
    public tictac2() {
        init();
    }
    private void init() {
        try { //Try to set the L&F to system
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            e.toString();
            System.out.println("Couln't change look and feel. Using default");
        }
        mainWindow = new JFrame("Tic Tac Toe!");
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setVisible(true);
        mainWindow.setSize(800,600);
        JMenuBar bar = new JMenuBar(); //Menu bar init
        JMenu file = new JMenu("File");
        JMenuItem newGame = new JMenuItem("New Game"), quitItem = new JMenuItem("Quit");
        file.add(newGame);
        file.addSeparator();
        file.add(quitItem);
        bar.add(file);
        mainWindow.getContentPane().add(bar, BorderLayout.NORTH); //Menu bar done
        newGameBoard(); //New Game Board
        JPanel infoPane = new JPanel();
        infoPane.setLayout(new GridLayout(1,3));
        JLabel turn = new JLabel("Blue Player's Turn"), spacer = new JLabel(""), score = new JLabel("Blue: 0, Red: 0");
        infoPane.add(turn);
        infoPane.add(spacer);
        infoPane.add(score);
        mainWindow.getContentPane().add(infoPane,BorderLayout.SOUTH);
        newGame.addActionListener(this); //Add action listeners
        quitItem.addActionListener(this);

    }
    private void newGameBoard() {
        board = new JPanel(); //Game Pane init
        board.setLayout(new GridLayout(3,3));
        buttons = new JButton[9];
        for (int i = 0; i <9; i++){
            buttons[i] = new JButton("");
            buttons[i].setOpaque(true);
            buttons[i].setFont(new Font("sansserif", Font.BOLD, 90));
            board.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
        mainWindow.getContentPane().add(board,BorderLayout.CENTER); //Finish game pane init
    }
    public void actionPerformed(ActionEvent e){
        if (e.getSource() instanceof JButton){
            JButton j = (JButton)e.getSource();
            j.setForeground(tictac2.blue ? Color.BLUE:Color.RED);
            j.setText(tictac2.blue ? "X" : "O");
            j.setEnabled(false);
            tictac2.blue = !tictac2.blue;

        }
        else if (e.getSource() instanceof JMenuItem){
            JMenuItem j = (JMenuItem) e.getSource();
            if (j.getText().equals("Quit")) {
                System.exit(0);
            }
            else if (j.getText().equals("New Game")) {
                board.removeAll();
                mainWindow.remove(board);
                board = null;
                for (int i = 0; i < 9; i++) {
                    buttons[i] = null;
                }
                newGameBoard();
                tictac2.bWins = 0;
                tictac2.rWins = 0;
                tictac2.blue = true;
                mainWindow.repaint(100);
            }
        }
    }
    public static void main(String[] args) {
            new tictac2();
    }
}

I am working on a tic tac toe program. In it I have 2 buttons. Whenever I hit new game, the newGameBoard function is supposed to run and make a new board. However, the screen just goes blank! I can't figure it out and would appreciate help. Sorry if I am not posting in the correct format, I am new here.

Thanks so much!

+2  A: 

Move mainWindow.setVisible(true) to the end of init(). You don't want to set the frame to visible until you've added everything to it. That way it will validate and layout its subcomponents.

Another solution is to manually call mainWindow.validate() at the end of init(). That's what you have to do if you add components to the frame after making it visible.

John Kugelman
I tested both. Didn't seem to work.
sarahTheButterFly
I tried moving mainWindow.setVisible(true) to the end of init() but it doesn't help with the new game. Moving it to the end of newGameBoard() worked though.
Arnold Spence
Yeah, if you add or remove components *after* setting the frame visible then you'll need to call `validate()` to get the frame to re-layout its components.
John Kugelman
Thanks. Works great now! Now to do AI. Ugh.
David Watson
A: 

use paintAll() or validate() on mainWindow:

mainWindow.validate();

instead of :

mainWindow.repaint(100);

validate and paintAll() as described in API doc.

Your changed code looks like:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class TicTac2 implements ActionListener{
    static boolean blue = true; //used to keep track of turns. if true, blue's turn, else, red's turn. Blue is x, red is o
    static int bWins = 0, rWins = 0;
    JFrame mainWindow;
    JPanel board;
    JButton[] buttons;
    public TicTac2() {
        init();
    }
    private void init() {
        try { //Try to set the L&F to system
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            e.toString();
            System.out.println("Couln't change look and feel. Using default");
        }
        mainWindow = new JFrame("Tic Tac Toe!");
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setVisible(true);
        mainWindow.setSize(800,600);
        JMenuBar bar = new JMenuBar(); //Menu bar init
        JMenu file = new JMenu("File");
        JMenuItem newGame = new JMenuItem("New Game"), quitItem = new JMenuItem("Quit");
        file.add(newGame);
        file.addSeparator();
        file.add(quitItem);
        bar.add(file);
        mainWindow.getContentPane().add(bar, BorderLayout.NORTH); //Menu bar done
        newGameBoard(); //New Game Board
        JPanel infoPane = new JPanel();
        infoPane.setLayout(new GridLayout(1,3));
        JLabel turn = new JLabel("Blue Player's Turn"), spacer = new JLabel(""), score = new JLabel("Blue: 0, Red: 0");
        infoPane.add(turn);
        infoPane.add(spacer);
        infoPane.add(score);
        mainWindow.getContentPane().add(infoPane,BorderLayout.SOUTH);
        newGame.addActionListener(this); //Add action listeners
        quitItem.addActionListener(this);

    }
    private void newGameBoard() {
        board = new JPanel(); //Game Pane init
        board.setLayout(new GridLayout(3,3));
        buttons = new JButton[9];
        for (int i = 0; i <9; i++){
            buttons[i] = new JButton("");
            buttons[i].setOpaque(true);
            buttons[i].setFont(new Font("sansserif", Font.BOLD, 90));
            board.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
        mainWindow.getContentPane().add(board,BorderLayout.CENTER); //Finish game pane init
    }
    public void actionPerformed(ActionEvent e){
        if (e.getSource() instanceof JButton){
            JButton j = (JButton)e.getSource();
            j.setForeground(TicTac2.blue ? Color.BLUE:Color.RED);
            j.setText(TicTac2.blue ? "X" : "O");
            j.setEnabled(false);
            TicTac2.blue = !TicTac2.blue;

        }
        else if (e.getSource() instanceof JMenuItem){
            JMenuItem j = (JMenuItem) e.getSource();
            if (j.getText().equals("Quit")) {
                System.exit(0);
            }
            else if (j.getText().equals("New Game")) {
                board.removeAll();
                mainWindow.remove(board);
                board = null;
                for (int i = 0; i < 9; i++) {
                    buttons[i] = null;
                }
                newGameBoard();
                TicTac2.bWins = 0;
                TicTac2.rWins = 0;
                TicTac2.blue = true;

                mainWindow.validate();
                //mainWindow.repaint();
            }
        }
    }
    public static void main(String[] args) {
            new TicTac2();
    }
}
YoK