有人可以检查我的代码吗,我不知道这是代码本身的问题还是 macOS 的外观问题

问题描述 投票:0回答:0

我有两个问题。游戏是一个 3x3 的网格,顶部有一个标签,显示轮到谁和谁赢了。当标签显示获胜者时,标签的背景颜色将重置为默认值。同样在显示“Tic Tac Toe”以及单人游戏和多人游戏按钮的主屏幕中,单人游戏按钮在该按钮的文本周围有一个奇怪的白色轮廓。

我意识到有些颜色因为 macOS 上的织机和感觉而无法使用。我试着改变它,但现在我发现了更多问题。

这是。代码

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

public class TicTacToeGUI extends JFrame{
    private JButton[][] board;
    private static boolean xTurn = true;
    private JLabel label;
    private boolean isDraw;
    private boolean singleplayer;
    private boolean hasWon;

    public void checkWin() {
        // check horizontal lines
        Color green = new Color(0, 128, 0);


        for (int i = 0; i < 3; i++) {
            if (board[i][0].getText().equals(board[i][1].getText()) && board[i][1].getText().equals(board[i][2].getText()) && !board[i][0].getText().equals(" ")) {
                if(singleplayer && board[i][0].getText().equals("O")){
                    label.setText("Computer wins!");
                }
                else{
                    label.setText(board[i][0].getText() + " wins!"); 
                }
            
                board[i][0].setBackground(green);
                board[i][1].setBackground(green);
                board[i][2].setBackground(green);
                hasWon = true;
                disableButtons();
                addBackButton();
            }
        }
    
        // check vertical lines
        for (int j = 0; j < 3; j++) {
            if (board[0][j].getText().equals(board[1][j].getText()) && board[1][j].getText().equals(board[2][j].getText()) && !board[0][j].getText().equals(" ")) {
                if(singleplayer && board[0][j].getText().equals("O")){
                    label.setText("Computer wins!");
                }
                else{
                    label.setText(board[0][j].getText() + " wins!"); 
                }
                board[0][j].setBackground(green);
                board[1][j].setBackground(green);
                board[2][j].setBackground(green);
                hasWon = true;
                disableButtons();
                addBackButton();

            }
        }
    
        // check diagonal lines
        if (board[0][0].getText().equals(board[1][1].getText()) && board[1][1].getText().equals(board[2][2].getText()) && !board[0][0].getText().equals(" ")) {
            if(singleplayer && board[0][0].getText().equals("O")){
                label.setText("Computer wins!");
            }
            else{
                label.setText(board[0][0].getText() + " wins!"); 
            }            
            board[0][0].setBackground(green);
            board[1][1].setBackground(green);
            board[2][2].setBackground(green);
            hasWon = true;
            disableButtons();
            addBackButton();

        }
    
        if (board[2][0].getText().equals(board[1][1].getText()) && board[1][1].getText().equals(board[0][2].getText()) && !board[2][0].getText().equals(" ")) {
            if(singleplayer && board[2][0].getText().equals("O")){
                label.setText("Computer wins!");
            }
            else{
                label.setText(board[2][0].getText() + " wins!"); 
            }            
            board[2][0].setBackground(green);
            board[1][1].setBackground(green);
            board[0][2].setBackground(green);
            hasWon = true;
            disableButtons();
            addBackButton();
        }
        isDraw = true;
        if(!hasWon){
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    if(board[i][j].getText().equals(" ")){
                        isDraw = false;
                    }
                    
                }
            }
        
