我的第一个 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 (gridpane):
<?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 共享控制器,我觉得这是问题的根源,但我不知道如何解决它。