在运行时设置JLabel文本

时间:2011-10-06 02:27:41

标签: java swing jlabel concurrency

我是Java的新手(约2周),我正在尝试设置JLabel的文本。唯一的问题是我在另一个类中进行计算,我不知道如何引用我已经创建的Jlabel。以下是这两个课程。

package fightsim;

import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class FightSimPane extends JPanel {
FightManager FightManager = new FightManager();
/**
 * Create the panel.
 */
public FightSimPane() {
    setLayout(new MigLayout("", "[][][][][][][][][][]", "[][][][]"));

    JLabel lblChampionleft = new JLabel("ChampionLeft");
    add(lblChampionleft, "cell 1 3");

    JButton btnGo = new JButton("Go");
    btnGo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            FightManager.startFight();
            FightManager.runTheFight();

        }
    });
    add(btnGo, "cell 5 3");

    JLabel lblChampionright = new JLabel("ChampionRight");
    add(lblChampionright, "cell 9 3");

}


public void setLeftChampionLabel(String s){
    //not able to reference Jlabel lblChampionLeft here???
    System.out.println("Setting Left Champion text to"+s);

}

public void setRightChampionLabel(String s){
    //not able to reference Jlabel lblChampionRight here???
    System.out.println("Setting Right Champion text to"+s);
}


}

正在尝试设置Label的类。

package fightsim;

public class FightManager {

Champion LeftChamp = new Champion();
Champion RightChamp = new Champion();

public FightManager() {

}

Thread LeftChampThread = new Thread(LeftChamp);
Thread RightChampThread = new Thread(RightChamp);

;

public void startFight() {

    LeftChamp.setHealth(200);
    RightChamp.setHealth(300);
    LeftChamp.setATKsp(1000);
    RightChamp.setATKsp(1000);
    LeftChamp.setAD(20);
    RightChamp.setAD(20);

}

public void runTheFight() {
    System.out.println("Starting Threads");
    LeftChampThread.start();
    RightChampThread.start();

    while ((LeftChamp.getHealth() > 0) && (RightChamp.getHealth() > 0)) {

        if (RightChamp.isReadyToAttack()) {
            LeftChamp.setHealth(LeftChamp.getHealth() -     RightChamp.getAD());
            RightChamp.setNotReady();
            System.out.println("Setting Left Champion test to"
                    + Integer.toString(LeftChamp.getHealth()));

            // This is where I'd like to update the left Jlabel in
            // FightSimPane

        }
        if (LeftChamp.isReadyToAttack()) {
            RightChamp
                    .setHealth(RightChamp.getHealth() - LeftChamp.getAD());
            LeftChamp.setNotReady();
            System.out.println("Setting Right Champion test to"
                    + Integer.toString(RightChamp.getHealth()));

            // This is where I'd like to update the right Jlabel in
            // FightSimPane
        }

    }

}
}

所以,问题是......如何让我的FightManager类访问并更改我的FightSimPane类/ Gui中的JLabel。在此先感谢,如果这是一个愚蠢的问题,对不起。我对编程非常陌生,我仍然试图全力以赴。有了这样说,任何其他的建议都会很棒。谢谢!

2 个答案:

答案 0 :(得分:2)

传递引用,以便类可以相互通信,而不仅仅是与其他类型的类的正确活动实例通信。例如,您可以为FlightManager提供FlightSimPane字段:

class FightManager {

   private FightSimPane fightSimPane;

   // and fill it in the constructor:
   public FightManager(FightSimPane fightSimPane) {
      this.fightSimPane = fightSimPane;
   }

然后,您将处理实际可视化的FightSimPane GUI对象。

请注意,您必须注意传递正确的实例:

public class FightSimPane extends JPanel {
   FightManager FightManager = new FightManager(this);

然后你可以在FightManager类中调用FightSimPane的公共方法:

public void runTheFight() {
  System.out.println("Starting Threads");
  LeftChampThread.start();
  RightChampThread.start();

  while ((LeftChamp.getHealth() > 0) && (RightChamp.getHealth() > 0)) {

     if (RightChamp.isReadyToAttack()) {
        LeftChamp.setHealth(LeftChamp.getHealth() - RightChamp.getAD());
        RightChamp.setNotReady();
        System.out.println("Setting Left Champion test to"
              + Integer.toString(LeftChamp.getHealth()));

        // !!!  **** added this *************
        fightSimPane.setRightChampionLabel("Setting Left Champion test to"
              + Integer.toString(LeftChamp.getHealth()));

     }

编辑1
我在这里看到另一个可能严重且无关的问题:

  while ((LeftChamp.getHealth() > 0) && (RightChamp.getHealth() > 0)) {

     //.........

  }

这个代码似乎是在主Swing线程上调用的,EDT和它的性质(而(true))表明它很有可能锁定EDT带来你的Swing GUI' s图形处理和更新以及所有用户交互到尖叫停止。您可能需要为此或后台线程使用Swing Timer,以便让EDT自由地完成必要的工作。

答案 1 :(得分:1)

FightSimPane类中声明FightManager类的引用变量,并通过FightSimPane的构造函数传递FightManager对象的引用。

在FightManager类中,

public class FightManager {

Champion LeftChamp = new Champion();
Champion RightChamp = new Champion();
private FightSimPane pane;

public FightManager(FightSimPane pane) { this.pane=pane;}
public FightManager() {
}
....

使用“窗格”引用变量,您可以访问FightSimPane类的可访问元素。

修改FightSimPane代码

public class FightSimPane extends JPanel {
FightManager fightManager;

public FightSimPane() {
     fightManager= new FightManager(this);
     ...
}