我有麻煩與其他班級的控制器交互(是的,我知道有幾個線程這一點 - 但我仍然沒有得到什麼錯誤)。
所以,基本上我有一個StackPane「根」,其中包括兩個AnchorPane的:「vertMenu」和「內容」。 「根」StackPane嵌套在VBox中用於佈局。 「vertMenu」將包含不同的按鈕來將內容(從fxml文件)加載到內容窗格 - 在此MVE中未實現。
我想在類RootController中按「menuButton」,以禁用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");
}
}
內容查看
<?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」。
非常感謝,迄今爲止。
啊是有道理的。我不記得這當然會創建一個UI的新實例...謝謝! – Michael