【发布时间】:2017-11-30 19:00:06
【问题描述】:
我根据要求编辑了问题
所以我想要一个标签在按下按钮时更改它的文本。但是,当我在单击按钮时尝试在标签上使用 setText() 时,它无法识别它。这是为什么呢?
public class SingleButton extends JPanel
implements ActionListener {
protected JButton b1;
public SingleButton() {
b1 = new JButton("Axxxxxx"/*, leftButtonIcon*/);
b1.setActionCommand("enableb1");
b1.setEnabled(true);
b1.addActionListener(this);
add(labelUpn);
labelUpn.setText("UPN number here");
}
public void actionPerformed(ActionEvent e) {
if ("enableb1".equals(e.getActionCommand())) {
b1.setEnabled(false);
labelUpn.setText("New Text");
}
}
【问题讨论】:
-
Minimal - 使用尽可能少的代码,但仍然会产生同样的问题。如果我不向您展示所有代码,您将无法重现问题。
-
只用 1 标签和 1 按钮编写相同类型的代码,别无其他。如果它重现了问题 - 你得到了你的 MVCE,如果没有 - 与你的真实代码进行比较并找出差异。
-
发布minimal reproducible example。我们不会看 github 项目
标签: java jframe jbutton jlabel actionevent