0

I'm making a tic tac toe game and on the bottom of my frame with the board I'm trying to add a panel with a menu, reset, and quit button. To do this I've made two JPanels, one for the board itself and one for these buttons. When I add both components to the JFrame, my reset, menu, and quit button won't resize. Why is this occurring and how can I fix it?

import java.awt.Color;
import java.awt.Dimension;
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.JLabel;
import javax.swing.JPanel;

public class Board extends JFrame implements ActionListener {

    private JButton one = new JButton();
    private JButton two = new JButton();
    private JButton three = new JButton();
    private JButton four = new JButton();
    private JButton five = new JButton();
    private JButton six = new JButton();
    private JButton seven = new JButton();
    private JButton eight = new JButton();
    private JButton nine = new JButton();

    private JButton resetButton = new JButton("Reset");
    private JLabel placeHolder = new JLabel("");
    private JButton menuButton = new JButton("Menu");
    private JButton exitButton = new JButton("Exit");

    private Font f = new Font("Arial", Font.BOLD, 100);



    public Board() {
        setTitle("Tic-Tac-Toe Game");
        setLayout(new GridLayout(2,1));
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Panel containing all buttons for game play
        JPanel gameBoard = new JPanel();
        gameBoard.setLayout(new GridLayout(3,3,8,8));
        gameBoard.add(one);
        gameBoard.add(two);
        gameBoard.add(three);
        gameBoard.add(four);
        gameBoard.add(five);
        gameBoard.add(six);
        gameBoard.add(seven);
        gameBoard.add(eight);
        gameBoard.add(nine);
        one.setPreferredSize(new Dimension(150, 150));

        // Panel used to put reset button on board
        JPanel resetButtonPanel = new JPanel();
        resetButtonPanel.setLayout(new GridLayout(1,3,8,8));
        resetButtonPanel.add(menuButton);
        resetButtonPanel.add(resetButton);
        resetButtonPanel.add(exitButton);

        // Adding all components to the Board() constructor
        add(gameBoard);
        add(resetButtonPanel);

        // Adds listeners to all buttons on board
        one.addActionListener(this);
        two.addActionListener(this);
        three.addActionListener(this);
        four.addActionListener(this);
        five.addActionListener(this);
        six.addActionListener(this);
        seven.addActionListener(this);
        eight.addActionListener(this);
        nine.addActionListener(this);
        resetButton.addActionListener(this);
        exitButton.addActionListener(this);
        menuButton.addActionListener(this);

        // Turn off focus on all buttons to avoid blue outline around buttons during game
        one.setFocusable(false);
        two.setFocusable(false);
        three.setFocusable(false);
        four.setFocusable(false);
        five.setFocusable(false);
        six.setFocusable(false);
        seven.setFocusable(false);
        eight.setFocusable(false);
        nine.setFocusable(false);

        // Sets all buttons to same font
        one.setFont(f);
        two.setFont(f);
        three.setFont(f);
        four.setFont(f);
        five.setFont(f);
        six.setFont(f);
        seven.setFont(f);
        eight.setFont(f);
        nine.setFont(f);


        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    int turn = 0;

    public void actionPerformed(ActionEvent e) {


        if (e.getSource() == one) {
            turn++;
            if(turn%2 == 0) {
                one.setText("X");
            }else{
                one.setText("O");
            }
            one.setEnabled(false);
            checkWin();
        }
        if (e.getSource() == two) {
            turn++;
            if(turn%2 == 0) {
                two.setText("X");
            }else{
                two.setText("O");
            }
            two.setEnabled(false);
            checkWin();
        }
        if (e.getSource() == three) {
            turn++;
            if(turn%2 == 0) {
                three.setText("X");
            }else{
                three.setText("O");
            }
            three.setEnabled(false);
            checkWin();
        }
        if (e.getSource() ==four) {
            turn++;
            if(turn%2 == 0) {
                four.setText("X");
            }else{
                four.setText("O");
            }
            four.setEnabled(false);
            checkWin();
        }
        if (e.getSource() == five) {
            turn++;
            if(turn%2 == 0) {
                five.setText("X");
            }else{
                five.setText("O");
            }
            five.setEnabled(false);
            checkWin(); 
        }
        if (e.getSource() == six) {
            turn++;
            if(turn%2 == 0) {
                six.setText("X");
            }else{
                six.setText("O");
            }
            six.setEnabled(false);
            checkWin();
        }
        if (e.getSource() == seven) {
            turn++;
            if(turn%2 == 0) {
                seven.setText("X");
            }else{
                seven.setText("O");
            }
            seven.setEnabled(false);
            checkWin();
        }
        if (e.getSource() == eight) {
            turn++;
            if(turn%2 == 0) {
                eight.setText("X");
            }else{
                eight.setText("O");
            }
            eight.setEnabled(false);
            checkWin();
        }
        if (e.getSource() == nine) {
            turn++;
            if(turn%2 == 0) {
                nine.setText("X");
            }else{
                nine.setText("O");
            }
            nine.setEnabled(false);
            checkWin();
        }
        if(e.getSource() == resetButton) {
            dispose();
            new Board();
        }
        if(e.getSource() == exitButton) {
            System.exit(0);
        }
        if(e.getSource() == menuButton) {
            dispose();
            new Menu();
        }
    }

    // Tests all possible win scenarios
    public void checkWin() {

        String oneText = one.getText();
        String twoText = two.getText();
        String threeText = three.getText();
        String fourText = four.getText();
        String fiveText = five.getText();
        String sixText = six.getText();
        String sevenText = seven.getText();
        String eightText = eight.getText();
        String nineText = nine.getText();

        if(oneText.equals(twoText) && twoText.equals(threeText) && !oneText.equals("") && !twoText.equals("") && !threeText.equals("")){
            one.setBackground(Color.green);
            one.setOpaque(true);
            two.setBackground(Color.green);
            two.setOpaque(true);
            three.setBackground(Color.green);
            three.setOpaque(true);
            disableButtons();
        }
        if(oneText.equals(fiveText) && fiveText.equals(nineText) && !oneText.equals("") && !fiveText.equals("") && !nineText.equals("")) {
            one.setBackground(Color.green);
            one.setOpaque(true);
            five.setBackground(Color.green);
            five.setOpaque(true);
            nine.setBackground(Color.green);
            nine.setOpaque(true);
            disableButtons();
        }
        if(oneText.equals(fourText) && fourText.equals(sevenText) && !oneText.equals("") && !fourText.equals("") && !sevenText.equals("")) {
            one.setBackground(Color.green);
            one.setOpaque(true);
            four.setBackground(Color.green);
            four.setOpaque(true);
            seven.setBackground(Color.green);
            seven.setOpaque(true);
            disableButtons();
        }
        if(twoText.equals(fiveText) && fiveText.equals(eightText) && !twoText.equals("") && !fiveText.equals("") && !eightText.equals("")) {
            two.setBackground(Color.green);
            two.setOpaque(true);
            five.setBackground(Color.green);
            five.setOpaque(true);
            eight.setBackground(Color.green);
            eight.setOpaque(true);
            disableButtons();
        }
        if(threeText.equals(sixText) && sixText.equals(nineText) && !threeText.equals("") && !sixText.equals("") && !nineText.equals("")) {
            three.setBackground(Color.green);
            three.setOpaque(true);
            six.setBackground(Color.green);
            six.setOpaque(true);
            nine.setBackground(Color.green);
            nine.setOpaque(true);
            disableButtons();
        }
        if(threeText.equals(fiveText) && fiveText.equals(sevenText) && !threeText.equals("") && !fiveText.equals("") && !sevenText.equals("")) {
            three.setBackground(Color.green);
            three.setOpaque(true);
            five.setBackground(Color.green);
            five.setOpaque(true);
            seven.setBackground(Color.green);
            seven.setOpaque(true);
            disableButtons();
        }
        if(fourText.equals(fiveText) && fiveText.equals(sixText) && !fourText.equals("") && !fiveText.equals("") && !sixText.equals("")) {
            four.setBackground(Color.green);
            four.setOpaque(true);
            five.setBackground(Color.green);
            five.setOpaque(true);
            six.setBackground(Color.green);
            six.setOpaque(true);
            disableButtons();
        }
        if(sevenText.equals(eightText) && eightText.equals(nineText) && !sevenText.equals("") && !eightText.equals("") && !nineText.equals("")) {
            seven.setBackground(Color.green);
            seven.setOpaque(true);
            eight.setBackground(Color.green);
            eight.setOpaque(true);
            nine.setBackground(Color.green);
            nine.setOpaque(true);
            disableButtons();
        }
    }
    // Method to turn off buttons when a win occurs
    public void disableButtons(){
        one.setEnabled(false);
        two.setEnabled(false);
        three.setEnabled(false);
        four.setEnabled(false);
        five.setEnabled(false);
        six.setEnabled(false);
        seven.setEnabled(false);
        eight.setEnabled(false);
        nine.setEnabled(false);
    }
}

Is this how the array should be used to minimize repetitiveness?

