gpt4 book ai didi

user-interface - 如何在JavaFX中设置ComboBox的下拉宽度?

转载 作者:行者123 更新时间:2023-12-03 00:25:14 24 4
gpt4 key购买 nike

我的ComboBox的下拉列表扩展到其中输入的最大元素的大小。不过我希望它是固定大小。

---------------------
| Small Combobox | V |
--------------------------------------------
| "Long item 1" |
--------------------------------------------
| "Long item 2" |
--------------------------------------------
| "Long item 3" |
--------------------------------------------

最佳答案

使用CSS

/** file: combo-size.css */

/** Size the combo-box button. */
.combo-box {
-fx-pref-width: 100;
}

/** Size the combo-box drop down list. */
.combo-box-popup > .list-view {
-fx-pref-width: 100;
}

注意:我仅在 Java 8 上测试了此示例,我相信 -fx-*-width-fx-*-height css 属性可能会对于 JavaFX 8 来说是新的。

使用示例

在此示例中,组合框按钮和下拉列表的大小均已调整为相同的首选宽度(100 像素)。

enter image description here

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SizedComboBoxSampleWithCss extends Application {
public static void main(String[] args) { launch(args); }

@Override public void start(Stage stage) {
final ComboBox<String> combo = new ComboBox<>();

combo.setValue(Font.getDefault().getFamily());
combo.getItems().setAll(Font.getFamilies());
combo.getStylesheets().add(
getClass().getResource(
"combo-size.css"
).toExternalForm()
);

StackPane layout = new StackPane(combo);
layout.setPadding(new Insets(10));

stage.setScene(new Scene(layout));
stage.show();
}
}

关于user-interface - 如何在JavaFX中设置ComboBox的下拉宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22190370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com