繁体   English   中英

JavaFX 2.2使用带有FXML的第二个窗口

[英]JavaFX 2.2 using a second window with FXML

我读了很多有相同问题的文章,但是我无法解决它并找到有用的答案。

我想创建一个带有打开新窗口的选项的应用程序,以输入,编辑,删除一些数据。

我有一个如下所示的Main.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
        Scene scene = new Scene(root, 1000, 700);

        stage.setTitle("FXML Welcome");
        stage.setScene(scene);
        stage.show();

    }

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

为此,我有一个MainWindowController.java

import java.net.URL;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import java.sql.*;
import java.util.ResourceBundle;
import javafx.scene.control.ComboBox;
import java.sql.Connection;
import java.sql.ResultSet;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

public class MainWindowController implements Initializable {

    String ComboCompany = null;
    String ComboModellName = null;
    String ComboTyp = null;

    @FXML
    protected void handelAddButtonAction(ActionEvent event) {
        System.out.println("Add");
    }

    @FXML
    protected void handelEditButtonAction(ActionEvent event) {
        System.out.println("edit");
    }

    @FXML
    protected void handelUpdateButtonAction(ActionEvent event) {
        System.out.println("update");
    }

    @FXML
    protected void handelDelButtonAction(ActionEvent event) {
        System.out.println("del");
    }

    @FXML
    protected void handelCloseButtonAction(ActionEvent event) {
        Platform.exit();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }
}

一个新的AddWindow.fxml已创建并可以使用。 我必须创建一个新的AddWindowController.java还是可以在新类中编写一些代码?

最大的问题是,如何解决我单击“添加”按钮后弹出的新窗口以输入数据并关闭它的问题?

例外:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1147985808.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757)
... 47 more
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2428)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at multiscreeninput.MainWindowController.openInputWindow(MainWindowController.java:26)
... 57 more

我根据您的问题创建了一个小示例,其中我们可以使用子窗口编辑 MainWindow上存在text of a Labeltext of a Label

它从child window预设的TextField获取输入,并在MainWindow上更新Label

您也可以使用它来添加其他要求。

所有文件都在包multiscreeninput内部

Main.java

package multiscreeninput;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("/multiscreeninput/MainWindow.fxml"));
        Scene scene = new Scene(root, 200, 200);
        stage.setTitle("FXML Welcome");
        stage.setScene(scene);
        stage.show();
    }

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

MainWindow.fxml

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="multiscreeninput.MainWindowController" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
         <children>
            <Label fx:id="label" text="Use to button to edit data" wrapText="true" />
         </children>
      </HBox>
      <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
         <children>
            <Button fx:id="button" mnemonicParsing="false" onAction="#openInputWindow" text="Change Text" />
         </children>
      </HBox>
   </children>
</VBox>

MainWindowController.java

package multiscreeninput;

import java.io.IOException;

import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class MainWindowController {

    @FXML
    Button button;

    @FXML
    Label label;

    public void openInputWindow(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Child.fxml"));
        VBox newWindow = (VBox)loader.load();
        ChildController controller = loader.getController();
        controller.setMainWindow(this);
        Stage stage = new Stage();
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(button.getScene().getWindow());
        Scene scene = new Scene(newWindow);
        stage.setScene(scene);
        stage.show();   
    }

    public StringProperty getLabelTextProperty() {
        return label.textProperty();
    }
}

Child.fxml

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

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<VBox alignment="CENTER" fillWidth="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="200.0" spacing="20.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="multiscreeninput.ChildController">
   <children>
      <TextField fx:id="text">
         <VBox.margin>
            <Insets />
         </VBox.margin></TextField>
      <Button fx:id="button" mnemonicParsing="false" onAction="#saveAndClose" text="Save and Close" />
   </children>
   <padding>
      <Insets top="10.0" />
   </padding>
</VBox>

ChildController.java

package multiscreeninput;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class ChildController {

    @FXML
    TextField text;

    @FXML
    Button button;

    private MainWindowController mainWindow;


    public void setMainWindow(MainWindowController mainWindow){
        this.mainWindow = mainWindow;
    }

    public void saveAndClose(ActionEvent event){
        mainWindow.getLabelTextProperty().bind(text.textProperty());
        ((Button)event.getSource()).getScene().getWindow().hide();
    }
}

我有一个示例代码,该代码按此顺序打开ROOT->父亲-> SON-> GRANDSON窗口。 没有其他处理,只需单击NEXT按钮,它将打开它的子​​项。 这是在Netbeans IDE版本8.02和JavaFX版本8x中开发的。

这是主程序-TESTMULTILEVEL.JAVA,它将打开ROOTWindow.FXML

package testmultilevel;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Hornigold Arthur
 */
public class TestMultiLevel extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("RootWindow.fxml"));

        Scene scene = new Scene(root,300,300);
        stage.setTitle("This is the ROOT WINDOW");

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

这是ROOT FXML

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" style="-fx-background-color: cornsilk; -fx-border-color: red; -fx-border-style: solid; -fx-border-width: 3;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testmultilevel.RootWindowController">
    <children>
        <Button fx:id="btnNext" layoutX="126.0" layoutY="161.0" onAction="#handleBtnNext" text="Next">
         <font>
            <Font name="Bookman Old Style Bold" size="14.0" />
         </font></Button>
        <Label fx:id="label" layoutX="84.0" layoutY="32.0" minHeight="16" minWidth="69" text="This is the ROOT" textAlignment="CENTER">
         <font>
            <Font name="Bookman Old Style Bold" size="14.0" />
         </font></Label>
    </children>
</AnchorPane>

这是ROOT程序

package testmultilevel;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.beans.property.StringProperty;
import javafx.scene.layout.AnchorPane;

public class RootWindowController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button btnNext;

    @FXML
    private Label label;

    @FXML
    void handleBtnNext(ActionEvent event) {

        System.out.println("Opening the FATHER Window");
        try {
            openInputWindow(event);
        } catch (IOException ex) {
            Logger.getLogger(RootWindowController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @FXML
    void initialize() {
        assert btnNext != null : "fx:id=\"btnNext\" was not injected: check your FXML file 'RootWindow.fxml'.";
        assert label != null : "fx:id=\"label\" was not injected: check your FXML file 'RootWindow.fxml'.";

    }

    public void openInputWindow(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Father.fxml"));
        AnchorPane newWindow = (AnchorPane) loader.load();
        FatherController controller = loader.getController();
        controller.setMainWindow(this);
        Stage stage = new Stage();
        stage.setTitle("Father Window");
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(btnNext.getScene().getWindow());
        Scene scene = new Scene(newWindow);
        stage.setScene(scene);
        stage.show();
    }

}

这是FATHER FXML

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" style="-fx-background-color: cornsilk; -fx-border-color: red; -fx-border-style: solid; -fx-border-width: 3;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testmultilevel.FatherController">
    <children>
        <Button fx:id="btnNext" layoutX="126.0" layoutY="161.0" onAction="#handleBtnNext" text="Next">
         <font>
            <Font name="Bookman Old Style Bold" size="14.0" />
         </font></Button>
        <Label fx:id="label" layoutX="84.0" layoutY="32.0" minHeight="16" minWidth="69" text="This is the FATHER" textAlignment="CENTER">
         <font>
            <Font name="Bookman Old Style Bold" size="14.0" />
         </font></Label>
    </children>
</AnchorPane>

这是父亲控制器

package testmultilevel;

import static java.awt.SystemColor.text;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class FatherController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button btnNext;

    @FXML
    private Label label;

    private RootWindowController rootWindow;

    public void setMainWindow(RootWindowController rootWindow){
        this.rootWindow = rootWindow;
    }

    @FXML
    void handleBtnNext(ActionEvent event) {

        System.out.println("Opening the Son Window");
        try {
            openInputWindow(event);
        } catch (IOException ex) {
            Logger.getLogger(RootWindowController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @FXML
    void initialize() {
        assert btnNext != null : "fx:id=\"btnNext\" was not injected: check your FXML file 'RootWindow.fxml'.";
        assert label != null : "fx:id=\"label\" was not injected: check your FXML file 'RootWindow.fxml'.";

    }

    public void openInputWindow(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Son.fxml"));
        AnchorPane newWindow = (AnchorPane) loader.load();
        SonController controller = loader.getController();
        controller.setMainWindow(this);
        Stage stage = new Stage();
        stage.setTitle("Son Window");
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(btnNext.getScene().getWindow());
        Scene scene = new Scene(newWindow);
        stage.setScene(scene);
        stage.show();
    }
}

这是SON FXML

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" style="-fx-background-color: cornsilk; -fx-border-color: red; -fx-border-style: solid; -fx-border-width: 3;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testmultilevel.SonController">
    <children>
        <Button fx:id="btnNext" layoutX="126.0" layoutY="161.0" onAction="#handleBtnNext" text="Next">
         <font>
            <Font name="Bookman Old Style Bold" size="14.0" />
         </font></Button>
        <Label fx:id="label" layoutX="84.0" layoutY="32.0" minHeight="16" minWidth="69" text="This is the SON" textAlignment="CENTER">
         <font>
            <Font name="Bookman Old Style Bold" size="14.0" />
         </font></Label>
    </children>
</AnchorPane>

这是SON控制器

package testmultilevel;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class SonController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button btnNext;

    @FXML
    private Label label;

    private FatherController FatherWindow;

    public void setMainWindow(FatherController FatherWindow){
        this.FatherWindow = FatherWindow;
    }

    @FXML
    void handleBtnNext(ActionEvent event) {

        System.out.println("Opening the Grand Son Window");
        try {
            openInputWindow(event);
        } catch (IOException ex) {
            Logger.getLogger(RootWindowController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @FXML
    void initialize() {
        assert btnNext != null : "fx:id=\"btnNext\" was not injected: check your FXML file 'RootWindow.fxml'.";
        assert label != null : "fx:id=\"label\" was not injected: check your FXML file 'RootWindow.fxml'.";

    }

    public void openInputWindow(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("GrandSon.fxml"));
        AnchorPane newWindow = (AnchorPane) loader.load();
        GrandSonController controller = loader.getController();
        controller.setMainWindow(this);
        Stage stage = new Stage();
        stage.setTitle("Grand Son Window");
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(btnNext.getScene().getWindow());
        Scene scene = new Scene(newWindow);
        stage.setScene(scene);
        stage.show();
    }
}

这是大儿子FXML

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" style="-fx-background-color: cornsilk; -fx-border-color: red; -fx-border-style: solid; -fx-border-width: 3;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testmultilevel.GrandSonController">
    <children>
        <Button fx:id="btnNext" layoutX="126.0" layoutY="161.0" onAction="#handleBtnNext" text="Next">
         <font>
            <Font name="Bookman Old Style Bold" size="14.0" />
         </font></Button>
        <Label fx:id="label" layoutX="84.0" layoutY="32.0" minHeight="16" minWidth="69" text="This is the GRANDSON" textAlignment="CENTER">
         <font>
            <Font name="Bookman Old Style Bold" size="14.0" />
         </font></Label>
    </children>
</AnchorPane>

这是GRAND SON控制器。

package testmultilevel;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class GrandSonController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button btnNext;

    @FXML
    private Label label;

    private SonController SonWindow;

    public void setMainWindow(SonController SonWindow){
        this.SonWindow = SonWindow;
    }

   @FXML
    void handleBtnNext(ActionEvent event) {

    }

    @FXML
    void initialize() {
        assert btnNext != null : "fx:id=\"btnNext\" was not injected: check your FXML file 'RootWindow.fxml'.";
        assert label != null : "fx:id=\"label\" was not injected: check your FXML file 'RootWindow.fxml'.";

    }
}

我没有尝试将创建子窗口作为一个单独的函数进行一般化,并使用适当的参数对其进行调用,因此不必在每个CONTROLLER中重复以下代码。

    public void openInputWindow(ActionEvent event) throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("GrandSon.fxml"));
    AnchorPane newWindow = (AnchorPane) loader.load();
    GrandSonController controller = loader.getController();
    controller.setMainWindow(this);
    Stage stage = new Stage();
    stage.setTitle("Grand Son Window");
    stage.initModality(Modality.WINDOW_MODAL);
    stage.initOwner(btnNext.getScene().getWindow());
    Scene scene = new Scene(newWindow);
    stage.setScene(scene);
    stage.show();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM