【问题标题】:Menu button style while menu open JavaFX菜单打开 JavaFX 时的菜单按钮样式
【发布时间】:2020-10-20 17:12:00
【问题描述】:

我想知道是否可以在此菜单按钮的菜单当前打开时为该菜单按钮设置某种样式(例如背景颜色为黑色)。也就是说,如果菜单没有打开,那么它不应该有黑色背景色。我可以在 CSS 文件中执行此操作吗?甚至有可能吗?

我知道菜单按钮有一个方法showing,它指示我,菜单是否打开,但我不知道应用它,以便在菜单打开时按钮颜色改变。

这是菜单按钮:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/MenuButton.html

我使用的是 JavaFX,我使用的是 CSS。

【问题讨论】:

    标签: css javafx controlsfx


    【解决方案1】:

    documentation 表示 MenuButton 有一个 showing 伪类,在显示上下文菜单时设置,所以你可以这样做

    .menu-button:showing {
      -fx-base: black ;
    }
    

    这是一个快速测试工具:

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.MenuButton;
    import javafx.scene.control.MenuItem;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    
    public class App extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            MenuButton button = new MenuButton("Test");
            button.getItems().addAll(
                    new MenuItem("Item 1"),
                    new MenuItem("Item 2")
            );
            BorderPane root = new BorderPane();
            root.setTop(button);
            Scene scene = new Scene(root, 200, 200);
            scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    我从中得到了以下信息:

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多