Java 组合框选项中的文本对齐方式
我有一个JavaFX组合框,有几个文本选项。如果选择使用中心对齐而不是左对齐,那就太好了,但我还没有弄清楚如何做到这一点 以下是我使用的样式。我添加了“-fx text alignment:center;”子句,但它对组合框中字符串的位置没有影响Java 组合框选项中的文本对齐方式,java,javafx,javafx-8,Java,Javafx,Javafx 8,我有一个JavaFX组合框,有几个文本选项。如果选择使用中心对齐而不是左对齐,那就太好了,但我还没有弄清楚如何做到这一点 以下是我使用的样式。我添加了“-fx text alignment:center;”子句,但它对组合框中字符串的位置没有影响 normalStyle = "-fx-font-family:san serif;" + "-fx-font-size:12;" + "-fx-text-alignment:center;"
normalStyle = "-fx-font-family:san serif;"
+ "-fx-font-size:12;"
+ "-fx-text-alignment:center;"
+ "-fx-font-weight:normal;";
样式将附加到组合框,如下所示:
cbChoices.setStyle(normalStyle);
我注意到组合框条目的大小和权重将响应上面的更改,但不会响应对齐方式
我不希望在字符串的开头添加空格以使它们对齐。还有其他方法吗?您需要在
组合框
中的每个列表单元格
上设置样式,而不是在组合框
本身上设置样式
您可以通过使用setCellFactory()
方法提供自己的ListCell
实现来实现这一点:
// Provide our own ListCells for the ComboBox
comboBox.setCellFactory(lv -> new ListCell<String>() {
// We override the updateItem() method
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
// Set the style for this ListCell
setStyle("-fx-alignment: center");
// If there is no item for this cell, leave it empty, otherwise show the text
if (item != null && !empty) {
setText(item);
} else {
setText(null);
}
}
});
//为组合框提供我们自己的列表单元格
comboBox.setCellFactory(lv->new ListCell(){
//我们重写updateItem()方法
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
//设置此列表单元格的样式
设置样式(“-fx对齐:中心”);
//如果此单元格没有项目,请将其留空,否则显示文本
如果(项!=null&&!空){
setText(项目);
}否则{
setText(空);
}
}
});
示例应用程序:
导入javafx.application.application;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.ListCell;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类ComboBoxAlignment扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
//简单接口
VBox根=新的VBox(5);
根。设置填充(新插图(10));
根部设置对齐(位置中心);
//带有项目的简单组合框
ComboBox ComboBox=新建ComboBox();
comboBox.getItems().addAll(“红色”、“橙色”、“黄色”、“绿色”、“蓝色”、“靛蓝”、“紫色”);
//为组合框提供我们自己的列表单元格
comboBox.setCellFactory(lv->new ListCell(){
//我们重写updateItem()方法
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
//设置此列表单元格的样式
设置样式(“-fx对齐:中心”);
//如果此单元格没有项目,请将其留空,否则显示文本
如果(项!=null&&!空){
setText(项目);
}否则{
setText(空);
}
}
});
root.getChildren().add(组合框);
//上台
初级阶段。设置宽度(300);
初生阶段:坐位高度(300);
primaryStage.setScene(新场景(根));
primaryStage.show();
}
}
结果:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ComboBoxAlignment extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
// Simple ComboBox with items
ComboBox<String> comboBox = new ComboBox<>();
comboBox.getItems().addAll("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet");
// Provide our own ListCells for the ComboBox
comboBox.setCellFactory(lv -> new ListCell<String>() {
// We override the updateItem() method
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
// Set the style for this ListCell
setStyle("-fx-alignment: center");
// If there is no item for this cell, leave it empty, otherwise show the text
if (item != null && !empty) {
setText(item);
} else {
setText(null);
}
}
});
root.getChildren().add(comboBox);
// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}