按钮不会调整大小

时间:2014-12-07 04:16:23

标签: java swing

我正在制作一个tic tac toe游戏,并且在我的框架底部与棋盘我试图添加一个带有菜单,重置和退出按钮的面板。为此,我制作了两个JPanel,一个用于电路板本身,另一个用于这些按钮。当我将两个组件添加到JFrame时,我的重置,菜单和退出按钮不会调整大小。为什么会发生这种情况?如何解决?

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

这应该如何使用数组来最小化重复性?

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

2 个答案:

答案 0 :(得分:0)

您确定要设置BOTH JPanels的布局吗?

通常我只是将布局设置为null,并使用setBounds()设置按钮的位置/大小。

答案 1 :(得分:0)

将框架的布局更改为BorderLayout(默认情况下)...

setLayout(new BorderLayout());

然后将网格添加到中心,将按钮添加到SOUTH位置...

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

TicTacToe

我不是改变按钮的首选大小,而是创建一个工厂方法来构造按钮并改变边距而不是......

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

请参阅Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?了解原因......

如果你使用一组按钮而不是分配给各个变量的多个按钮实例,你也会发现它更容易......