我正在学习Java创建小项目以获得乐趣。我很新,很容易陷入困境,因此我要问的问题。
我正在尝试创建一个简单的购物车程序,在它的主面板中有一个3x3网格,后面包含空标签 将被替换为图像。在网格的右侧有四个按钮,添加餐,添加饮料,总价格 并清除所有。因此,当单击前两个按钮中的任何一个时,我希望它将当前面板更改为与单击的按钮对应的面板。 然后显示的面板具有数据,如组合框中的食物类型,其显示设定的食物和食物的量。 然后,当用户选择类型和数量时,按下“添加”按钮,然后更改面板 返回主面板,第一个空标签现在有一个已添加到购物车中的项目的图像。
这就是我目前所知道的,据我所知。
主要课程
public class ShoppingCart
{
public ShoppingCart()
{
JFrame app = new JFrame("Cart");
MainPanel main = new MainPanel();
app.add(main);
app.pack();
app.setResizable(false);
app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
public static void main(String[] args)
{
new ShoppingCart();
}
}
MainPanel类
public class MainPanel extends JPanel implements ActionListener
{
private final JLabel labels[] = new JLabel[9];
private final JButton button[] = new JButton[4];
public MainPanel()
{
String buttonText[] ={"Add a Meal", "Add a Drink", "Total Price", "Clear All"};
String buttonToolTipText[] =
{"This will add food", "This will add drink", "This will display the price", "Clear the cart"};
this.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 1, 2, 2));
for (int i = 0; i < 4; i++)
{
button[i] = new JButton(buttonText[i]);
button[i].setOpaque(true);
button[i].setBackground(Color.WHITE);
button[i].setFont(new Font("Ariel", Font.PLAIN, 18));
button[i].setPreferredSize(new Dimension(150,50));
button[i].setVisible(true);
button[i].setToolTipText(buttonToolTipText[i]);
button[i].addActionListener(this);
buttonPanel.add(button[i]);
}
JPanel labelPanel = new JPanel(new GridLayout(3, 3, 2, 2));
Border border = BorderFactory.createLineBorder(Color.GRAY);
for (int i = 0; i < 9; i++)
{
labels[i] = new JLabel();
labels[i].setOpaque(true);
labels[i].setVisible(true);
labels[i].setPreferredSize(new Dimension(100, 100));
labels[i].setBorder(border);
labels[i].setBackground(Color.WHITE);
labelPanel.add(labels[i]);
//set empty label to the image of the type chose,
//if right clicked, show type and quantity chosen
}
add(labelPanel, BorderLayout.WEST);
add(buttonPanel, BorderLayout.EAST);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(button[0] == e.getSource())
{
//Change panel to Meal panel
}
if(button[1] == e.getSource())
{
//Change panel to Drink panel
}
if(button[2] == e.getSource())
{
//JOptionPane to show total price
}
if(button[3] == e.getSource())
{
//Check whether labels are empty, if not clear them else display message
}
}
}
MealPanel类
public class MealPanel extends JPanel implements ActionListener
{
public MealPanel()
{
this.setLayout(new BorderLayout());
JPanel titlePanel = new JPanel();
JPanel labelPanel = new JPanel(new GridLayout(2, 1, 3, 3));
JPanel inputField = new JPanel(new GridLayout(2, 1, 3, 3));
JPanel addCancel = new JPanel();
JLabel title = new JLabel("Add a Meal");
title.setBorder(BorderFactory.createMatteBorder(5,0,10,0, new Color(0,0,0,0)));
titlePanel.add(title);
JLabel type = new JLabel("Type:", JLabel.RIGHT);
labelPanel.add(type);
JLabel quantity = new JLabel("Quantity:", JLabel.RIGHT);
labelPanel.add(quantity);
final String arr[] = {"Burger", "Pizza"};
JComboBox mealType = new JComboBox(arr);
inputField.add(mealType);
JTextField quant = new JTextField();
inputField.add(quant);
JButton add = new JButton("Add");
add.addActionListener(this);
addCancel.add(add);
JButton cancel = new JButton("Cancel");
cancel.addActionListener(this);
addCancel.add(cancel);
add(titlePanel, BorderLayout.NORTH);
add(labelPanel, BorderLayout.WEST);
add(inputField, BorderLayout.EAST);
add(addCancel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e)
{
//If cancel is pressed, return back to main, nothing changed on this panel
//else if add is pressed, set the type and quantity and return back to main
}
}
我想显示的是,我不知道如何将当前面板专门更改为通过单击按钮选择的面板。然后,当显示面板时,如何设置数据然后将面板更改回MainPanel,然后将标签中显示的那种食物显示为网格中与所选类型对应的图像,因此要么是汉堡还是第一个空标签中的披萨。
如何进行申请?