gpt4 book ai didi

java - 在 JScrollPane 中显示对象 ArrayList 中的变量

转载 作者:行者123 更新时间:2023-12-01 18:05:55 26 4
gpt4 key购买 nike

一个具有静态 ArrayList,一个具有已设置的 UI,我想显示每个对象 foodName 的列表,因为这将是可以在在线菜单 GUI 上选择和订购的食品项目.

    public class MenuItem
{
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;
}
}

上面是我为每个 MenuItem 编写的类。

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));
}
}

这是我也设置的 ArrayList 类。我想在文本区域中显示食物的名称,但我不知道该怎么做。如果有人可以提供帮助那就太好了。谢谢

JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(219, 50, 134, 309);
contentPane.add(scrollPane_1);

这是设置 JScrollPane 的一小段代码,我希望在其内部显示信息,该信息位于其自己的 GUI 类中。

最佳答案

我会使用JList与定制DefaultListCellRenderer 。单元格渲染器确定单元格中对象的显示方式,因此在您的情况下,您需要 JList 中的 MenuItem 对象,并且渲染器应提取名称字符串以进行显示。这是我起草的:

创建一个 JList 并将其添加到 JScrollPane 的视口(viewport)中:

JList foodList = new JList(RestaurantArrayList.items.toArray());
foodList.setCellRenderer(new FoodListRenderer());
JScrollPane scr = new JScrollPane(foodList);

和自定义ListCellRenderer类:

class FoodListRenderer extends DefaultListCellRenderer {

@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);
}}

关于java - 在 JScrollPane 中显示对象 ArrayList 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36429167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com