我的布局是:MVE
我在与其他类的控制器交互时遇到了困难(是的,我知道这方面有几个线程--但我仍然不明白出了什么问题)。
因此,基本上,我有一个StackPane“根”,其中包括两个锚窗格:"vertMenu“和"content”。“根”为了布局目的,StackPane嵌套在VBox中。"vertMenu“将包含不同的按钮来将内容(从fxml-file)加载到内容窗格--而不是在这个MVE中实现。
我希望在类RootController中按下"vertMenu“来禁用VertMenuController类中的”vertMenu“,以便在来自ContentController类的内容窗格中使用UI进行迭代。
我的守则:
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();
}
}根视图:
<?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>我没有任何异常,所以我认为我正确地引用了根控制器类中的"verticalMenuController“。但不幸的是,我不能禁用AnchorPane "vertMenu“。
到目前为止非常感谢。
发布于 2017-06-09 13:37:22
加载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>加载和显示的不同的实例,因此您看不到任何效果。
相反,您需要对控制器的引用,该控制器与<fx:include>元素加载的UI相关联。您可以通过注入“嵌套控制器”在主控制器中执行此操作。
首先,向您的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进行同样的处理。
https://stackoverflow.com/questions/44451704
复制相似问题