【发布时间】:2016-08-02 21:18:39
【问题描述】:
我有一个包含多个对象的数组列表,这些对象显示在 JList 面板中。我想选择一个对象,当我按下一个按钮时,它会将所选项目添加到另一个 ArrayList。那个也将显示在第二个 JList 上。
下面的代码显示了我到目前为止所做的工作:
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,其中我的所有项目都添加到了 Array 中。
JButton button = new JButton(">>");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button.setBounds(333, 180, 59, 25);
contentPane.add(button);
这是我需要使用动作侦听器的按钮。但我不确定将 什么 放入动作监听器中。第二个数组我也没有,因为我不知道怎么设置,所以我可以动态添加对象。
如果我以一种奇怪的方式处理这件事,那么我愿意接受建议,请记住我是新手,所以我可能会以一种冗长的方式来处理它。
【问题讨论】:
-
您的代码不够完整和清晰。您的代码中定义的另一个
Arraylist在哪里?请提供stackoverflow.com/help/mcve -
我不确定你的意思,你说的是我展示的 ArrayList 还是我说我还没做的那个?
-
类似this?
-
button.setBounds(333, 180, 59, 25);Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。 -
@AndrewThompson:非常感谢您的提示,一直向您学习。我正在寻找如何嵌入它!再次感谢!