增加Combobox文本大小

时间:2013-12-25 21:22:46

标签: javafx javafx-2 javafx-8

我有JavaFX组合框的基本示例:

public class JavaFXComboBox extends Application {

    @Override
    public void start(Stage primaryStage) {

        final ComboBox comboBox = new ComboBox();
        comboBox.getItems().addAll(
                "Item 1",
                "Item 2",
                "Item 3",
                "Item 4");
        comboBox.setValue("Item 1");

        final Label label = new Label();

        Button btn = new Button();
        btn.setText("Read comboBox");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                label.setText("selectd: " + comboBox.getValue());
            }
        });

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(label, comboBox, btn);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

你能告诉我如何增加文本的大小,以及保持原始的组合框标签大小吗?

enter image description here

3 个答案:

答案 0 :(得分:4)

制作一个类似于

的combo.css文件
.combo-box-popup .list-view {
    -fx-font-size : 15pt;
}

/*added this in an edit*/
.combo-box-popup .list-view .list-cell {
    -fx-padding: -1 -1 -1 -1;
}

并在创建场景后添加行scene.getStylesheets().add("javafxcombobox/combo.css");

enter image description here

来自caspian.css的一些剪辑会给你一些尝试。玩填充,看看会发生什么。

.combo-box-popup .list-view {
-fx-background-color: -fx-box-border, -fx-control-inner-background;
-fx-background-insets: 0, 1;
-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 8, 0.0 , 0 , 0 );
}

.combo-box-popup .list-view .list-cell {
-fx-padding: 4 0 4 5;
-fx-background-color: -fx-control-inner-background;
}

如果有人知道的话,我想知道是否有办法在comboBox.setStyle()中添加子样式。

答案 1 :(得分:3)

您需要在setCellFactory上设置comboBox,如下所示,如下所示 -

   comboBox.setValue("Item 1");

        comboBox.setCellFactory(
                new Callback<ListView<String>, ListCell<String>>() {
                    @Override
                    public ListCell<String> call(ListView<String> param) {
                        final ListCell<String> cell = new ListCell<String>() {
                            @Override
                            public void updateItem(String item,
                                    boolean empty) {
                                super.updateItem(item, empty);
                                if (item != null) {
                                    setText(item);
                                    setFont(this.getFont().font(this.getFont().getName(), 30.0)); //set your desired size
                                } else {
                                    setText(null);
                                }
                            }
                        };
                        return cell;
                    }

                });

        final Label label = new Label();

这会对你有所帮助。 谢谢!

答案 2 :(得分:0)

您好peter使用此代码您的下拉大小增加,但一个问题选择框节点大小不增加。如果plz检查此代码,我将工作该部分

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 *
 * @author reegan
 */
public class JavaFXComboBoxStack extends Application {

    @Override
    public void start(Stage primaryStage) {

        final ComboBox comboBox = new ComboBox();
        comboBox.getItems().addAll(
                "Item 1",
                "Item 2",
                "Item 3",
                "Item 4");
        comboBox.setValue( "Item 1");

       comboBox.setMaxWidth(10);


       comboBox.setMaxSize(25, 25);

        comboBox.setCellFactory(
                new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> param) {
                final ListCell<String> cell = new ListCell<String>() {
                    {
                        super.setPrefWidth(10); // Set Drop Down Width 
                    }

                    @Override
                    public void updateItem(String item,
                            boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null) {
                            setText(item);

                        } else {
                            setText(null);
                        }
                    }
                };
                return cell;
            }
        });
        final Label label = new Label();

        Button btn = new Button();
        btn.setText("Read comboBox");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                label.setText("selectd: " + comboBox.getValue());
            }
        });

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(label, comboBox, btn);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}