从JScrollPane中的对象的ArrayList显示变量 [英] Displaying a variable from an ArrayList of objects in a JScrollPane
问题描述
一个具有静态ArrayList和一个设置了UI,我想要显示每个对象foodName的列表,因为这将是可以在在线菜单GUI上选择和订购的食品。
one with a static ArrayList and one with a UI that is set up, I am wanting to show a list of each of the objects foodName because this is going to be food items that can be selected and ordered on an online menu 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的类。 p>
This above is my class for each 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类。我想要在文本区域显示食物的名称,但我不知道该怎么做。如果有人可以帮助那将是伟大的。感谢
Here is the ArrayList Class I have also set up. I am wanting to just display the name of the food in the text area but I am not sure how to do this. If anyone can help that would be great. Thanks
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(219, 50, 134, 309);
contentPane.add(scrollPane_1);
这是一段代码设置JScrollPane的代码,我想要的信息是
this is a little snippet of code setting up a JScrollPane as well that I am wanting the information to be displayed inside, which is within its own GUI class.
推荐答案
我将使用 JList 与自定义 DefaultListCellRenderer 。单元格渲染器确定单元格中的对象是如何显示的,因此在您的情况下,您需要 JList
中的 MenuItem
对象,渲染器应该提取用于显示的名称字符串。这是我起草的:
I would use a JList with a custom DefaultListCellRenderer. Cell renderers determine how the objects in a cell is shown, so in your case you want MenuItem
objects in your JList
and the renderer should extract the name string for showing. Here's what I drafted:
创建一个JList并将其添加到JScrollPane的视口中:
Create a JList and add it to the viewport of a JScrollPane:
JList foodList = new JList(RestaurantArrayList.items.toArray());
foodList.setCellRenderer(new FoodListRenderer());
JScrollPane scr = new JScrollPane(foodList);
和自定义ListCellRenderer类:
and custom ListCellRenderer class:
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);
}}
这篇关于从JScrollPane中的对象的ArrayList显示变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!