我是新来的JavaFX,有一些问题。
假设我有两个fxml文件,以及相应的控制器类。每个fxml都有一个按钮,它应该打开另一个屏幕并传递一个参数。
有没有人能举个例子,说明谷歌是如何做到这一点的,谷歌没有任何帮助。
发布于 2015-10-17 05:06:51
这里的“屏幕”是指JavaFX Stage实例,对吧?如果是这样,那就相当简单了:
唯一有点不寻常的事情是获取控制器引用。您需要创建一个FXMLoader实例。它不适用于通常称为静态方法的方法:
(应用程序的主类)
public class MyFXMLApp extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("MainForm.fxml"));
Parent root = (Parent) loader.load();
// as soon as the load() method has been invoked, the scene graph and the
// controller instance are availlable:
MainFormController controller = loader.getController();
controller.setText("Ready.");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
// ...(控制器)
public class MainFormController implements Initializable {
// some ui control:
@FXML
private Label label;
// JavaFX property for values that shall be accessible from outside:
private final StringProperty text = new SimpleStringProperty();
public String getText() {
return text.get();
}
public void setText(String value) {
text.set(value);
}
public StringProperty textProperty() {
return text;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("MainFormController.initialize");
this.label.textProperty().bind(this.text);
}
// ...该示例使用JavaFX属性在控制器中保存“参数”-因此,可以很容易地观察到该值及其更改,并且该属性可以绑定到任何其他字符串属性。
发布于 2017-03-24 10:54:17
@FXML
public void handleAddPartAction(ActionEvent event) throws IOException {
Stage stage;
Parent root;
//get reference to the button's stage
stage=(Stage) partAddButton.getScene().getWindow();
//load up OTHER FXML document
root = FXMLLoader.load(getClass().getResource("AddPart.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();https://stackoverflow.com/questions/33166012
复制相似问题