(已解决)如何在ChoiceBoxes中实现选择-SceneBuilder-JavaFX

时间:2019-07-15 22:21:26

标签: javafx

我知道这个问题已经在很多文章中讨论过,并且已经被询问过,但仍然无法解决。

问题: 我使用SceneBuilder制作了fxml,现在我想将Choices添加到我的ChoiceBox中(听起来很简单,我知道)。但是,经过数小时的尝试,观看了教程,阅读了文章等等,等等。我仍然没有将选择添加到我的选择框中(我的程序不会崩溃,它不会显示任何选择)

控制器的一部分:

    public class Controller{


@FXML
private Button btn;


@FXML
private ChoiceBox<String> CL1;




@FXML
private ChoiceBox CL2;

@FXML
private TextField TL2;

@FXML
private TextField TL1;


@FXML
private void initialize() {

    CL1 = new ChoiceBox();
    btn = new Button();

CL1.setValue("test"); // Not working

CL1.setItems(FXCollections.
 observableArrayList("24566", "23546"));            



}

FXML:

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1100.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button id="btn" fx:id="btn" layoutY="2.0" mnemonicParsing="false" onAction="#btnclicked" style="-fx-background-color: #666666; -fx-border: none; -fx-text-fill: black; -fx-alignment: center; -fx-padding: 15px 32px; -fx-font-size: 26px;" text="Zurück" textAlignment="CENTER" />
      <ChoiceBox id="CL1" fx:id="CL1" layoutX="262.0" layoutY="115.0" prefHeight="25.0" prefWidth="170.0" />
      <ChoiceBox id="CL2" fx:id="CL2" layoutX="658.0" layoutY="115.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField layoutX="262.0" layoutY="332.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField id="TL2" fx:id="TL2" layoutX="658.0" layoutY="170.0" prefHeight="25.0" prefWidth="170.0" />
      <ChoiceBox layoutX="262.0" layoutY="282.0" prefHeight="25.0" prefWidth="170.0" />
      <ChoiceBox layoutX="658.0" layoutY="282.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField id="TL1" fx:id="TL1" layoutX="262.0" layoutY="170.0" prefHeight="25.0" prefWidth="170.0" />
      <Button layoutX="474.0" layoutY="140.0" mnemonicParsing="false" prefHeight="44.0" prefWidth="152.0" style="-fx-background-color: #009900;" text="Umrechnen">
         <font>
            <Font size="20.0" />
         </font>
      </Button>
      <TextField layoutX="658.0" layoutY="332.0" prefHeight="25.0" prefWidth="170.0" />
      <ChoiceBox layoutX="658.0" layoutY="434.0" prefHeight="25.0" prefWidth="170.0" />
      <ChoiceBox layoutX="262.0" layoutY="434.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField layoutX="262.0" layoutY="484.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField layoutX="658.0" layoutY="484.0" prefHeight="25.0" prefWidth="170.0" />
      <Button layoutX="475.0" layoutY="307.0" mnemonicParsing="false" prefHeight="44.0" prefWidth="152.0" style="-fx-background-color: #009900;" text="Umrechnen">
         <font>
            <Font size="20.0" />
         </font>
      </Button>
      <Button layoutX="475.0" layoutY="437.0" mnemonicParsing="false" prefHeight="44.0" prefWidth="152.0" style="-fx-background-color: #009900;" text="Umrechnen">
         <font>
            <Font size="20.0" />
         </font>
      </Button>
   </children>
</AnchorPane>

(正如我已经说过的,这很可能已经被问到了)

希望您能帮助我:) 谢谢

0 个答案:

没有答案