        if(isDraw){
            label.setText("It's a Draw!");
            disableButtons();
            addBackButton();
        }
        
    }

    }
    
    private void addBackButton() {
        // adds a back button if the game is finished
        JButton backButton = new JButton("Back");
        backButton.setBackground(Color.white);
        
        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getContentPane().removeAll();
                new TicTacToeGUI();
                dispose();
            }
        });
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(backButton);
    
        add(buttonPanel, BorderLayout.NORTH);
    }
    

    private void disableButtons() {
        // disables the buttons when the game is finished
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                board[i][j].setForeground(Color.white);
                board[i][j].setEnabled(false);
            }
        }
    }
    

    public void setBoard(){
        // creates the tic tac toe board
        setLayout(new BorderLayout());

        board = new JButton[3][3];


        label = new JLabel("X's turn");
        label.setFont(new Font("SANS_SERIF", Font.PLAIN, 30));
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setBackground(new Color(54, 117, 136));
        label.setForeground(Color.white);
        



        add(label, BorderLayout.NORTH);

        JPanel panel = new JPanel(new GridLayout(3, 3, 10, 10));
        panel.setBackground(new Color(54, 117, 136));

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                JButton button = new JButton(" ");
                button.setBackground(new Color(214, 214, 214));
                panel.add(button);
                button.setFont(new Font("Arial", Font.PLAIN, 32));
                board[i][j] = button;
                board[i][j].setOpaque(true);
                xTurn = true;
                if(singleplayer){
                    //Singleplayer board setup
                    button.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e){
                            if(button.getText().equals(" ")){
                                button.setText("X");
                                checkWin();
                                if(!isDraw){
                                    
                                boolean guess = true;
                                // makes sure that the computer only makes a move if the game hasn't finished yet
                                while(guess){
                                    int row = (int) (Math.random() * 3);
                                    int col = (int) (Math.random() * 3);
                                    if(board[row][col].getText().equals(" ")){
                                        board[row][col].setText("O");
                                        checkWin();
                                        guess = false;
                                    }
                                    
                                }
                            }
                            }

                        }
                    });
                }
                else{
                    //Multiplayer board setup
                    button.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                        if(button.getText().equals(" ")){
                            if(xTurn){
                                button.setText("X");
                                xTurn = false;
                                label.setText("O's turn");
                            }
                            else{
                                button.setText("O");
                                xTurn = true;
                                label.setText("X's turn");
                            }
                        checkWin();
                        }
                               
                    }
                });
            }
                
            }
        }
        add(panel, BorderLayout.CENTER);
        setVisible(true);
    }


    public TicTacToeGUI(){

        // Set up GridBagLayout
        GridBagConstraints constraints = new GridBagConstraints();
        setLayout(new GridBagLayout());
        
    
        // Set window properties
        setTitle("Tic Tac Toe");
        setSize(600, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setBackground(new Color(54, 117, 136));

    
        // Add title label
        JLabel title = new JLabel("Tic Tac Toe!");
        title.setFont(new Font("Sans_Serif", Font.BOLD, 36));
        title.setForeground(Color.white);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.insets = new Insets(10, 10, 50, 10); // Add extra bottom padding to separate title from buttons
        constraints.anchor = GridBagConstraints.CENTER; 
        add(title, constraints);
        
        Color teal = new Color(0, 130, 128);
        // Add singleplayer button
        JButton singleplayerButton = new JButton("Singleplayer");
        singleplayerButton.setFont(new Font("Sans_Serif", Font.BOLD, 24));
        singleplayerButton.setPreferredSize(new Dimension(200, 75)); // Make button larger
        singleplayerButton.setOpaque(true);
        singleplayerButton.setBackground(teal); // Set button color
        singleplayerButton.setForeground(Color.white); // Set button text color
        constraints.gridx = 0; 
        constraints.gridy = 1; 
        constraints.insets = new Insets(50, 10, 50, 10); // Add extra padding to separate buttons
        add(singleplayerButton, constraints); 
    
        // Add multiplayer button
        JButton multiplayerButton = new JButton("Multiplayer");
        multiplayerButton.setFont(new Font("Sans_Serif", Font.BOLD, 24));
        multiplayerButton.setPreferredSize(new Dimension(200, 75)); // Make button larger
        multiplayerButton.setOpaque(true);
        multiplayerButton.setForeground(Color.white); // Set button text color

        multiplayerButton.setBackground(teal); // Set button color
        constraints.gridx = 0;
        constraints.gridy = 2; 
        add(multiplayerButton, constraints);
    
        // Set up action listeners for buttons
        multiplayerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                getContentPane().removeAll();
                singleplayer = false;
    
                setBoard();
    
                revalidate();
                repaint();
                setVisible(true);
            }
    
        });
    
        singleplayerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                getContentPane().removeAll();
                singleplayer = true;
    
                setBoard();
    
                revalidate();
                repaint();
                setVisible(true);
            }
    
        });
        setVisible(true);
    }
    

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            // handle exception
        }
        new TicTacToeGUI();
        
    }

}

java swing user-interface look-and-feel
© www.soinside.com 2019 - 2024. All rights reserved.