我在javafx和场景构建器中显示组合框选项时遇到问题。
首先我尝试使用数组,我猜这不起作用。然后我尝试使用ObservableList<String>
并创建组合框的实例,如
ComboBox combo = new ComboBox(list);
这仍然不会显示组合框选项。 控制器类:
package javafxapplication1;
import javafxapplication1.JavaFXApplication1;
import java.net.URL;
import static java.util.Collections.list;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javax.lang.model.element.Element;
/**
*
* @author KJ4CC
*/
public class FXMLDocumentController implements Initializable {
ObservableList<String> options =
FXCollections.observableArrayList(
"Option 1",
"Option 2",
"Option 3"
);
@FXML
private Label label;
@FXML
private TextField dateText;
@FXML
private TextField time;
@FXML
private ComboBox band;
public void setTimeDate(){
JavaFXApplication1 javaFXApp = new JavaFXApplication1();
dateText.setText(javaFXApp.getDate());
}
public void setTime(){
JavaFXApplication1 javaFXApp = new JavaFXApplication1();
time.setText(javaFXApp.getTime());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
band = new ComboBox(options);
}
}
主要java类:
public class JavaFXApplication1 extends Application {
private FXMLDocumentController initScene;
private DateFormat dtf;
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public String getDate(){
dtf = new SimpleDateFormat("dd/MM/yy");
Date dateobj = new Date();
return dtf.format(dateobj);
}
public String getTime(){
dtf = new SimpleDateFormat("HH:mm;ss");
Date dateobj = new Date();
return dtf.format(dateobj);
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="383.0" prefWidth="590.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController">
<top>
<VBox prefHeight="235.0" prefWidth="543.0" BorderPane.alignment="CENTER">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<TableView prefHeight="210.0" prefWidth="634.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
<VBox.margin>
<Insets left="2.0" right="2.0" />
</VBox.margin>
</TableView>
</children>
</VBox>
</top>
<bottom>
<VBox prefHeight="160.0" prefWidth="676.0" BorderPane.alignment="CENTER">
<children>
<HBox prefHeight="22.0" prefWidth="625.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Date:" wrappingWidth="83.462890625">
<HBox.margin>
<Insets left="5.0" right="20.0" />
</HBox.margin>
</Text>
<Label onMouseClicked="#setTimeDate" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Time:" wrappingWidth="73.40625" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Call:" wrappingWidth="122.94921875">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Band:" wrappingWidth="58.443359375" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Freq:" wrappingWidth="106.974609375" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Location:" />
</children>
</HBox>
<HBox prefHeight="35.0" prefWidth="625.0">
<children>
<TextField id="dateText" fx:id="dateText" prefHeight="25.0" prefWidth="94.0">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</TextField>
<TextField fx:id="time" layoutX="10.0" layoutY="10.0" prefHeight="25.0" prefWidth="71.0">
<padding>
<Insets left="5.0" />
</padding>
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</TextField>
<TextField layoutX="104.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TextField>
<ComboBox fx:id="band" prefHeight="25.0" prefWidth="57.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</ComboBox>
<TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TextField>
<TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0" />
</children>
</HBox>
<HBox prefHeight="30.0" prefWidth="580.0">
<children>
<Text fill="#14bdd7" onMouseClicked="#setTimeDate" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Date">
<HBox.margin>
<Insets right="20.0" />
</HBox.margin>
</Text>
<Text fill="#14bdd7" layoutX="10.0" layoutY="23.0" onMouseClicked="#setTime" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Time" />
</children>
</HBox>
</children>
<BorderPane.margin>
<Insets left="5.0" right="5.0" />
</BorderPane.margin>
</VBox>
</bottom>
</BorderPane>
任何帮助将不胜感激!我对这个场景构建器的东西很新,并使用javafx。我只是在摸索它。谢谢你的帮助!
答案 0 :(得分:-1)
好的,我找出了问题所在。我正在创建一个组合框的新实例,从而创建一个空实例。以下是修改后的工作代码的方法。
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> options =
FXCollections.observableArrayList(
"Option 1",
"Option 2",
"Option 3"
);
band.setItems(options);
}