簡體   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