尝试从数组列表中获取项目以显示在网格中

时间:2018-12-05 15:11:42

标签: java arraylist javafx-8

我试图在网格中显示我的arraylist,但是由于某种原因,我无法显示该arraylist。我下面有我的主类,还有包含我的arraylist的OrderMenu类。我更喜欢将arraylist显示在网格中,因为我试图制作一个披萨点菜系统,并且网格使我更容易使用。最好是一个网格,其中arraylist中的每个项目一行12行,两行一列的名称和价格。

希望你们能有所帮助。

public class PizzaOrderingSystem extends Application {  

private OrderMenu menu;
public ArrayList<MenuItem> menuItems;
private GridPane grid;
private BorderPane border;

//Creating Stage and layout for Stage
@Override
public void start(Stage stage) {

    stage.setTitle("Pizza Ordering System");
    stage.setWidth(600);
    stage.setHeight(600);

    this.menu.menuItems.size();

    int numRows = 2;
    int numColumns = 12;

    for (int i = 0; i < numColumns; i++) {
    ColumnConstraints cc = new ColumnConstraints(40);
    cc.setPercentWidth(80);
    cc.setHalignment(HPos.CENTER);
    grid.getColumnConstraints().add(cc);

    for (int row = 0; row < numRows; row++) {
    RowConstraints rc = new RowConstraints(40);
    rc.setPercentHeight(80);
    rc.setValignment(VPos.CENTER);
    grid.getRowConstraints().add(rc);            }

    }   

    border.setCenter(grid);

    stage.setTitle("Pizza Ordering System");
    stage.setWidth(600);
    stage.setHeight(600);

    //Attacing borderPane to scene
    stage.setScene(new Scene(border));
    stage.getScene();
    stage.show();
    }

     public static void main(String[] args) {
    launch(args);
}  
}      

public class OrderMenu {


public ArrayList<MenuItem> menuItems;


public OrderMenu(){

            menuItems.add(new Pizza("Margherita", 50));
            menuItems.add(new Pizza("Hawaii", 55));
            menuItems.add(new Pizza("Marinara", 70));
            menuItems.add(new Pizza("Meat Lovers", 70));
            menuItems.add(new Pizza("Calzone", 60));

            menuItems.add(new Burger("Burger", 60));
            menuItems.add(new Burger("Cheeseburger", 65));
            menuItems.add(new Burger("Baconburger", 60));

            menuItems.add(new Soda("Coca cola", 25));
            menuItems.add(new Soda("Coca cola light", 25));             
            menuItems.add(new Soda("Fanta", 25));
            menuItems.add(new Soda("Faxe kondi", 25));  

}

}

1 个答案:

答案 0 :(得分:0)

查看您的代码,我看不到您尝试在哪里使用在OrderMenu类中创建的menuItem。我认为您的意思不是它们是javafx menuItems。另外,您似乎并没有创建私有字段的实例,因此我认为在尝试使用它们时,它们全都为空。

这是您所拥有内容的简化,可以在网格上显示比萨饼和价格列表。我没有添加任何东西来调整应用程序的大小,因此它看起来有些混乱。

主应用

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class PizzaOrderingSystem extends Application {  

    @Override
    public void start(Stage stage) {

        GridPane grid = new GridPane();

        int row = 0;
        for (OrderItem item : OrderMenu.getItems()) {
            grid.add(new Label(item.getName()), 0, row);
            grid.add(new Label(String.valueOf(item.getPrice())), 1, row);
            row++;
        }

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(grid);

        stage.setTitle("Pizza Ordering System");

        stage.setScene(new Scene(borderPane));

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }  
}      

OrderItem的接口

public interface OrderItem {

    public String getName();
    public int getPrice();
}

实现OrderItem的Pizza类

public class Pizza implements OrderItem {

    private String name;
    private int price;

    public Pizza(String name, int price) {
        this.setName(name);
        this.setPrice(price);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}

还有一个带有静态方法的OrderMenu,用于获取订单商品列表。

import java.util.ArrayList;
import java.util.List;

public class OrderMenu {

    public static List<OrderItem> getItems(){
        List<OrderItem> menuItems = new ArrayList<>();

        menuItems.add(new Pizza("Margherita", 50));
        menuItems.add(new Pizza("Hawaii", 55));
        menuItems.add(new Pizza("Marinara", 70));
        menuItems.add(new Pizza("Meat Lovers", 70));
        menuItems.add(new Pizza("Calzone", 60));

        return menuItems;
    }

}