gpt4 book ai didi

java - 更新扩展 Task 的类中的文本

转载 作者:行者123 更新时间:2023-12-02 03:23:33 26 4
gpt4 key购买 nike

以下 JavaFx 程序应该在按下按钮后获取计算机上的当前 IP 地址并将其显示在屏幕上。我已经完成了所有工作,但在更新扩展任务的类中的文本时遇到问题

MainApp.java

public class MainApp extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));

Scene scene = new Scene(root);
scene.getStylesheets().add("/styles/Styles.css");

stage.setAlwaysOnTop(true);
stage.setTitle("IP Viewer");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}

我的 FXMLController

public class FXMLController implements Initializable {

@FXML
private void handleButtonAction(ActionEvent event) {

Task<Void> task = new IpTask();;

Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();

}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}

IpTask:它获取当前 IP 地址并尝试更新 UI,但我得到 java.lang.NullPointerException textIp.setText 上的异常

public class IpTask extends Task<Void> {

@FXML
private Text textIp;

@Override
protected Void call() throws Exception {
Common common = new Common();
Ip ip = common.getIp();
System.out.println("You clicked me!");
return null;
}

@Override
protected void failed() {
System.out.println("Failed!");
}

@Override
protected void succeeded() {
System.out.println("Updated");
Platform.runLater(new Runnable() {
@Override
public void run() {
textIp.setText("JUST TO SEE IF THE TEXT GETS SET");
}
});
}
}

下面是我的 Scene.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="com.limitedenterprisesinc.ipviewer.FXMLController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<!-- <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" /> -->
<javafx.scene.text.Text layoutX="126" layoutY="140" fx:id="textIp"/>
</children>
</AnchorPane>

如何从 IpTask 类更新 textIp 文本字段?

最佳答案

字段被注入(inject)到与 FXMLLoader 一起使用的 Controller 实例中。 IpTask 不用作 Controller ,因此不会向该字段注入(inject)任何内容。

将该字段注入(inject) Controller 并将其传递给IpTask。此外,您不必在 succeeded 方法中使用 Platform.runLater,因为它是在 JavaFX 应用程序线程上调用的。

public class FXMLController implements Initializable {

@FXML
private Text textIp;

@FXML
private void handleButtonAction(ActionEvent event) {
Task<Void> task = new IpTask(textIp);

Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}

...
}
public class IpTask extends Task<Void> {

private final Text textIp;

public IpTask(Text textIp) {
if (textIp == null) {
throw new IllegalArgumentException();
}
this.textIp = textIp;
}

...

@Override
protected void succeeded() {
System.out.println("Updated");
textIp.setText("JUST TO SEE IF THE TEXT GETS SET");
}
}

关于java - 更新扩展 Task 的类中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39303575/

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