gpt4 book ai didi

java - 在运行时设置 JLabel 文本

转载 作者:行者123 更新时间:2023-11-29 06:08:59 25 4
gpt4 key购买 nike

我是 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);
}


}

以及试图设置标签的类。

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。在此先感谢,如果这是一个愚蠢的问题,我们深表歉意。我对编程非常陌生,我仍在努力接受这一切。话虽如此,任何其他建议都会很棒。谢谢!

最佳答案

传递引用以便类可以相互通信,不仅如此,还可以与其他类型的类的正确 Activity 实例通信。例如,您可以为 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)方法:

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 上调用的,它的本质(while (true))表明它很有可能锁定 EDT,从而使您的 Swing GUI 的图形处理和更新以及所有用户互动戛然而止。您可能需要为此使用 Swing 计时器或后台线程,以便让 EDT 自由执行其必要的工作。

关于java - 在运行时设置 JLabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com