将数据插入另一个 fxml 文件中的网格窗格
inserting a data to a gridpane in another fxml file
我的第一个 fxml(按钮):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.fxml_gridtest_controller" >
<Button fx:id="insertBut" text="insert" onMouseClicked="#insertData"/>
</StackPane>
我的第二个 fxml(网格窗格):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.fxml_gridtest_controller">
<top>
<GridPane fx:id="gpane">
</GridPane>
</top>
</BorderPane>
我的控制器:
package fxml_grid_test;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class fxml_gridtest_controller extends Application {
@FXML private Button insertBut;
@FXML private GridPane gpane;
@FXML private void insertData() throws IOException{
gpane.add(new Label("test"), 0, 0);
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("grid_fxml.fxml"));
Scene scene = new Scene(root, 500, 350);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void start(Stage primaryStage) throws Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
我的主要 :
package fxml_grid_test;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Fxml_grid_test extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("button_fxml.fxml"));
Scene scene = new Scene(root, 300, 275);
primaryStage.setTitle("insert data test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
想法是:
当我按下按钮时,我想将一些数据插入到我的网格窗格中,然后显示 fxml。
我的两个fxml共享控制器,我觉得这是问题的根源,但我不知道如何解决。
控制器编译 class 实现 "code behind" fxml 定义的对象层次结构。这些对于定义操作 UI 元素的方法很有用,并且可以在必要时由其他 classes 调用。
在您的情况下,我们需要为 grid.fxml
定义一个控制器,因为我们要创建一个方法,该方法将负责向 grid.This 添加元素,方法将在 MainController
,它负责加载 grid.fxml
并更新它。
FXMLLoader 帮助我们获取在 fxml 中引用的控制器实例。
因此,您可以为每个 fxml 文件设置两个不同的控制器。对于带按钮的 fxml,我们将控制器称为 ParentController
,因为它负责加载第二个 fxml。
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.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class ParentController implements Initializable {
@FXML
private Button insertButton;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML
private void insertData() throws IOException {
Stage newStage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("gridpane.fxml"));
Parent root = loader.load();
//Get controller instance from the FXMLLoader
GridPaneController controller = loader.getController();
controller.updateGridPane();
Scene scene = new Scene(root, 500, 350);
newStage.setScene(scene);
newStage.show();
}
}
如您所见,我们正在获取控制器实例,然后尝试调用 GridPaneController 中的方法 class。
GridPaneController controller = loader.getController();
controller.updateGridPane();
此 updateGridPane()
包含更新您的 GridPane
的逻辑,如果您需要在 GridPane 上设置某些内容,您可以在方法中包含参数并使用它们。
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import java.net.URL;
import java.util.ResourceBundle;
public class GridPaneController implements Initializable {
@FXML private GridPane gpane;
public void updateGridPane() {
Label newLabel = new Label("test");
gpane.add(newLabel, 0, 0);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
FXML 没有什么特别的,它只是有不同的控制器。
grid.fxml
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.GridPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.GridPaneController">
<top>
<GridPane fx:id="gpane">
</GridPane>
</top>
</BorderPane>
main.fxml
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.ParentController" >
<Button fx:id="insertButton" text="insert" onMouseClicked="#insertData"/>
</StackPane>
我的第一个 fxml(按钮):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.fxml_gridtest_controller" >
<Button fx:id="insertBut" text="insert" onMouseClicked="#insertData"/>
</StackPane>
我的第二个 fxml(网格窗格):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.fxml_gridtest_controller">
<top>
<GridPane fx:id="gpane">
</GridPane>
</top>
</BorderPane>
我的控制器:
package fxml_grid_test;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class fxml_gridtest_controller extends Application {
@FXML private Button insertBut;
@FXML private GridPane gpane;
@FXML private void insertData() throws IOException{
gpane.add(new Label("test"), 0, 0);
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("grid_fxml.fxml"));
Scene scene = new Scene(root, 500, 350);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void start(Stage primaryStage) throws Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
我的主要 :
package fxml_grid_test;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Fxml_grid_test extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("button_fxml.fxml"));
Scene scene = new Scene(root, 300, 275);
primaryStage.setTitle("insert data test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
想法是: 当我按下按钮时,我想将一些数据插入到我的网格窗格中,然后显示 fxml。 我的两个fxml共享控制器,我觉得这是问题的根源,但我不知道如何解决。
控制器编译 class 实现 "code behind" fxml 定义的对象层次结构。这些对于定义操作 UI 元素的方法很有用,并且可以在必要时由其他 classes 调用。
在您的情况下,我们需要为 grid.fxml
定义一个控制器,因为我们要创建一个方法,该方法将负责向 grid.This 添加元素,方法将在 MainController
,它负责加载 grid.fxml
并更新它。
FXMLLoader 帮助我们获取在 fxml 中引用的控制器实例。
因此,您可以为每个 fxml 文件设置两个不同的控制器。对于带按钮的 fxml,我们将控制器称为 ParentController
,因为它负责加载第二个 fxml。
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.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class ParentController implements Initializable {
@FXML
private Button insertButton;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML
private void insertData() throws IOException {
Stage newStage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("gridpane.fxml"));
Parent root = loader.load();
//Get controller instance from the FXMLLoader
GridPaneController controller = loader.getController();
controller.updateGridPane();
Scene scene = new Scene(root, 500, 350);
newStage.setScene(scene);
newStage.show();
}
}
如您所见,我们正在获取控制器实例,然后尝试调用 GridPaneController 中的方法 class。
GridPaneController controller = loader.getController();
controller.updateGridPane();
此 updateGridPane()
包含更新您的 GridPane
的逻辑,如果您需要在 GridPane 上设置某些内容,您可以在方法中包含参数并使用它们。
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import java.net.URL;
import java.util.ResourceBundle;
public class GridPaneController implements Initializable {
@FXML private GridPane gpane;
public void updateGridPane() {
Label newLabel = new Label("test");
gpane.add(newLabel, 0, 0);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
FXML 没有什么特别的,它只是有不同的控制器。
grid.fxml
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.GridPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.GridPaneController">
<top>
<GridPane fx:id="gpane">
</GridPane>
</top>
</BorderPane>
main.fxml
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxml_grid_test.ParentController" >
<Button fx:id="insertButton" text="insert" onMouseClicked="#insertData"/>
</StackPane>