Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javafx中更新组合框中的文本字体_Java_Javafx - Fatal编程技术网

在javafx中更新组合框中的文本字体

在javafx中更新组合框中的文本字体,java,javafx,Java,Javafx,我有一部分密码。我想在组合框中显示操作系统上的所有字体。然后字体名称应该看起来像一个预览,显示每个字体的外观。这是我的代码: List<String> families = Font.getFamilies(); fontfamilies = FXCollections.observableArrayList(families); comboBox.setItems(fontfamilies); comboBox.getSelectionModel().select(0); com

我有一部分密码。我想在组合框中显示操作系统上的所有字体。然后字体名称应该看起来像一个预览,显示每个字体的外观。这是我的代码:

List<String> families = Font.getFamilies();
fontfamilies = FXCollections.observableArrayList(families);
comboBox.setItems(fontfamilies);
comboBox.getSelectionModel().select(0);

comboBox.setCellFactory((ListView<String> listView) -> {
    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(new Font(item, 12));
            }
        }
    };
    //cell.setPrefWidth(120);
    return cell;
});
List families=Font.getFamilies();
fontfamilies=FXCollections.observableArrayList(系列);
comboBox.setItems(fontfamilies);
comboBox.getSelectionModel().select(0);
comboBox.setCellFactory((ListView ListView)->{
最终ListCell单元格=新ListCell(){
@凌驾
public void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
如果(项!=null){
setText(项目);
setFont(新字体(第12项));
}
}
};
//单元格宽度(120);
返回单元;
});
但我得到了这个错误:

Error:(59, 20) java: incompatible types: bad return type in lambda expression
javafx.scene.control.ListCell<java.lang.String> cannot be converted to javafx.scene.control.ListCell<capture#1 of ?>
错误:(59,20)java:不兼容类型:lambda表达式中的返回类型错误
无法将javafx.scene.control.ListCell转换为javafx.scene.control.ListCell

有人能帮我解决这个问题吗?

你的问题与你如何定义你的成员变量
comboBox
有关,你定义它时使用了一个表示未知类型的,而你的其余代码期望
字符串
comboBox
的类型

因此,只需将其定义为下一步:

@FXML private ComboBox<String> comboBox;
@FXML私有组合框组合框;

请说明您是如何定义组合框的
我是这样定义的:@FXML private comboBox comboBox;我的错,我以前没见过,应该是个好主意!唯一缺少的是组合框的选定值也使用相同的字体!