Java JavaFX如何制作这样的左悬停菜单

时间:2017-01-23 23:21:46

标签: css javafx menu hover

这是我尝试在我的JavaFx ERP中复制的左悬停菜单,起初我认为使用MenuButton会很好,遗憾的是我无法获得菜单折叠/展开,也没有子菜单方式,我也试过手风琴,但它的表现不一样。有没有人知道我应该如何进行以及我应该使用哪些javafx组件来实现目标?

悬停时折叠的菜单(悬停显示全名和子菜单):

enter image description here

菜单展开:

enter image description here

1 个答案:

答案 0 :(得分:0)

  

主:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Sedrick
 */
public class SideView extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
  

控制器:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

/**
 *
 * @author Sedrick
 */
public class FXMLDocumentController implements Initializable {

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }    
}
  

FXML:

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

<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitMenuButton?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" prefHeight="554.0" prefWidth="897.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sideview.FXMLDocumentController">
   <children>
      <VBox layoutX="7.0" prefHeight="200.0" prefWidth="162.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <SplitMenuButton mnemonicParsing="false" popupSide="RIGHT" text="SplitMenuButton">
              <items>
                <MenuItem mnemonicParsing="false" text="Action 1" />
                <MenuItem mnemonicParsing="false" text="Action 2" />
              </items>
            </SplitMenuButton>
            <SplitMenuButton mnemonicParsing="false" popupSide="RIGHT" text="SplitMenuButton">
              <items>
                <MenuItem mnemonicParsing="false" text="Action 1" />
                <MenuItem mnemonicParsing="false" text="Action 2" />
              </items>
            </SplitMenuButton>
         </children>
      </VBox>
   </children>
</AnchorPane>

您需要将jar“controlsFx”添加到库中,并确保拥有最后一个Java JRE和JDK

用图像替换单词,您还必须使用css使其看起来像您希望的那样。

enter image description here