    JButton[] buttons = {one, two, three, four, five, six, seven, eight, nine};

            // Panel containing all buttons for game play
            JPanel gameBoard = new JPanel();
            gameBoard.setLayout(new GridLayout(3,3,8,8));
            for(int i = 0; i < buttons.length; i++){
                gameBoard.add(buttons[i]);
            }
            one.setPreferredSize(new Dimension(150, 150));
  • I don't know you're problem, as the resize okay when the frame is resized... – MadProgrammer Dec 07 '14 at 04:32
  • Nice game by the way ;) – MadProgrammer Dec 07 '14 at 04:33
  • @MadProgrammer Sorry if my question lacked clarity. I want the buttons to be smaller when the frame is opened initially, so that they are smaller than the buttons on the board itself (maybe size (100,25)ish). And thanks! It's my first real attempt at a program beyond the bare basics on my own. –  Dec 07 '14 at 04:36

2 Answers2

0

Have you made sure to set the layout of BOTH JPanels?

Usually I just set the layout to null and set the locations/sizes of the buttons with setBounds().

EKW
  • 2,059
  • 14
  • 24
  • 4
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Dec 07 '14 at 04:23
  • Yes, both JPanel have GridLayouts, along with the JFrame itself. –  Dec 07 '14 at 04:24
  • It's right there in the code that was posted. `gameBoard.setLayout(new GridLayout(3,3,8,8));` `resetButtonPanel.setLayout(new GridLayout(1,3,8,8));` – takendarkk Dec 07 '14 at 04:26
