使用JFoenix切换导航栏

时间:2019-08-07 15:48:33

标签: java javafx jfoenix

我的工具栏菜单出现问题,当我打开它时,它位于其他组件的顶部。

菜单关闭: image

菜单打开:image

我的控制器:

public class Controller2 implements Initializable {

    @FXML
    private JFXHamburger hamburger;
    @FXML
    private VBox toolbar;
 /*   @FXML
    private Button hamburger;*/
    @FXML
    private JFXDrawer drawer;
    @FXML
    private Pane stackConsole;
    @FXML
    private AnchorPane teste1;
    @FXML
    private AnchorPane teste2;
    @FXML
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        initDrawer();
    }

    private void initDrawer() {
        try {
            toolbar = FXMLLoader.load(getClass().getResource("/fxml/toolbarMenu.fxml"));  
        } catch (IOException ex) {
            Logger.getLogger(Controller2.class.getName()).log(Level.SEVERE, null, ex);
        }
        HamburgerSlideCloseTransition task = new HamburgerSlideCloseTransition(hamburger);
        task.setRate(-1);
        hamburger.addEventHandler(MouseEvent.MOUSE_CLICKED, (Event event) ->  {
                task.setRate(task.getRate() * -1);
                task.play();
                if(drawer.isClosed()){
                    drawer.setSidePane(toolbar);
                    drawer.open();
                }else{
                    drawer.close();
                    task.setRate(-1);

                }
        });

主屏幕FXML文件:

<?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.StackPane?> <?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.Controller2">    <children>
      <HBox alignment="CENTER_LEFT" prefHeight="50.0" prefWidth="600.0" style="-fx-background-color: #0080ff;" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <JFXHamburger fx:id="hamburger" />
         </children>
         <padding>
            <Insets left="10.0" />
         </padding>
      </HBox>
      <AnchorPane layoutY="50.0" prefHeight="350.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="50.0">
         <children>
            <StackPane fx:id="stackConsole" layoutY="25.0" prefHeight="325.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
            <Button mnemonicParsing="false" text="Button" AnchorPane.leftAnchor="30.0" AnchorPane.topAnchor="50.0" />
            <JFXDrawer fx:id="drawer" defaultDrawerSize="150.0" prefWidth="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
         </children>
      </AnchorPane>
      <VBox prefHeight="400.0" prefWidth="100.0" style="-fx-background-color: #c544;" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="0.0">
         <children>
          <Button mnemonicParsing="false" text="1" />
          <Button mnemonicParsing="false" text="2" />
          <Button mnemonicParsing="false" text="3" />   
         </children>
      </VBox>    </children> </AnchorPane>

工具栏菜单FXML文件:

<VBox id="toolbar" fx:id="toolbar" alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="286.0" prefWidth="148.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">    <children>
      <JFXButton alignment="CENTER" contentDisplay="TOP" prefHeight="60.0" prefWidth="150.0" text="Home">
         <graphic>
            <FontAwesomeIconView glyphName="USER" size="25" />
         </graphic>
      </JFXButton>
      <JFXButton alignment="CENTER" contentDisplay="TOP" prefHeight="60.0" prefWidth="148.0" text="Register">
         <graphic>
            <FontAwesomeIconView glyphName="BOOK" size="25" />
         </graphic>
      </JFXButton>
      <JFXButton alignment="CENTER" contentDisplay="TOP" prefHeight="60.0" prefWidth="149.0" text="List">
         <graphic>
            <FontAwesomeIconView glyphName="TABLE" size="25" />
         </graphic>
      </JFXButton>
      <JFXButton alignment="CENTER" contentDisplay="TOP" prefHeight="60.0" prefWidth="149.0" text="Logout">
         <graphic>
            <FontAwesomeIconView glyphName="COG" size="25" />
         </graphic>
      </JFXButton>    </children>    <opaqueInsets>
      <Insets />    </opaqueInsets>    <padding>
      <Insets top="10.0" />    </padding> </VBox>

我有2个FXML文件,一个用于主屏幕,一个用于我的工具栏。

工具栏打开时,我想将工具栏下的项目推到左侧,这可能吗?

jfoenix是最好的方法吗?

1 个答案:

答案 0 :(得分:0)

如果“菜单关闭”图像中的“按钮”在代码中已被注释掉,请删除注释标记,然后重命名它,使其与JFXHamburger对象的名称不同。

确保要移动的Button的fx:id与它的新重命名变量匹配。

完成后,只要打开或关闭菜单,就可以使用initDrawer()方法设置其位置。

例如:

HashMap