gpt4 book ai didi

java - 无法从 JavaFX 操作标签

转载 作者:行者123 更新时间:2023-11-30 11:24:56 24 4
gpt4 key购买 nike

每次用户转到该页面时,我都试图更改 JavaFX 软件 GUI 标签的文本。为此,我必须能够更改该标签的值,例如将其解析为变量并执行这些操作。

问题是,由于标签都是用FXML写的,我不知道如何在Java上正确使用它。我试过:

@FXML public Label WordID;

为了使标签(以“WordID”作为 ID)成为对象,但我失败了。当我这样做时,程序将不再转到该帧。我该怎么办?

代码:

public class Screen3Controller implements Initializable, ControlledScreen {

ScreensController myController;

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

public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}

@FXML
private void goToScreen1(ActionEvent event){
myController.setScreen(ScreensFramework.screen1ID);
}

@FXML
private void goToScreen2(ActionEvent event){
myController.setScreen(ScreensFramework.screen2ID);
}

}

FXML:

<Label fx:id="WordID" layoutX="393.0" layoutY="148.0" prefWidth="177.0" text="">
<font>
<Font name="Avenir Roman" size="40.0" />
</font>
</Label>

来自 Screen2 的按钮监听器:

@FXML
private void goToScreen3(ActionEvent event){
myController.setScreen(ScreensFramework.screen3ID);
WordID.setText("LOl");
}

错误信息:

Glass detected outstanding Java exception at -[GlassViewDelegate sendJavaMouseEvent:]:src/com/sun/mat/ui/GlassViewDelegate.m:543 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: ... com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173) at java.security.AccessController.doPrivileged(Native Method) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1444) ... 40 more Caused by: java.lang.NullPointerException at screensframework.Screen3Controller.mostrarPalavra(Screen3Controller.java:89) ... 50 more

最佳答案

要以编程方式使用 Label,您需要做的就是在 Controller 文件中声明一个变量,其 id 与在您的 中声明的相同.fxml 文件:

你的FXML.fxml

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.sample.Screen3Controller ">
...
<Label fx:id="WordID" layoutX="393.0" layoutY="148.0" prefWidth="177.0" text="">
<font>
<Font name="Avenir Roman" size="40.0" />
</font>
</Label>
...
</AnchorPane>

com.sample.Screen3Controller 必须引用 com.sample 包中的现有 .java 文件。

Screen3Controller.java

import javafx.scene.control.Label;  //make sure you're importing the JavaFX Label

public class Screen3Controller implements Initializable, ControlledScreen {

ScreensController myController;
@FXML private Label WordID;

...

WordID.setText("some text...");

...
}

关于java - 无法从 JavaFX 操作标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501999/

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