简体   繁体   English

在ActionEvent中更改JLabel的文本

[英]Changing text of JLabel in ActionEvent

OK, so I'm creating a Tic Tac Toe GUI. OK,所以我要创建一个Tic Tac Toe GUI。 I currently having a working frame of 3x3 buttons that will change to either an X or O. My issue is implementing a JLabel on the top of the buttons. 我目前有一个3x3按钮的工作框架,该框架将变为X或O。我的问题是在按钮顶部实现JLabel。 The label keeps track of who's turn it is and will change from either "O's turn" or "X's turn" however I can't seem to get the JLabel to update (only spouts errors). 标签会跟踪其轮到谁,并且会从“ O轮”或“ X轮”更改,但是我似乎无法让JLabel更新(仅喷口错误)。 Here's a portion of my code showing how Trying to implement this. 这是我的代码的一部分,显示了如何尝试实现此目标。 The program runs fine but as soon as I add the (turn.setText("O's turn");) I get errors. 该程序运行正常,但是一旦添加(turn.setText(“ O's turn”);),我就会收到错误消息。

Any feedback as to why this may not be working would be appreciated. 任何关于为什么这可能不起作用的反馈将不胜感激。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TicTacToe extends JFrame {

    XOButton[] xob = new XOButton[9];
    private JLabel turn;

    public static int state;

    public TicTacToe() {
        super("TicTacToe");

        //add XOButtons to panel
        JPanel center = new JPanel();
        state = 0;

        JLabel turn = new JLabel("X's turn");

        center.setLayout(new GridLayout(3, 3, 2, 2));
        //action listener
        ButtonListener bl = new ButtonListener();
        for (int i = 0; i < 9; i++) {
            xob[i] = new XOButton();
            xob[i].addActionListener(bl);
            center.add(xob[i]);
        }

        //add panel to frame
        setLayout(new BorderLayout());
        add(center, BorderLayout.CENTER);
        add(turn, BorderLayout.NORTH);

    }



    //inner action listener class
    private class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            for (int i = 0; i < 9; i++) {
                if(ae.getSource() == xob[i] && xob[i].getState() == XOButton.NONE && state == 0) {
                    xob[i].setState(XOButton.X);
                    state = 1;
                    //turn.setText("O's turn");

                } else if(ae.getSource() == xob[i] && xob[i].getState() == XOButton.NONE && state == 1) {
                    xob[i].setState(XOButton.O);
                    state = 0;
                }
            }
        }
    }
}

Guess: your error is a NullPointerException because turn is null. 猜猜:您的错误是NullPointerException,因为turn为null。 Solution: don't shadow the variable. 解决方案:不要隐藏变量。 In other words you're re-declaring the turn variable in the constructor leaving the field null. 换句话说,您要在构造函数中重新声明turn变量,而将字段保留为空。

eg, 例如,

public class TicTacToe extends JFrame{
XOButton[] xob = new XOButton[9];
private JLabel turn;

public static int state;

public TicTacToe() {
    super("TicTacToe");

    //add XOButtons to panel
    JPanel center = new JPanel();
    state = 0;

    JLabel turn = new JLabel("X's turn");  // **** you're re-declaring turn here!

Better: 更好:

public class TicTacToe extends JFrame{
XOButton[] xob = new XOButton[9];
private JLabel turn;

public static int state;

public TicTacToe() {
    super("TicTacToe");

    //add XOButtons to panel
    JPanel center = new JPanel();
    state = 0;

    turn = new JLabel("X's turn");  // **** Note the difference?

In the future, if you have similar problems, please post the entire error message and indicate which line throws the exception as it will help us immensely! 将来,如果您遇到类似的问题,请张贴完整的错误消息,并指出哪一行引发异常,因为它将极大地帮助我们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM