【问题标题】:JavaFX 2.2 using a second window with FXMLJavaFX 2.2 使用带有 FXML 的第二个窗口
【发布时间】:2015-01-27 11:15:15
【问题描述】:

我阅读了很多相同问题的帖子,但我无法修复它并找到有用的答案。

我想创建一个应用程序,可以选择打开一个新窗口来输入、编辑、删除一些数据。

我有一个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

【问题讨论】:

    标签: java javafx javafx-2


    【解决方案1】:

    我根据您的问题创建了一个小示例,我们可以在其中使用子窗口编辑MainWindow 上的text 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();
        }
    }
    

    【讨论】:

    • 感谢您的回答,但它不起作用,只有按下按钮时出错
    • 您可以粘贴指向错误的链接吗?你把文件放在包里了吗multiscreeninput
    • 它工作正常。问题是它是否是处理它的正确方法以及在使用后隐藏窗口是否可以..
    • 请添加截图
    【解决方案2】:

    我有一个示例代码,它按顺序打开 ROOT -> FATHER -> SON -> GRANDSON 窗口。不涉及其他处理,只需单击下一步按钮,它将打开它的孩子。这是在 Netbeans IDE 8.02 版和 JavaFX 8x 版中开发的。

    这是打开 ROOTWindow.FXML 的主程序 - TESTMULTILEVEL.JAVA

    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);
        }
    
    }
    

    这是根 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();
        }
    
    }
    

    这是父亲 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();
        }
    }
    

    这是 GRAND 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.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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-14
      • 2013-11-04
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多