2023-02-16 348
嗨,第一次来这里,但这里有:
我有一个JAVAFX应用程序,该应用程序动态更改FXML UI标签,并从播放器类中删除数据.
所讨论的两个类是Player.java和InterfaceHandler.java.
玩家类存储播放器详细信息,我想将详细信息传递给界面类,该类别设置了标签上的文本.
作为测试,我的FXML UI只有一个按钮和两个标签.
如果它单击了该按钮,则它调用handleButton方法,它将locationLabel设置为”镇”.
但是,如果我在播放器类中调用locationLabel()方法,则在调用nameLabel.setText(name)时将获得NullPoInterException.通过调试,我发现接口类中的名称字符串应该是” dan”.
任何人可以帮忙吗?
主类:
public class Main extends Application {
public void start(final Stage mainStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
Scene scene = new Scene(root);
mainStage.setTitle("Main Screen");
mainStage.setScene(scene);
mainStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
玩家类:
public class Player{
InterfaceHandler ui = new InterfaceHandler();
public void setNameLabel() {
String name = "Dan";
ui.setName(name);
}
interfaceHandler类:
public class InterfaceHandler implements Initializable {
public Label nameLabel;
public Label locationLabel;
public void handleButton(ActionEvent event) throws IOException {
locationLabel.setText("Town");
}
public void setName(String name){
nameLabel.setText(name);
}
}
mainscreen.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="629.0" prefWidth="600.0" snapToPixel="true" style="-fx-background-color: beige;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.InterfaceHandler">
<children>
<Button fx:id="button1" layoutX="512.0" layoutY="381.0" minWidth="14.0" mnemonicParsing="false" onAction="#handleButton" prefHeight="30.0" prefWidth="51.0" text="Town" visible="true" />
<Label fx:id="nameLabel" layoutX="57.0" layoutY="8.0" prefWidth="216.0" text="blank" />
<Label fx:id="locationLabel" layoutX="68.0" layoutY="27.0" prefWidth="193.0" text="blank" />
</children>
</AnchorPane>
这是因为您没有从fxml文件中正确注入Labels.
注释您的Label vars.带有FXML注释:
public class InterfaceHandler implements Initializable {
@FXML
public Label nameLabel;
@FXML
public Label locationLabel;
public void handleButton(ActionEvent event) throws IOException {
locationLabel.setText("Town");
}
public void setName(String name){
nameLabel.setText(name);
}
}
另外,InterfaceHandler是一个控制器,您可以在FXML中使用fx:controller引用.这指示FXMLLOADER加载其FXML时创建接口手的新实例.因此,不要在玩家类中创建一个新的interfaceHandler,而是将InterfaceHandler成为播放器的构造函数参数,并使用loader.getController将interfaceHandler Controller实例从fxmlloader中获取.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("MainScreen.fxml"));
Parent root = (Parent)loader.load();
Player player = new Player(loader.getController());
Scene scene = new Scene(root);
. . .
public class Player {
private InterfaceHandler ui;
public Player(InterfaceHandler ui) {
this.ui = ui;
}
public void setNameLabel() {
String name = "Dan";
ui.setName(name);
}
}
我也有类似的问题,但事实证明我已经将标签作为静态属性,当我删除静态部分时,它已修复了nullpointer exeption.
以上所述是小编给大家介绍的标签setText NullPointerException,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/34030.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态