【问题标题】:Combo box menu item font change in fxmlfxml中的组合框菜单项字体更改
【发布时间】:2014-08-08 12:45:20
【问题描述】:

我需要更改字体并移除组合框项目的阴影。

我已尝试使用以下代码,但未成功:

.combo-box .popup-menu, .combo-box .menu-item, .combo-box .popup-menu .menu-item-radio
    {
        -fx-shadow-highlight-color: transparent;
         -fx-font-family: "Arial";
         -fx-font-size: 14px;
    }

【问题讨论】:

    标签: css javafx javafx-2 fxml scenebuilder


    【解决方案1】:

    对于 JavaFX2,您可以创建一个 CellFactory 来设置它:

    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.scene.Scene;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.scene.layout.Pane;
    import javafx.stage.Stage;
    import javafx.util.Callback;
    
    public class Main extends Application {
    
        @Override
        public void start(Stage stage) throws Exception {
            ComboBox<String> cb = new ComboBox<String>();
            cb.setItems(FXCollections.observableArrayList("Foo","Bar","777","Batman"));
            cb.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
                @Override public ListCell<String> call(ListView<String> p) {
                    return new ListCell<String>() {
                        {
                            getStyleClass().add("customcell");
                        }
                        @Override
                        protected void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                            setText(item);
                        }
                    };
                }
            });
            cb.getSelectionModel().selectFirst();
            Pane root = new Pane();
            root.getChildren().add(cb);
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
            stage.setScene(scene);
            stage.show();
        }
        public static void main(String[] args) {launch(args);}
    }
    

    style.css

    /* for the main button of the ComboBox  */
    .combo-box .cell{
        -fx-text-fill: blue;
        -fx-font: 16px "Arial";
    }
    .customcell {
        -fx-text-fill: blue;
        -fx-font: 16px "Arial";
        /* No alternate highlighting */
        -fx-background-color: #FFF;
    }
    

    对于 JavaFX 8 示例,请检查 this answer,我提出了一个非常相似的问题,但对于其他版本。

    【讨论】:

    • 但是我有一个 fxml 文件,关于如何更改组合框菜单项的字体和背景颜色。我使用场景构建器 1.1 创建组合框。
    • 只需加载CSS,如上例所示。您甚至不必更改您的 fxml
    • 我直接加载了上面例子中的css。但是组合框中的默认值只改变了(可见的)。菜单项字体和大小仍为默认字体和大小。
    • @Karthikram,那么您使用的是JavaFX 8。此答案适用于 JavaFX 2,底部有 a link 用于 JavaFX 8 示例。
    • 我检查了版本。如果版本是 javaFX 1.1。我使用 javafx scene builder 1.1 来开发页面。我有什么选择可以在这里更改菜单项吗?我想我使用的是相当旧的版本。
    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    相关资源
    最近更新 更多