首页 > 解决方案 > While 循环导致我的 JavaFX 应用程序崩溃

问题描述

我正在尝试检查我的 ArrayLists(存储在 GrocerieList 类中)并将它们添加到我的 TreeView 分支中。我可以在不经过 while 循环的情况下显示单个的。由于它们是动态的,但是我需要某种循环。目前 ArrayList foodAmount ("4" ) 和 foodList ("pizza") 都各持有一项。

这是我的 while 循环代码(为了便于阅读,我排除了其他分支):

public class Main extends Application {

    Stage window;
    TextField nameInput, quantityInput;
    TreeView<String> tree;

    GrocerieList shoppingList = new GrocerieList();
    Storage storage = new Storage();
    static Scanner scan = new Scanner(System.in);
    static String item;
    static String quant;
    static String exit = "exit";
    static int menuchoice;
    static int catchoice;
    static int marketchoice;

    public static void main(String[] args) throws FileNotFoundException {
        loadFile();
        launch(args);

    }

    public void start (Stage primaryStage) throws Exception{

        window = primaryStage;
        window.setTitle("GrocerieList");

        // tree view
        TreeItem<String> root, food, drink, hygiene, other;

        root = new TreeItem<>();
        root.setExpanded(true);

        // food
        food = makeBranch("Food",root);
        while(GrocerieList.foodList.listIterator().hasNext()) {
            makeBranch("[" + GrocerieList.foodAmount.listIterator().next() + "] " + GrocerieList.foodList.listIterator().next(), food);
        }

        // tree
    tree = new TreeView<>(root);
    tree.setShowRoot(false);
    tree.getStyleClass().add("myTree");

    // bottom input fields
    nameInput = new TextField();
    nameInput.setPromptText("Productname");
    nameInput.setMinWidth(100);

    quantityInput = new TextField();
    quantityInput.setPromptText("Quantity");
    quantityInput.setMinWidth(100);

    // buttons
    Button addbutton = new Button("Add");

    // Layout
    // HBOX
    HBox hBox = new HBox();
    hBox.setPadding(new Insets(10));
    hBox.setSpacing(30);
    hBox.getChildren().addAll(nameInput, quantityInput, addbutton);

    // BorderPane
    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(tree);
    borderPane.setBottom(hBox);
    borderPane.setPadding(new Insets(20));

    Scene scene = new Scene(borderPane, 520, 620);
    scene.getStylesheets().add("Viper.css");
    window.setScene(scene);
    window.show();

这是包含我的 ArrayLists 的类:

public class GrocerieList {

    static ArrayList<String> foodList = new ArrayList<>();
    static ArrayList<String> hygieneList = new ArrayList<>();
    static ArrayList<String> drinkList = new ArrayList<>();
    static ArrayList<String> otherList = new ArrayList<>();

    static ArrayList<String> foodAmount = new ArrayList<>();
    static ArrayList<String> hygieneAmount = new ArrayList<>();
    static ArrayList<String> drinkAmount = new ArrayList<>();
    static ArrayList<String> otherAmount = new ArrayList<>();
}

任何帮助,将不胜感激 :)

标签: javafx

解决方案


推荐阅读