从JList中的ArrayList显示集类型的项

时间:2016-04-12 19:15:44

标签: java eclipse swing arraylist actionlistener

我已经为一家餐馆设置了一个GUI,它会在JList中显示来自ArrayList的项目,因此它不会显示我拥有的所有项目,所以当你点击说一个Mains按钮时它会显示主菜,当点击甜点时,它会显示甜点等。我试图在主菜行动监听器中实现它,但它仍然显示所有项目。

// sets up the class MenuItem, this can then be used to generate different types of food and put them
//into catagories like starter, main course etc
//@Jonathan Gedlek
//@Version 1.0 31/03/16


public class MenuItem
{  
    public static String typeFood;
    private String foodName;
    private String foodType;
    private float price;
    private int calories;    

    /**
     * Constructor for objects of class MenuItem
     */
    public MenuItem(String nameFood, String typeFood, float foodPrice, int caloryCount)

    {     
    foodName = nameFood;
    foodType = typeFood;
    price = foodPrice;
    calories = caloryCount;

   } 

   public String foodName()   
   {
     return foodName;       
   }

   public String foodType()   
   {
      return foodType; 
   }

   public float price()
   {
       return price;
   }

   public int  calories()
   {
       return calories;
    }


}

这是我的Menu Item类,因此它正在设置要添加到ArrayList中的对象。

import java.util.ArrayList;


/**
 * 
 * ArrayList for the class, will hold all food items
 * @author Jonathan
 * @version 1.0
 *
 */

public class RestaurantArrayList extends MenuItem
{


    public RestaurantArrayList(String nameFood, String typeFood, float foodPrice, int caloryCount) {
        super(nameFood, typeFood, foodPrice, caloryCount);


    }


    public static final ArrayList<MenuItem> items;

    static 
    {
        items = new ArrayList<>();
        items.add(new MenuItem("Coca Cola", "Drink", 3.00f, 38));
        items.add(new MenuItem("Fanta Orange", "Drink", 3.00f, 31 ));
        items.add(new MenuItem("Glass of Red Wine", "Drink", 5.00f, 85));
        items.add(new MenuItem("Glass of White Wine", "Drink", 5.00f, 82));
        items.add(new MenuItem("Carling", "Drink", 3.50f, 189));
        items.add(new MenuItem("Fosters", "Drink", 3.50f, 378));
        items.add(new MenuItem("Water", "Drink", 0.00f, 0));
        items.add(new MenuItem("Breads", "Starter", 5.00f, 150));
        items.add(new MenuItem("Cold Meat", "Starter", 5.00f, 150));
        items.add(new MenuItem("Potato Skins and Barbeque Sauce", "Starter", 5.00f, 500));
        items.add(new MenuItem("Cold Meat", "Starter", 5.00f, 400));
        items.add(new MenuItem("Garlic Bread and Cheese", "Starter", 4.50f, 450));
        items.add(new MenuItem("Steak", "Main", 13.50f, 750));
        items.add(new MenuItem("Cheese and Bacon Burger", "Main", 8.00f, 850));
        items.add(new MenuItem("Spaghetti Cabonara", "Main", 7.00f, 675));
        items.add(new MenuItem("Steak", "Main", 13.50f, 378));
        items.add(new MenuItem("Seafood Paella", "Main", 10.00f, 850));
        items.add(new MenuItem("Chocolate Gateau with Cream", "Dessert", 5.00f, 179));
        items.add(new MenuItem("Apple Pie and Cream", "Dessert", 5.00f, 237));
        items.add(new MenuItem("Lemon Cheese Cake", "Dessert", 5.00f, 250));
        items.add(new MenuItem("Strawberries and Cream", "Dessert", 3.00f, 200));


    }

    public static String getFoodName(int index)
    {
        return items.get(index).foodName();
    }

    public static String getFoodType(int index)
    {
        return items.get(index).foodType();

    }

    public static int getCalories(int index)
    {
        return items.get(index).calories();
    }

    public static float getPrice(int index)
    {
        return items.get(index).price();
    }

    public static int getListSize()
    {
        return items.size();
    }
}

这是我的ArrayList类,我试图让它只显示列表中foodType设置为Main的项目。

import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

class FoodListRenderer extends DefaultListCellRenderer {

/**
     * 
     */
    private static final long serialVersionUID = 1L;

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    String name = ((MenuItem) value).foodName();
    return super.getListCellRendererComponent(list, name, index, isSelected, cellHasFocus);
}}

这是我的FoodListRenderer类

JButton btnMainMenu = new JButton("Main Menu");
btnMainMenu.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        for (int i = 0; i<RestaurantArrayList.getListSize();i++)
        {
            if(RestaurantArrayList.getFoodType(i) == "Main")
            {
                JList foodList = new JList(RestaurantArrayList.items.toArray());
                foodList.setCellRenderer(new FoodListRenderer());
                scrollPane_1.setViewportView(foodList);
            }
            else
            {

            }
        }

    }
});

最后这是我的按钮,我将按此按钮显示主要课程项目。 没有任何语法错误,但仍然不会按照我的意图行事。任何帮助将不胜感激。

0 个答案:

没有答案