我的目标是改变我的程序主窗口的中心窗格,由一个控制器通过在不同控制器的按钮上的操作来控制。我知道为了访问主窗口视图,我必须在其他控制器中示例化主窗口控制器,我知道我可以将主控制器类自动连接(注入)到不同的控制器中,但是当我这样做时,我的所有css样式和主菜单都处于非活动状态,如果没有它,当我尝试从mainmenucontroller使用maincontroller时会出现nullpointer异常
我有main.fxml
<?import com.jfoenix.controls.JFXDrawer?>
<?import com.jfoenix.controls.JFXHamburger?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.StackPane?>
<StackPane fx:id="rootPane" prefHeight="720.0" prefWidth="1280.0" styleClass="root" stylesheets="@../style/dark-theme-contrast.css" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.hrc.backoffice.MainController">
<children>
<AnchorPane accessibleRole="MENU_BUTTON" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" styleClass="root" stylesheets="@../style/dark-theme-contrast.css">
<children>
<BorderPane prefHeight="720.0" prefWidth="1280.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<left>
<JFXDrawer fx:id="mainMenuDrawer" alignment="CENTER_LEFT" defaultDrawerSize="220.0" prefWidth="0.0" resizableOnDrag="true" stylesheets="@../style/dark-theme-contrast.css" BorderPane.alignment="TOP_LEFT" />
</left>
<center>
<AnchorPane fx:id="mainWindowCenterPane" BorderPane.alignment="TOP_LEFT">
<children>
<JFXHamburger fx:id="mainMenuHamburger" styleClass="jfx-hamburger-icon" stylesheets="@../style/dark-theme-contrast.css" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="10.0" />
</children>
</AnchorPane>
</center>
</BorderPane>
</children>
</AnchorPane>
</children>
这是maincontroller.java
public class MainController implements Initializable {
@FXML
private StackPane rootPane;
@FXML
private JFXDrawer mainMenuDrawer;
@FXML
private JFXHamburger mainMenuHamburger;
@FXML
private AnchorPane mainWindowCenterPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
initDrawer();
} catch (IOException e) {
e.printStackTrace();
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, e);
}
}
private void initDrawer() throws IOException {
ScrollPane menuScrollpane = FXMLLoader.load(getClass().getResource("/view/mainMenu.fxml"));
mainMenuDrawer.setSidePane(menuScrollpane);
HamburgerSlideCloseTransition task = new HamburgerSlideCloseTransition(mainMenuHamburger);
task.setRate(-1);
mainMenuHamburger.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
mainMenuDrawer.toggle();
});
mainMenuDrawer.setOnDrawerOpening((event) -> {
task.setRate(task.getRate() * -1);
task.play();
mainMenuDrawer.setMinWidth(220);
});
mainMenuDrawer.setOnDrawerClosed((event) -> {
task.setRate(task.getRate() * -1);
task.play();
mainMenuDrawer.setMinWidth(0);
});
}
public void setMainWindowCenterPane(AnchorPane mainWindowCenterPane) {
this.mainWindowCenterPane = mainWindowCenterPane;
}
}
这里是main-menu.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<ScrollPane fx:id="mainMenuScrollPane" prefHeight="720.0" prefWidth="220.0" stylesheets="@../style/dark-theme-contrast.css" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.hrc.backoffice.MainMenuController">
<content>
<VBox fx:id="mainMenuVbox" prefHeight="574.0" prefWidth="200.0" stylesheets="@../style/dark-theme-contrast.css">
<children>
<Accordion fx:id="mainMenuAccordion" stylesheets="@../style/dark-theme-contrast.css">
<panes>
<TitledPane fx:id="menuProductMenu" alignment="TOP_RIGHT" contentDisplay="RIGHT" expanded="false" nodeOrientation="RIGHT_TO_LEFT" stylesheets="@../style/dark-theme-contrast.css" text="Меню">
<graphic>
<ImageView fitHeight="25.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/028-menu1.png" />
</image>
</ImageView>
</graphic>
<content>
<VBox prefHeight="100.0" prefWidth="216.0" styleClass="titled-pane" stylesheets="@../style/dark-theme-contrast.css">
<children>
<JFXButton fx:id="compositeProductMenu" alignment="BASELINE_RIGHT" buttonType="RAISED" onAction="#initCompositeProductMenu" prefHeight="30.0" prefWidth="170.0" text="Блюда" textFill="WHITE">
<font>
<Font size="13.0" />
</font>
</JFXButton>
<JFXButton fx:id="semifinishedProductMenu" alignment="BASELINE_RIGHT" buttonType="RAISED" layoutX="20.0" layoutY="20.0" prefHeight="30.0" prefWidth="170.0" text="Полуфабрикаты" textFill="WHITE">
<font>
<Font size="13.0" />
</font>
</JFXButton>
<JFXButton fx:id="compositeProductMenuCategories" alignment="BASELINE_RIGHT" buttonType="RAISED" layoutX="20.0" layoutY="45.0" prefHeight="30.0" prefWidth="170.0" text="Категории блюд" textFill="WHITE">
<font>
<Font size="13.0" />
</font>
</JFXButton>
<JFXButton fx:id="productModificators" alignment="BASELINE_RIGHT" buttonType="RAISED" layoutX="10.0" layoutY="60.0" prefHeight="30.0" prefWidth="170.0" text="Модификаторы" textFill="WHITE">
<font>
<Font size="13.0" />
</font>
</JFXButton>
</children>
</VBox>
</content>
<font>
<Font name="System Bold Italic" size="14.0" />
</font>
</TitledPane>
</panes>
</Accordion>
</children>
</VBox>
</content>
和mainmenucontroller.java
public class MainMenuController implements Initializable {
@FXML
private ScrollPane mainMenuScrollPane;
@FXML
private VBox mainMenuVbox;
@FXML
private Accordion mainMenuAccordion;
@FXML
private TitledPane menuFinances;
@FXML
private TitledPane menuProductMenu;
@FXML
private JFXButton compositeProductMenu;
@FXML
private JFXButton semifinishedProductMenu;
@FXML
private JFXButton compositeProductMenuCategories;
@FXML
private JFXButton productModificators;
@FXML
private TitledPane menuStock;
@FXML
private TitledPane menuProductOperations;
@FXML
private TitledPane menuMarketing;
@FXML
private TitledPane menuCompany;
@FXML
private TitledPane menuStatistics;
@FXML
private TitledPane menuSettings;
MainController mainController;
// @Autowired
// public MainMenuController(MainController mainController) {
// this.mainController = mainController;
// }
@Override
public void initialize(URL location, ResourceBundle resources) {
initCompositeProductMenu();
// compositeProductMenu.setOnAction(event -> {
// try {
// AnchorPane productMenuPane = FXMLLoader.load(getClass().getResource("/view/product-menu-list.fxml"));
// mainController.setMainWindowCenterPane(productMenuPane);
// } catch (IOException e) {
// e.printStackTrace();
// }
// });
}
@FXML
private void initCompositeProductMenu() {
compositeProductMenu.addEventHandler(ActionEvent.ACTION, event1 -> {
try {
AnchorPane productMenuPane = FXMLLoader.load(getClass().getResource("/view/product-menu-list.fxml"));
mainController.setMainWindowCenterPane(productMenuPane);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
以及要传递到product-menu-list.fxml中主窗口的视图
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXTabPane?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="720.0" prefWidth="1200.0" stylesheets="@../style/dark-theme-contrast.css" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.hrc.backoffice.ProductMenuListController">
<children>
<JFXTabPane prefHeight="720.0" prefWidth="1200.0" stylesheets="@../style/dark-theme-contrast.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="Untitled Tab">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView prefHeight="691.2000202824526" prefWidth="1200.0000352125912" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<padding>
<Insets top="5.0" />
</padding>
</TableView>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Untitled Tab">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Untitled Tab">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</JFXTabPane>
</children>
</AnchorPane>
我知道为了访问主窗口视图,我必须在其他控制器中示例化主窗口控制器,我知道我可以将主控制器类自动连接(注入)到不同的控制器中,但是当我这样做时,我的所有css样式和主菜单都处于非活动状态,如果没有它,当我尝试从mainmenucontroller使用maincontroller时,当然会出现nullpointer异常。
我得到了什么 @Autowired // public MainMenuController(MainController mainController) { // this.mainController = mainController; // }
是完全不活动的白色窗口,只有汉堡包小部件可见,但不活动
我错过了什么???
项目结构
影响
暂无答案!
目前还没有任何答案,快来回答吧!