在javafx中无法解析组合框

时间:2013-02-01 06:37:08

标签: javafx

我想用一个包含项目的组合框来设计一个应用程序。而选择我想给用户自动完成建议。但是这个组合框没有得到解决。我附上了文件。

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";

    }

}

请帮忙......

1 个答案:

答案 0 :(得分:1)

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