【发布时间】:2021-03-05 12:32:00
【问题描述】:
我正在尝试制作一个包含组合框的简单 GUI,并且我希望该框具有“员工”和“经理”选项。但是,由于某种原因,我的组合框没有被填充,我不确定为什么。这是我的 FXML 文件的代码:
<AnchorPane fx:id="mainpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="30.0" layoutY="14.0" text="Database status " />
<TextField fx:id="username" layoutX="30.0" layoutY="125.0" />
<Label layoutX="30.0" layoutY="99.0" text="Username" />
<Label layoutX="30.0" layoutY="174.0" text="Password" />
<PasswordField fx:id="password" layoutX="30.0" layoutY="200.0" />
<ComboBox fx:id="combobox" layoutX="30.0" layoutY="268.0" prefWidth="150.0" promptText="Manager/Employee" />
<Button fx:id="Login" layoutX="61.0" layoutY="338.0" mnemonicParsing="false" text="Login" />
<Label fx:id="dbstatus" layoutX="186.0" layoutY="14.0" text="Label" />
</children>
</AnchorPane>
这段代码应该设置组合框的选项:
public void initialize(URL url, ResourceBundle RB){
if (this.loginmodel.isDBconnected()){
this.dbstatus.setText("Connected");
}
else{
this.dbstatus.setText("Not connected");
}
this.combobox.setItems(FXCollections.observableArrayList(Option.values())); //this piece sets the options of the combo box
}
这是上面代码中引用的我的选项枚举:
public enum Option {
Manager, Employee;
Option(){}
public String value(){
return name();
}
public static Option fromvalue(String value){
return valueOf(value); // returns the enum constant of that type
}
使用此代码,当我的组合框下拉时,当前没有选项,它看起来像这样:
我能做些什么来解决这个问题?
【问题讨论】:
-
提供minimal reproducible example。 可能问题是您的 FXML 文件的根元素中没有
fx:controller属性,但是如果没有完整的示例,就不可能知道这是否是问题所在。 -
@James_D 是的,这就是问题所在,我忘记了 FXML 文件根目录中的控制器属性。谢谢。
-
您好 :) 您的代码中的
dbStatus是什么?
标签: java javafx combobox fxml scene