java 更改标签文本 JavaFX FXML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45110461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:30:28  来源:igfitidea点击:

Changing Label text JavaFX FXML

javajavafxfxml

提问by M. Barabas

There is my main class:

有我的主要课程:

public class Main extends Application {
private static Stage primaryStage;
public static BorderPane mainLayout;

@Override
public void start(Stage primaryStage) {
    this.setPrimaryStage(primaryStage);
    primaryStage.setTitle("Project");

    try {
        mainLayout = 
        FXMLLoader.load(Main.class.getResource("/main/view/MainPage.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(mainLayout);
        primaryStage.setScene(scene);
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {

            @Override
            public void handle(WindowEvent event) {
                System.exit(0);
            }
        });
        primaryStage.show();
    }

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

    public static Stage getPrimaryStage() {
        return primaryStage;
    }

    public void setPrimaryStage(Stage primaryStage) {
         Main.primaryStage = primaryStage;
    }
}

This FXML of my window:

我的窗口的这个 FXML:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<BorderPane prefHeight="410.0" prefWidth="512.0" 
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="main.controller.MainController">
 <center>
  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" 
  spacing="20.0" BorderPane.alignment="CENTER">
     <children>
        <Label fx:id="aaa" prefHeight="72.0" prefWidth="336.0" 
text="Project" textAlignment="CENTER">
           <font>
              <Font name="Century Gothic" size="25.0" />
           </font>
        </Label>
     </children>
     <padding>
        <Insets bottom="30.0" />
     </padding>
   </VBox>
 </center>
</BorderPane>

This is Controller for this FXML:

这是此 FXML 的控制器:

public class MainController {
@FXML
private static Label aaa;

@FXML
public static void initialize(){
    aaa.setText("AHOJ");

    }
} 

I want to call method initialize() from another class ten times like this:

我想像这样从另一个类调用方法 initialize() 十次:

public class MyClass {
public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        MainController.initialize();
    }
}
}

But there is NullPointerException. Can someone help me?

但是有 NullPointerException。有人能帮我吗?

回答by Dmytro Maslenko

Just remove statics for field and method, then run your application by main() in Main class:

只需删除字段和方法的静态,然后在 Main 类中通过 main() 运行您的应用程序:

public class MainController {
    @FXML
    private Label aaa;

    @FXML
    public void initialize(){
        aaa.setText("AHOJ");
    }
}