在Java Swing中更改按钮文本时遇到问题。

huangapple go评论65阅读模式
英文:

Having issues changing button text in Java Swing

问题

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

public class TCTBack implements ActionListener {

    JFrame frame;
    JPanel panel;
    JLabel label1;
    JButton button1;
    String turn;

    public TCTBack() {

        turn = "-";

        panel = new JPanel();
        panel.setSize(500, 500);

        frame = new JFrame();
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);

        panel.setLayout(null);

        label1 = new JLabel("交互式按钮测试");
        label1.setBounds(10, 20, 80, 25);
        panel.add(label1);

        button1 = new JButton("按钮");
        button1.setBounds(10, 80, 80, 25);
        button1.addActionListener(this);
        panel.add(button1);

        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        switch (turn) {
            case "-":
                turn = "X";
                button1.setText("X");
                break;
            case "X":
                turn = "O";
                button1.setText("O");
                break;
            case "O":
                turn = "-";
                button1.setText("-");
                break;
        }

    }

    public static void main(String[] args) {

        new TCTBack();

    }

}
英文:

I'm trying to test ways to change a button's text via an actionlistener. Here's my code:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TCTBack implements ActionListener {
JFrame frame;
JPanel panel;
JLabel label1;
JButton button1;
String turn;
public TCTBack() {
turn = "-";
panel = new JPanel();
panel.setSize(500, 500);
frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
label1 = new JLabel("Interactive Button Test");
label1.setBounds(10,20,80,25);
panel.add(label1);
button1 = new JButton("Button");
button1.setBounds(10,80,80,25);
button1.addActionListener(this);
panel.add(button1);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
switch (turn) {
case "-" -> {
turn = "X";
button1.setText("X");
}
case "x" -> {
turn = "O";
button1.setText("O");
}
case "O" -> {
turn = "-";
button1.setText("-");
}
}
}
public static void main(String[] args) {
new TCTBack();
}
}

It changes the button's text once, but won't change it after I click the button again. My goal is to be able to change the button's text from the default value("-") to X, then to O, then back to -. Any help would be appreciated, thanks. Also, I'm a newbie at asking questions so if I'm missing any information then just let me know.

答案1

得分: 2

因为你将文本更改为 X,但你检查它是否等于 x

case "X" -> {
...
}
英文:

Because you change the text to X but you check if it equals x

case "X" -> {
...
}

答案2

得分: 0

turn = "X"

case "x"

其中一个是大写的 X,另一个不是。

提示:如果您认为 switch 是“穷尽的”(case 语句应始终匹配),请添加一个带有 throw new IllegalStateException(); 或类似内容的 default。或者将 switch 用作表达式,这种情况下,Java 会自动为您提供该功能。

英文:

turn = "X"

case "x"

One is a capital X, and one is not.

Tip: If you think a switch is 'exhaustive' (a case statement should always be matched), add a default with throw new IllegalStateException(); or some such. Or use the switch as an expression, in which case java will give you that for free.

答案3

得分: 0

定义常量。使用常量可以防止像您这样的错误。以下是我建议您更改的代码部分以及更改建议。代码之后是解释。

public class TCTBack implements ActionListener {
    private static final String NONE = "-";
    private static final String O = "O";
    private static final String X = "X";

    public TCTBack() {
        button1 = new JButton(NONE);
    }

    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        String newVal;
        switch (actionCommand) {
            case NONE:
                newVal = X;
                break;
            case O:
                newVal = NONE;
                break;
            case X:
                newVal = O;
                break;
            default:
                newVal = null;
                JOPtionPane.showMessageDialog(frame,
                                              actionCommand,
                                              "Unhandled",
                                              JOptionPane.WARNING_MESSAGE);
        }
        if (newVal != null) {
            button1.setActionCommand(newVal);
            button1.setText(newVal);
        }
    }
}

当您使用接受单个String参数的构造函数创建JButton时,您会同时设置按钮的文本和其操作命令。传递给actionPerformed()方法的ActionEvent对象包含此操作命令字符串。

然而,调用JButton类的setText()方法不会更改操作命令,因此您需要通过setActionCommand()方法更新它。

英文:

Define constants. Using constants prevents errors like yours.
Here are the parts of your code that I suggest you change with proposed changes. Explanations after the code.

public class TCTBack implements ActionListener {
    private static final String NONE = "-";
    private static final String O = "O";
    private static final String X = "X";

    public TCTBack() {
        button1 = new JButton(NONE);
    }

    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        String newVal;
        switch (actionCommand) {
            case NONE:
                newVal = X;
                break;
            case O:
                newVal = NONE;
                break;
            case X:
                newVal = O;
                break;
            default:
                newVal = null;
                JOPtionPane.showMessageDialog(frame,
                                              actionCommand,
                                              "Unhandled",
                                              JOptionPane.WARNING_MESSAGE);
        }
        if (newVal != null) {
            button1.setActionCommand(newVal);
            button1.setText(newVal);
        }
    }
}

When you create a JButton using the constructor that accepts a single, String argument, you set both the button's text and its action command. The ActionEvent object passed to method actionPerformed() contins this action command string.

However, calling method setText() of class JButton does not change the action command, hence you need to also update that via method setActionCommand().

huangapple
  • 本文由 发表于 2020年10月22日 20:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64482727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定