你好我这里有一个简单的清单,列出了将它们加到总成本上的食物。 (像收银机一样)我在制作删除按钮以从列表中删除项目并从总成本中减去时遇到问题。请帮助我不知道从哪里开始。这是代码。
// Import Core Java packages
import java.awt.*;
import java.awt.event.*;
public class FastFood extends Frame {
/**
*
*/
private static final long serialVersionUID = 1L;
// Initial Frame size
static final int WIDTH = 400; // frame width
static final int HEIGHT = 300; // frame height
// Arrays of selection items
static final String[] FRUIT_FOODS = {"Pear", "Apple", "Peach", "Banana"};
static final int[] FRUIT_FOODS_PRICE = {79, 69, 89, 59};
static final String[] BEVERAGE_DRINKS = {"Soda", "Coffee", "Milk", "Water"};
static final int[] BEVERAGE_DRINKS_PRICE = {79, 95, 85, 65};
static final String[] JUNK_FOODS = {"Burger", "Hotdog", "Fries", "Pizza"};
static final int[] JUNK_FOODS_PRICE = {199, 150, 99, 299};
static final String[] DESSERTS = {"Ice cream", "Pie", "Cake", "Donut"};
static final int[] DESSERTS_PRICE = {149, 249, 299, 99};
// List and Button control
List fruitFoodList;
List beverageList;
List junkFoodList;
List dessertList;
List itemsOrderedList;
Button addFruitFood;
Button addBeverage;
Button addJunkFood;
Button addDessert;
Button deleteItem;
// Total amount and display
Label amountLabel;
int amount = 0;
/**
* Constructor
*/
public FastFood() {
setTitle("Fast Food To Go");
setLayout(new BorderLayout());
// create display for item selection
Panel itemPanel = new Panel(new GridLayout(2, 1));
add(itemPanel, BorderLayout.CENTER);
// create display and control for junk food selection items
Panel junkFoodPanel = new Panel(new BorderLayout());
itemPanel.add(junkFoodPanel);
Label junkFood = new Label("Fast Food Items", Label.CENTER);
junkFood.setForeground(Color.red);
junkFoodPanel.add(junkFood, BorderLayout.NORTH);
junkFoodList = new List();
for(int i=0; i<JUNK_FOODS.length; i++) {
junkFoodList.add(JUNK_FOODS[i]+ " $" + (float)JUNK_FOODS_PRICE[i]/100);
}
junkFoodPanel.add(junkFoodList, BorderLayout.CENTER);
Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
junkFoodPanel.add(buttonPanel, BorderLayout.SOUTH);
addJunkFood = new Button("Get Food Item");
buttonPanel.add(addJunkFood);
//create a display for fruit items
Panel fruitFoodPanel = new Panel(new BorderLayout());
itemPanel.add(fruitFoodPanel);
Label fruitFood = new Label("Fruit Items", Label.CENTER);
fruitFood.setForeground(Color.red);
fruitFoodPanel.add(fruitFood, BorderLayout.NORTH);
fruitFoodList = new List();
for(int i=0; i<FRUIT_FOODS.length; i++) {
fruitFoodList.add(FRUIT_FOODS[i]+ " $" + (float)FRUIT_FOODS_PRICE[i]/100);
}
fruitFoodPanel.add(fruitFoodList, BorderLayout.CENTER);
buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
fruitFoodPanel.add(buttonPanel, BorderLayout.SOUTH);
addFruitFood = new Button("Get Fruit");
buttonPanel.add(addFruitFood);
// create display and control for dessert selection items
Panel dessertPanel = new Panel(new BorderLayout());
itemPanel.add(dessertPanel);
Label dessertLabel = new Label("Dessert Items", Label.CENTER);
dessertLabel.setForeground(Color.red);
dessertPanel.add(dessertLabel, BorderLayout.NORTH);
dessertList = new List();
for(int i=0; i<DESSERTS.length; i++) {
dessertList.add(DESSERTS[i]+ " $" + (float)DESSERTS_PRICE[i]/100);
}
dessertPanel.add(dessertList, BorderLayout.CENTER);
buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
dessertPanel.add(buttonPanel, BorderLayout.SOUTH);
addDessert = new Button("Get Dessert Item");
buttonPanel.add(addDessert);
//create a display for Beverages
Panel beveragePanel = new Panel(new BorderLayout());
itemPanel.add(beveragePanel);
Label beverage = new Label("Beverages", Label.CENTER);
beverage.setForeground(Color.red);
beveragePanel.add(beverage, BorderLayout.NORTH);
beverageList = new List();
for(int i=0; i<BEVERAGE_DRINKS.length; i++) {
beverageList.add(BEVERAGE_DRINKS[i]+ " $" + (float)BEVERAGE_DRINKS_PRICE[i]/100);
}
beveragePanel.add(beverageList, BorderLayout.CENTER);
buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
beveragePanel.add(buttonPanel, BorderLayout.SOUTH);
addBeverage = new Button("Get Beverage");
buttonPanel.add(addBeverage);
// create display and control for items ordered and amount
Panel orderedPanel = new Panel(new BorderLayout());
add(orderedPanel, BorderLayout.EAST);
// create display and control for items ordered
Panel itemsOrderedPanel = new Panel(new BorderLayout());
orderedPanel.add(itemsOrderedPanel, BorderLayout.CENTER);
Label itemsOrdered = new Label("Items Ordered", Label.CENTER);
itemsOrdered.setForeground(Color.red);
itemsOrderedPanel.add(itemsOrdered, BorderLayout.NORTH);
itemsOrderedList = new List();
itemsOrderedPanel.add(itemsOrderedList, BorderLayout.CENTER);
buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
itemsOrderedPanel.add(buttonPanel, BorderLayout.SOUTH);
deleteItem = new Button("Delete Ordered Item");
buttonPanel.add(deleteItem);
// create display for amount
Panel amountPanel = new Panel(new BorderLayout());
orderedPanel.add(amountPanel, BorderLayout.SOUTH);
Label totalLabel = new Label("Total Amount: ");
amountPanel.add(totalLabel, BorderLayout.WEST);
amountLabel = new Label("$0.00");
amountPanel.add(amountLabel, BorderLayout.CENTER);
// create and add list selection listener
SelectionListener selListener = new SelectionListener();
junkFoodList.addActionListener(selListener);
dessertList.addActionListener(selListener);
fruitFoodList.addActionListener(selListener);
beverageList.addActionListener(selListener);
// create and add button listener
ButtonListener buttonListener = new ButtonListener();
addJunkFood.addActionListener(buttonListener);
addDessert.addActionListener(buttonListener);
addFruitFood.addActionListener(buttonListener);
addBeverage.addActionListener(buttonListener);
} // end of constructor
/**
* Listener class for list selection
*/
class SelectionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String selectedItem;
int selectedIndex;
int itemPrice;
if(event.getSource() == junkFoodList) { // select item from junk food list
selectedIndex = junkFoodList.getSelectedIndex();
selectedItem = junkFoodList.getSelectedItem();
itemPrice = JUNK_FOODS_PRICE[selectedIndex];
addOrderedItem(selectedItem, itemPrice);
}
else if(event.getSource() == dessertList) { // select item from dessert list
selectedIndex = dessertList.getSelectedIndex();
selectedItem = dessertList.getSelectedItem();
itemPrice = DESSERTS_PRICE[selectedIndex];
addOrderedItem(selectedItem, itemPrice);
}
else if(event.getSource() == fruitFoodList) { // select item from dessert list
selectedIndex = fruitFoodList.getSelectedIndex();
selectedItem = fruitFoodList.getSelectedItem();
itemPrice = FRUIT_FOODS_PRICE[selectedIndex];
addOrderedItem(selectedItem, itemPrice);
}
else if(event.getSource() == beverageList) { // select item from dessert list
selectedIndex = beverageList.getSelectedIndex();
selectedItem = beverageList.getSelectedItem();
itemPrice = BEVERAGE_DRINKS_PRICE[selectedIndex];
addOrderedItem(selectedItem, itemPrice);
}
}
}
/**
* Listener class for button selection
*/
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String selectedItem;
int selectedIndex;
int itemPrice;
if(event.getSource() == addJunkFood) { // get item from junk food list
selectedIndex = junkFoodList.getSelectedIndex();
if(selectedIndex >= 0) {
selectedItem = junkFoodList.getSelectedItem();
itemPrice = JUNK_FOODS_PRICE[selectedIndex];
addOrderedItem(selectedItem, itemPrice);
}
}
else if(event.getSource() == addDessert) { // get item from dessert list
selectedIndex = dessertList.getSelectedIndex();
if(selectedIndex >= 0) {
selectedItem = dessertList.getSelectedItem();
itemPrice = DESSERTS_PRICE[selectedIndex];
addOrderedItem(selectedItem, itemPrice);
}
}
else if(event.getSource() == addFruitFood) { // get item from fruit food list
selectedIndex = fruitFoodList.getSelectedIndex();
if(selectedIndex >= 0) {
selectedItem = fruitFoodList.getSelectedItem();
itemPrice = FRUIT_FOODS_PRICE[selectedIndex];
addOrderedItem(selectedItem, itemPrice);
}
}
else if(event.getSource() == addBeverage) { // get item from fruit food list
selectedIndex = beverageList.getSelectedIndex();
if(selectedIndex >= 0) {
selectedItem = beverageList.getSelectedItem();
itemPrice = BEVERAGE_DRINKS_PRICE[selectedIndex];
addOrderedItem(selectedItem, itemPrice);
}
}
}
}
/**
* method to add/delete an ordered item
*/
void addOrderedItem(String item, int price) {
itemsOrderedList.add(item);
amount += price;
amountLabel.setText("$"+(float)amount/100);
}
/**
* the main method
*/
public static void main(String[] argv) {
// Create a frame
FastFood frame = new FastFood();
frame.setSize(WIDTH, HEIGHT);
frame.setLocation(150, 100);
// add window closing listener
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
// Show the frame
frame.setVisible(true);
}
}
答案 0 :(得分:0)
我建议你重新编写代码。有一个错误需要注意,您正在尝试创建List类型的对象。 List不是一个类,不能实例化,它是一个包含要实现的方法的接口,但这些方法不包含任何实体。要使用java.util.List,必须实现它,然后在为它们提供实体的接口中实现这些方法。如果需要某种列表,则需要根据需要使用java.util.ArrayList或java.util.LinkedList。
除此之外,您在此处发布的代码太长,您最好找到错误所在的代码块,并告诉我们您获得了哪些异常或错误消息。