1

我想设计一个包含项目的组合框的应用程序。并且在选择时我想给用户自动完成建议。但是在执行此组合框时并没有得到解决。我已附上文件。

Ademo.fxml

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

<?import java.lang.*?>
<?import javafx.collections.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="268.0" prefWidth="290.0" xmlns:fx="http://javafx.com/fxml" fx:controller="v.C_hgj">
  <!-- TODO Add Nodes -->
  <children>
    <ComboBox layoutX="110.0" layoutY="98.0" fx:id="fruitCombo" prefWidth="90.0" promptText="choose">
      <items>
        <FXCollections fx:factory="observableArrayList">
          <String fx:value="Apple" />
          <String fx:value="orange" />
          <String fx:value="mango" />
          <String fx:value="banana" />
          <String fx:value="guava" />
          <String fx:value="water melon" />
        </FXCollections>
      </items>
    </ComboBox>
  </children>
</AnchorPane>

V_hgj.java

package v;

    import java.util.logging.Level;
    import java.util.logging.Logger;

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;

    public class V_hgj extends Application{


        public static void main(String[] args) {
            launch(V_hgj.class, (java.lang.String[])null);

        }

        @Override
        public void start(Stage primaryStage) throws Exception {
            try
            {

                //loading .fxml file

                AnchorPane ap=FXMLLoader.load(V_hgj.class.getResource("Ademo.fxml"));
                Scene sc=new Scene(ap);
                primaryStage.setScene(sc);
                primaryStage.setTitle("Product");


                //getting external .css file 
              //  primaryStage.getScene().getStylesheets().add("file:/~/helper/gui.css");
                primaryStage.show();
            }
            catch(Exception ex)
            {
                Logger.getLogger(V_product.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

C_hgj.java

package v;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class C_hgj implements Initializable{

    @FXML
    private ComboBox fruitCombo;
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        assert fruitCombo!=null : "reset botton not injected";

    }

}

请帮忙 ....

4

1 回答 1

2

在 C_hgj 类(请遵循 Java 代码约定)中,您需要导入 javafx.scene.control.ComboBox

于 2013-02-01T13:20:36.703 回答