我在start方法中加载FXML1,当单击Button时,我打开FXML2。这是我的问题:当按下按钮时,我可以更改标签“标签”的文本吗?
主:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXMLExample extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXML1.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
控制器: 在initialize方法中是注释:label.setText(“Hello World”); 如果我取消注释并尝试它,我会得到一个例外。
package fxmlexample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
/**
* FXML Controller class
*/
public class FXMLController implements Initializable {
@FXML
Button button;
@FXML
Label label;
public void pressButton(ActionEvent event) throws Exception {
if (event.getSource() == button) {
Parent root = FXMLLoader.load(getClass().getResource("FXML2.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
}
/**
* Initializes the controller class.
*
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// label.setText("Hello World");
}
}
FXML1:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmlexample.FXMLController">
<children>
<Button fx:id="button" layoutX="264.0" layoutY="185.0" mnemonicParsing="false" onAction="#pressButton" prefHeight="31.0" prefWidth="73.0" text="Click me!" />
</children>
</Pane>
FXML2:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<Pane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmlexample.FXMLController">
<children>
<Label fx:id="label" layoutX="244.0" layoutY="177.0" prefHeight="46.0" prefWidth="112.0" style="-fx-background-color: lime;" />
</children>
</Pane>
答案 0 :(得分:0)
label
仅在FXML2的控制器中初始化(因为该FXML文件具有fx:id="label"
的元素)。在两个控制器上都调用initialize()
方法。因此,当在FXML1的控制器上调用initialize()
时,label
为空并且您获得空指针异常。
为每个FXML文件使用不同的控制器类:
package fxmlexample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
/**
* FXML Controller class
*/
public class FXMLController1 implements Initializable {
@FXML
Button button;
public void pressButton(ActionEvent event) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXML2.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
/**
* Initializes the controller class.
*
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
和
package fxmlexample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
/**
* FXML Controller class
*/
public class FXMLController2 implements Initializable {
@FXML
Label label;
/**
* Initializes the controller class.
*
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
label.setText("Hello World");
}
}
最后,修改每个FXML文件中的fx:controller
属性以指向正确的类。