0

Change the layout for the frame to something like BorderLayout (which it is by default)...

setLayout(new BorderLayout());

Then add the grid to the center and the buttons to the SOUTH position...

add(gameBoard);
add(resetButtonPanel, BorderLayout.SOUTH);

TicTacToe

Instead of changing the preferred size of the buttons, I would create a factory method to construct the buttons and change the margins instead...

JPanel gameBoard = new JPanel();
gameBoard.setLayout(new GridLayout(3, 3, 8, 8));
gameBoard.add((one = createButton()));
gameBoard.add((two = createButton()));
gameBoard.add((three = createButton()));
gameBoard.add((four = createButton()));
gameBoard.add((five = createButton()));
gameBoard.add((six = createButton()));
gameBoard.add((seven = createButton()));
gameBoard.add((eight = createButton()));
gameBoard.add((nine = createButton()));
//one.setPreferredSize(new Dimension(150, 150));

//...

protected JButton createButton() {
    JButton btn = new JButton();
    btn.setMargin(new Insets(50, 50, 50, 50));
    return btn;
}

See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for reasons why...

You may also find it easier if you use an array of buttons instead of multiple instances of the buttons assigned to individual variables...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Just what I was looking for, thanks for the help. By using arrays would I just be able to use these functions on the array as a whole and save time? –  Dec 07 '14 at 04:48
  • Yes, you could use a loop to loop through the array and reduce all the code duplication – MadProgrammer Dec 07 '14 at 04:54
  • Could you take a look at the bottom of my question. Is this how the array would work? –  Dec 07 '14 at 18:02