开发者

JLabel won't change after setText in Java

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:

pu开发者_JAVA技巧blic 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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