简体   繁体   English

Java中的setText之后,JLabel不会更改

[英]JLabel won't change after setText in Java

I got a JLabel Scoresp1 which I want to change using Scoresp1.setText(mijnScore + ""); 我得到了一个JLabel Scoresp1 ,我想使用Scoresp1.setText(mijnScore + "");进行更改Scoresp1.setText(mijnScore + ""); . But the text on the JLabel stays the same. 但是JLabel上的文本保持不变。

I got a class Dobbelsteen which looks like this: 我有一个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();
        }
    }
}

Which calls updateUI which is in the class Spel 哪个调用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 is declared as public JLabel Scoresp1; Scoresp1被声明为public JLabel Scoresp1; . If I use String l = Scoresp1.getText(); 如果我使用String l = Scoresp1.getText(); I get the right value, but the JLabel doesn't get updated visually. 我得到了正确的值,但是JLabel并没有得到可视化的更新。

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. 许多类都从Spel继承而来,但似乎并没有这样做。 For instance, your Dobbelsteen class inherits from Spel, and yet it also has a separate Spel instance -- why? 例如,您的Dobbelsteen类继承自Spel,但它也有一个单独的Spel实例-为什么? What Spel object is currently visible at the time this code is run? 运行此代码时当前可见哪些Spel对象? 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. 因此,我认为您正在尝试更改由Dobbelsteen类持有的JLabel,但当前可视化的不是“ Spel”对象。 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. 要正确更改可视化的JLabel,您需要有效引用当前保存的JLabel的Spel对象,并在该类上调用适当的public方法。

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. 总之,您可能希望从头开始重新编写该项目,以期从视图(GUI)中分离模型(数据),并着眼于良好的OOP原则。

Edit 1: 编辑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: //!!): 这可能只是一个创可贴,但是如果您在Dobbelsteen的构造函数中将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);
         }
      }

   }
}

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);

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

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

                }
            });

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

      });
   }

to have a test 进行测试

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

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

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