fxml中的组合框菜单项字体更改 [英] Combo box menu item font change in fxml

查看:288
本文介绍了fxml中的组合框菜单项字体更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to change the font and remove the shadow for the combo box items.

我尝试使用下面的代码但是没有成功:

I've made an attempt with the code below but it was unsuccessful:

.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;
    }


推荐答案

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

For JavaFX2 you can create a CellFactory to set it:

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示例,请检查这个答案,我给了一个非常相似的问题,但是其他版本。

For an JavaFX 8 example check this answer that I gave to a very similar question but for other version.

这篇关于fxml中的组合框菜单项字体更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