作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的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 像素)。
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/
我是一名优秀的程序员,十分优秀!