Java中setText后JLabel不会改变

发布于 2024-12-09 19:41:23 字数 1241 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸠书 2024-12-16 19:41:23

我查看了你们中的一些代码,我的第一个担心(除了过度使用静态变量之外)是您不恰当地使用继承,因此在错误的引用上调用方法。

许多类继承自 Spel,但似乎它们不应该这样做。例如,您的 Dobbelsteen 类继承自 Spel,但它也有一个单独的 Spel 实例 - 为什么?运行此代码时当前可见什么 Spel 对象?我怀疑这就是 Dobbelsteen 扩展的那个。因此,我认为您正在尝试更改 Dobbelsteen 类持有的 JLabel,但它不是当前可视化的“Spel”对象。要正确更改可视化 JLabel,您需要对保存它的当前可视化 Spel 对象的有效引用,并调用该类上适当的公共方法。

总之,您可能想要从头开始重写这个项目,目标是从视图(GUI)中分离出模型(数据),并着眼于良好的 OOP 原则。

编辑 1:

函数中将 Spel 引用传递给您,会怎么样(用 !! 注释指出的更改: //!!):

//!! public class Dobbelsteen extends Spel {
public class Dobbelsteen { //!!

   int dobbelsteen;
   int nieuwepositie;
   int nieuwepositie2;
   public static String newPos;
   public static String newPos2;
   int oudepositie;
   int oudepositie2;
   int huidigepositie = Spel.positie;
   // int huidigepositie2 = Spel.positie2;
   int aantalOgen = Spel.aantalogen;
   int aantalOgen2 = Spel.aantalogen2;
   static boolean heeftgewonnen = false;

   // !! Spel spiel = new Spel();
   Spel spiel; // !!

   // !!
   public Dobbelsteen(Spel spiel) {
      this.spiel = spiel;
   }

   public void aantalOgen(int aantalogen) {
      oudepositie = huidigepositie;
      nieuwepositie = (huidigepositie + aantalOgen);
      if (nieuwepositie == Spel.eindronde) { //!!
         System.out.println("Speler Piet heeft de ronde gewonnen!");
         spiel.updateUI(); //!! ****** here in particular ******
      } else if (nieuwepositie > Spel.eindronde) {
         Spel.positie = huidigepositie; //!!
         spiel.output.setText("Je hebt teveel gegooid"); //!!
         spiel.output.setForeground(Color.red); //!!
      } else {
         Spel.oudpositie = oudepositie; //!!
         Spel.positie = nieuwepositie; //!!
         newPos = String.valueOf(nieuwepositie);
         if (SpelHost.host) {
            SpelHost.verstuurPositie("Positie" + newPos);
         } else if (SpelClient.client) {
            SpelClient.verstuurPositie("Positie" + newPos);
         }
      }

   }
}

这可能只是一个创可贴,但是如果您在 Dobbelsteen 的构造 像这样称呼它:

class GooiDobbelsteen extends MouseAdapter {

   @Override
   public void mouseClicked(MouseEvent e) {
      aanBeurt = false;
      dobbelsteen = new Random();
      aantalogen = dobbelsteen.nextInt(6) + 1;
      aantalOog = String.valueOf(aantalogen);
      Dobbelsteen dobbel = new Dobbelsteen(Spel.this); // !!
      dobbel.aantalOgen(aantalogen);

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: //!!):

//!! public class Dobbelsteen extends Spel {
public class Dobbelsteen { //!!

   int dobbelsteen;
   int nieuwepositie;
   int nieuwepositie2;
   public static String newPos;
   public static String newPos2;
   int oudepositie;
   int oudepositie2;
   int huidigepositie = Spel.positie;
   // int huidigepositie2 = Spel.positie2;
   int aantalOgen = Spel.aantalogen;
   int aantalOgen2 = Spel.aantalogen2;
   static boolean heeftgewonnen = false;

   // !! Spel spiel = new Spel();
   Spel spiel; // !!

   // !!
   public Dobbelsteen(Spel spiel) {
      this.spiel = spiel;
   }

   public void aantalOgen(int aantalogen) {
      oudepositie = huidigepositie;
      nieuwepositie = (huidigepositie + aantalOgen);
      if (nieuwepositie == Spel.eindronde) { //!!
         System.out.println("Speler Piet heeft de ronde gewonnen!");
         spiel.updateUI(); //!! ****** here in particular ******
      } else if (nieuwepositie > Spel.eindronde) {
         Spel.positie = huidigepositie; //!!
         spiel.output.setText("Je hebt teveel gegooid"); //!!
         spiel.output.setForeground(Color.red); //!!
      } else {
         Spel.oudpositie = oudepositie; //!!
         Spel.positie = nieuwepositie; //!!
         newPos = String.valueOf(nieuwepositie);
         if (SpelHost.host) {
            SpelHost.verstuurPositie("Positie" + newPos);
         } else if (SpelClient.client) {
            SpelClient.verstuurPositie("Positie" + newPos);
         }
      }

   }
}

And call it like so:

class GooiDobbelsteen extends MouseAdapter {

   @Override
   public void mouseClicked(MouseEvent e) {
      aanBeurt = false;
      dobbelsteen = new Random();
      aantalogen = dobbelsteen.nextInt(6) + 1;
      aantalOog = String.valueOf(aantalogen);
      Dobbelsteen dobbel = new Dobbelsteen(Spel.this); // !!
      dobbel.aantalOgen(aantalogen);
時窥 2024-12-16 19:41:23

使用
公共无效更新UI(){
SwingUtilities.invokeLater(new Runnable() {
公共无效运行(){
ikWin = 真;
而(ikWin){
mijnScore = mijnScore+1;
SwingUtilities.invokeLater(new Runnable()
{

                @Override
                public void run()
                {
                    Scoresp1.setText(mijnScore + "");

                }
            });

               System.out.println("mijnScore" + mijnScore);
               ikWin = false;
               positie = 0;
               }
          }

      });
   }

进行测试

use
public void updateUI() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ikWin = true;
while(ikWin){
mijnScore = mijnScore+1;
SwingUtilities.invokeLater(new Runnable()
{

                @Override
                public void run()
                {
                    Scoresp1.setText(mijnScore + "");

                }
            });

               System.out.println("mijnScore" + mijnScore);
               ikWin = false;
               positie = 0;
               }
          }

      });
   }

to have a test

决绝 2024-12-16 19:41:23

Scoresp1.repaint() 添加到 while 循环的末尾。

Add a Scoresp1.repaint() to the end of your while loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文