堆栈窗口中的JavaFX交互与多个控制器

时间:2017-06-09 07:21:01

标签: javafx

我的布局是: MVE

我无法与其他班级的控制员互动(是的,我知道有几个关于此问题的线索 - 但我仍然没有解决错误)。

所以,基本上我有一个StackPane" root"其中包括两个AnchorPane:" vertMenu"和"内容"。 "根" StackPane嵌套在VBox中以进行布局。 " vertMenu"将包含不同的按钮以将内容(从fxml文件)加载到内容窗格 - 未在此MVE中实现。

我想按" menuButton"在类RootController中禁用" vertMenu"从VertMenuController类到内容窗格上的UI,它来自ContentController类。

我的代码:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    private Stage stage;
    @Override
    public void start(Stage primaryStage) {
        try {
            this.stage = primaryStage;
            FXMLLoader loader = new FXMLLoader(getClass().getResource("Root.fxml"));
            Parent myPane = loader.load();

            Scene scene = new Scene(myPane);
            primaryStage.setScene(scene);

            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

根控制器类:

package application;

import java.io.IOException;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

public class RootController {
    @FXML private StackPane root;
    @FXML private Button menuButton;
    @FXML private Button bb;
    VertMenuController vertMenuC;
    ContentController contentC;

    @FXML private void initialize() {

    }

    @FXML private void menuButtonAction() throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("VertMenuView.fxml"));
        Parent root = loader.load();
        vertMenuC = loader.getController();

        vertMenuC.disableMenu();

    }


}

Root View:

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

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


<VBox prefHeight="450.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.RootController">
   <children>
      <StackPane fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: rgb(222,222,222);">
         <children>
            <fx:include source="ContentView.fxml" />
            <fx:include source="VertMenuView.fxml" />
         </children>
      </StackPane>
      <Button fx:id="menuButton" mnemonicParsing="false" onAction="#menuButtonAction" prefHeight="48.0" prefWidth="157.0" text="Button" />
      <Button fx:id="bb" mnemonicParsing="false" prefHeight="25.0" prefWidth="154.0" text="Button" />
   </children>
</VBox>

VertMenuController类

package application;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;

public class VertMenuController {
    @FXML private AnchorPane vertMenu;
    @FXML private Button b1;
    @FXML private Button b2;
    @FXML private VBox vbo;


    public void disableMenu() {
        vertMenu.setDisable(true);
    }
    public void enableMenu() {
        vertMenu.setDisable(false);
    }

    public AnchorPane getVertMenu() {
        return vertMenu;
    }
    public VBox getBox() {
        return vbo;
    }


}

VertMenuView

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

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

<AnchorPane fx:id="vertMenu" prefHeight="400.0" prefWidth="150.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.VertMenuController">
   <children>
      <VBox fx:id="vbo" prefHeight="400.0" prefWidth="150.0">
         <children>
            <Button fx:id="b1" mnemonicParsing="false" prefHeight="59.0" prefWidth="148.0" text="B1" />
            <Button fx:id="b2" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" prefHeight="59.0" prefWidth="148.0" text="B2" />
         </children>
      </VBox>
   </children>
</AnchorPane>

ContentController类

package application;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;

public class ContentController {
    @FXML private AnchorPane content;
    @FXML private Button b3;
    @FXML private Label ausgabe;



    @FXML private void b3Action() {
        ausgabe.setText("b2 pressed");
    }

    public void setAusgabe() {
        this.ausgabe.setText("Hello World");
    }


}

ContentView

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

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

<AnchorPane fx:id="content" maxWidth="600.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ContentController">
   <children>
      <VBox prefHeight="262.0" prefWidth="600.0">
         <children>
            <Button fx:id="b3" mnemonicParsing="false" onAction="#b3Action" prefHeight="59.0" prefWidth="600.0" text="Button" />
            <Label fx:id="ausgabe" prefHeight="113.0" prefWidth="610.0" text="Label" />
         </children>
      </VBox>
   </children>
</AnchorPane>

我没有任何例外,所以我认为我正确地引用了&#34; verticalMenuController&#34;在根控制器类中。但不幸的是,我无法禁用AnchorPane&#34; vertMenu&#34;。

到目前为止非常感谢你。

1 个答案:

答案 0 :(得分:1)

使用

加载FXML文件时
FXMLLoader loader = new FXMLLoader(getClass().getResource("VertMenuView.fxml"));
Parent root = loader.load();
vertMenuC = loader.getController();

vertMenuC.disableMenu();

FXMLLoader创建VertMenuView.fxml中定义的UI的新实例以及与该UI关联的新控制器实例。您将UI的根分配给局部变量root。当您致电vertMenuC.disableMenu时,您将禁用FXMLLoader创建的新UI实例中的菜单。但是,该UI与<fx:include>加载和显示的UI不同,因此您看不到任何效果。

相反,您需要引用与<fx:include>元素加载的UI关联的控制器。您可以通过injecting the "Nested Controller"将其执行到主控制器中。

首先,将fx:id添加到fx:include

<fx:include fx:id="vertMenu" source="VertMenuView.fxml" />

然后将此注入您的RootController。规则是控制器的字段名称为fx:id,附加了文本"Controller"

public class RootController {
    @FXML private StackPane root;
    @FXML private Button menuButton;
    @FXML private Button bb;
    @FXML private VertMenuController vertMenuController;

    @FXML private void initialize() {

    }

    // ...

}

现在你可以直接在注入的控制器上调用disableMenu

@FXML private void menuButtonAction() {

    vertMenuController.disableMenu();

}

您可能也需要对ContentController执行相同操作。