javafx - 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<>();
}
任何帮助,将不胜感激 :)
解决方案
推荐阅读
- vue.js - 如何使用 Vue.js 在 Chart.js 中添加 Y 轴标签?
- java - 从 Firebase 检索 Base64 编码的图像并将其显示在 Android 应用程序上
- r - ggplot2 和 dplyr,可视化作为字符的列
- docker - Docker compose 错误:“./docker-compose.yml”第 16 行第 18 列中不允许映射值
- python - 如何删除以元素结尾的行
- angular - 带有分页的角材料卡有错误
- vba - 如何使用 VBA 更改 PowerPoint 中图表的默认数据?
- php - 有没有一种简单的方法可以像在 Node.JS 中那样异步执行 PHP 代码?
- tensorflow - bert_en_uncased_preprocess 是如何制作的?例如通过训练 NN 或手动编码?
- react-native - 如何在 MQTT 中使用特定类型的 clientId 创建客户端?