0

我搜索了互联网试图解决我的问题。whoWon.setText ()部分没有更新JLabel

它应该更新JLabel的部分位于注释下的PlayAgain.java文件中

//获取获胜者

我已经尝试过repaint()postInvalidate(),但它仍然无法正常工作。一定有什么我错过了。

这是代码:

井字游戏.java =>

package ticTacToe;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TicTacToe extends JFrame implements ActionListener {

    private JPanel contentPane;
    boolean winner = false;
    boolean draw = false;
    byte count = 0;
    String btnLabel;


    PlayAgain playAgain = new PlayAgain();

    JButton btn1 = new JButton("");
    JButton btn2 = new JButton("");
    JButton btn3 = new JButton("");
    JButton btn4 = new JButton("");
    JButton btn5 = new JButton("");
    JButton btn6 = new JButton("");
    JButton btn7 = new JButton("");
    JButton btn8 = new JButton("");
    JButton btn9 = new JButton("");

    static TicTacToe frame = new TicTacToe();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TicTacToe() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 616, 637);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btn1.setBounds(0, 0, 200, 200);
        btn1.addActionListener(this);
        contentPane.add(btn1);


        btn2.setBounds(200, 0, 200, 200);
        btn2.addActionListener(this);
        contentPane.add(btn2);


        btn3.setBounds(400, 0, 200, 200);
        btn3.addActionListener(this);
        contentPane.add(btn3);


        btn4.setBounds(0, 200, 200, 200);
        btn4.addActionListener(this);
        contentPane.add(btn4);


        btn5.setBounds(200, 200, 200, 200);
        btn5.addActionListener(this);
        contentPane.add(btn5);


        btn6.setBounds(400, 200, 200, 200);
        btn6.addActionListener(this);
        contentPane.add(btn6);


        btn7.setBounds(0, 400, 200, 200);
        btn7.addActionListener(this);
        contentPane.add(btn7);


        btn8.setBounds(200, 400, 200, 200);
        btn8.addActionListener(this);
        contentPane.add(btn8);


        btn9.setBounds(400, 400, 200, 200);
        btn9.addActionListener(this);
        contentPane.add(btn9);
    }


//Main Code
    @Override
    public void actionPerformed(ActionEvent e) {


        //Play label (Player 1 = X, Player 2 = O)
        if(count==0 || count==2 || count==4 || count==6 || count==8){
            btnLabel = "X";
        }else btnLabel = "O";

        count++;

        //Clicking buttons

        if(e.getSource()==btn1){
            btn1.setText(btnLabel);
            btn1.setEnabled(false);
            btn1.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn2){
            btn2.setText(btnLabel);
            btn2.setEnabled(false);
            btn2.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn3){
            btn3.setText(btnLabel);
            btn3.setEnabled(false);
            btn3.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn4){
            btn4.setText(btnLabel);
            btn4.setEnabled(false);
            btn4.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn5){
            btn5.setText(btnLabel);
            btn5.setEnabled(false);
            btn5.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn6){
            btn6.setText(btnLabel);
            btn6.setEnabled(false);
            btn6.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn7){
            btn7.setText(btnLabel);
            btn7.setEnabled(false);
            btn7.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn8){
            btn8.setText(btnLabel);
            btn8.setEnabled(false);
            btn8.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn9){
            btn9.setText(btnLabel);
            btn9.setEnabled(false);
            btn9.setBackground(Color.BLACK);
        }


        //Win Conditions

        //Horizontal
                if(btn1.getText().equals(btn2.getText())&&btn2.getText().equals(btn3.getText())&!btn1.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn4.getText().equals(btn5.getText())&&btn5.getText().equals(btn6.getText())&!btn4.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn7.getText().equals(btn8.getText())&&btn8.getText().equals(btn9.getText())&!btn7.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    //Vertical
    else if(btn1.getText().equals(btn4.getText())&&btn4.getText().equals(btn7.getText())&!btn1.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn2.getText().equals(btn5.getText())&&btn5.getText().equals(btn8.getText())&!btn2.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn3.getText().equals(btn6.getText())&&btn6.getText().equals(btn9.getText())&!btn3.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    //Diagonal
    else if(btn1.getText().equals(btn5.getText())&&btn5.getText().equals(btn9.getText())&!btn1.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn3.getText().equals(btn5.getText())&&btn5.getText().equals(btn7.getText())&!btn3.getText().equals("")) {
        winner = true;
        afterGame(btnLabel);
    }

        //Draw
        else if(count==9&winner==false)System.out.println("Game is draw");



    }

    public void afterGame(String btnName){
        playAgain.getWinner(btnName);
        playAgain = new PlayAgain();
        playAgain.setVisible(true);

        frame.dispose();


    }

}

PlayAgain.java =>

package ticTacToe;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;

public class PlayAgain extends JFrame {

    private JPanel contentPane;
    JLabel whoWon = new JLabel();
    static PlayAgain frame = new PlayAgain();
    String winner;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public PlayAgain() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 153);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        whoWon.setText(" ");
        whoWon.setFont(new Font("Tahoma", Font.BOLD, 18));
        whoWon.setBounds(10, 11, 414, 48);
        contentPane.add(whoWon);

        JButton playAgain = new JButton("Play Again!");
        playAgain.setBounds(10, 70, 200, 34);
        contentPane.add(playAgain);

        JButton exit = new JButton("Quit!");
        exit.setBounds(220, 70, 204, 34);
        contentPane.add(exit);
    }

    //Get Winner
    public void getWinner(String player){       
        System.out.println("getWinner()");
        System.out.println("btnName: " + player);

        if(player.equals("X")){
            System.out.println("Player 1 won");
            whoWon.setText("Player 1 has won!");
            repaint();
            }
        else {
            System.out.println("Player 2 won");
            whoWon.setText("Player 2 has won!");
            repaint();
        }
    }


}

编辑:System.out.println()可以帮助我遵循程序的步骤

4

3 回答 3

0

您不能使用 . 检查字符串是否相等==。使用equals

if (player.equals("X"))

btn1.getText()==btn2.getText()也是不好的平等检查......

于 2013-11-02T21:15:16.410 回答
0

在标签更新之前在某处执行此操作,看看是否有帮助。如果标签没有更新,可能是因为它不可编辑/启用。

whoWon.setEditable(true);
于 2013-11-02T21:30:58.200 回答
0

我认为您的问题出在方法afterGame中。您为一个实例调用getWinner 方法,然后在下一行中创建一个具有空值的新实例,因为您的标签不会更改 text 。您会看到一个新的对象实例。

于 2013-11-02T22:48:56.807 回答