css - fxml 中的组合框菜单项字体更改

标签 css javafx javafx-2 fxml scenebuilder

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

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

.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 来设置它:

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

样式.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我提出了一个非常相似的问题,但针对其他版本。

关于css - fxml 中的组合框菜单项字体更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25203870/

相关文章:

html - 将 bootstrap td 表的高度设置为最小

java - 链接来自不同 fxml 文件的内容

Java,使用 javaFX 作为主菜单,然后切换到 JFrame 作为游戏本身

java - 检测对象是否保存在 javafx 桌面应用程序中的最佳方法是什么?

java - 如何使用 CSS 在 JavaFX2 中设置表格单元格的样式(不删除悬停/选择等)

javafx-2 - 设置 JavaFX TreeView 线条样式

javascript - 如何根据屏幕宽度在 Rails App 中编辑 TinyMCE 编辑器的宽度

html - 在 Bootstrap 列中居中重叠图像

javascript - 溢出 :hidden doesn't work

java - 如何以编程方式在 JavaFX 中设置选项卡标签的颜色或纹理?