Java中setText后JLabel不会改变
我有一个 JLabel Scoresp1
,我想使用 Scoresp1.setText(mijnScore + "");
对其进行更改。但 JLabel 上的文本保持不变。
我有一个类 Dobbelsteen
,如下所示:
public class Dobbelsteen extends Spel {
...
public void aantalOgen(int aantalogen) {
oudepositie = huidigepositie;
nieuwepositie = (huidigepositie + aantalOgen);
if (nieuwepositie == eindronde) {
System.out.println("Speler Piet heeft de ronde gewonnen!");
updateUI();
}
}
}
它调用类 Spel
中的 updateUI
public class Spel {
...
public void updateUI() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ikWin = true;
while(ikWin){
mijnScore = mijnScore+1;
Scoresp1.setText(mijnScore + "");
System.out.println("mijnScore" + mijnScore);
ikWin = false;
positie = 0;
}
}
});
}
...
}
Scoresp1
被声明为 public JLabel Scoresp1 ;
。如果我使用 String l = Scoresp1.getText(); 我会得到正确的值,但 JLabel 不会在视觉上得到更新。
I got a JLabel Scoresp1
which I want to change using Scoresp1.setText(mijnScore + "");
. But the text on the JLabel stays the same.
I got a class Dobbelsteen
which looks like this:
public class Dobbelsteen extends Spel {
...
public void aantalOgen(int aantalogen) {
oudepositie = huidigepositie;
nieuwepositie = (huidigepositie + aantalOgen);
if (nieuwepositie == eindronde) {
System.out.println("Speler Piet heeft de ronde gewonnen!");
updateUI();
}
}
}
Which calls updateUI which is in the class Spel
public class Spel {
...
public void updateUI() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ikWin = true;
while(ikWin){
mijnScore = mijnScore+1;
Scoresp1.setText(mijnScore + "");
System.out.println("mijnScore" + mijnScore);
ikWin = false;
positie = 0;
}
}
});
}
...
}
Scoresp1
is declared as public JLabel Scoresp1;
. If I use String l = Scoresp1.getText();
I get the right value, but the JLabel doesn't get updated visually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我查看了你们中的一些代码,我的第一个担心(除了过度使用静态变量之外)是您不恰当地使用继承,因此在错误的引用上调用方法。
许多类继承自 Spel,但似乎它们不应该这样做。例如,您的 Dobbelsteen 类继承自 Spel,但它也有一个单独的 Spel 实例 - 为什么?运行此代码时当前可见什么 Spel 对象?我怀疑这就是 Dobbelsteen 扩展的那个。因此,我认为您正在尝试更改 Dobbelsteen 类持有的 JLabel,但它不是当前可视化的“Spel”对象。要正确更改可视化 JLabel,您需要对保存它的当前可视化 Spel 对象的有效引用,并调用该类上适当的公共方法。
总之,您可能想要从头开始重写这个项目,目标是从视图(GUI)中分离出模型(数据),并着眼于良好的 OOP 原则。
编辑 1:
函数中将 Spel 引用传递给您,会怎么样(用 !! 注释指出的更改: //!!):
这可能只是一个创可贴,但是如果您在 Dobbelsteen 的构造 像这样称呼它:
I've looked at some of you code, and my first concern (other than an over-use of static variables) is that you're using inheritance inappropriately and because of this are calling methods on the wrong reference.
Many classes inherit from Spel but don't appear that they should be doing this. For instance, your Dobbelsteen class inherits from Spel, and yet it also has a separate Spel instance -- why? What Spel object is currently visible at the time this code is run? I doubt it is the one that Dobbelsteen extends. Because of this, I think that you're trying to changing the JLabel that is held by the the Dobbelsteen class, but it is not the "Spel" object that is currently visualized. To properly change the visualized JLabel, you'll need a valid reference to the currently visualized Spel object that holds it, and call the appropriate public method on that class.
In all, you might want to re-write this project from the ground up, with a goal of separating out your model (the data) from the view (the GUI), and with an eye towards good OOP principles.
Edit 1:
This may only be a bandaid, but what if you got your Spel reference passed to you in Dobbelsteen's constructor, something like this (changes noted with !! comments: //!!):
And call it like so:
使用
公共无效更新UI(){
SwingUtilities.invokeLater(new Runnable() {
公共无效运行(){
ikWin = 真;
而(ikWin){
mijnScore = mijnScore+1;
SwingUtilities.invokeLater(new Runnable()
{
进行测试
use
public void updateUI() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ikWin = true;
while(ikWin){
mijnScore = mijnScore+1;
SwingUtilities.invokeLater(new Runnable()
{
to have a test
将
Scoresp1.repaint()
添加到 while 循环的末尾。Add a
Scoresp1.repaint()
to the end of your while loop.