对于我在Java类中的第二次编程任务,我们必须创建一个Pizza Shop菜单GUI。所有内容都出现在我的GUI上(包括选项,框,单选按钮等),除了您应该单击以计算总成本的按钮(“过程选择”)。以下是我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PizzaShop extends JFrame {
private Topping t;
private PizzaSize ps;
private PizzaType pt;
private JPanel buttonPanel;
private JButton ProcessSelection;
public PizzaShop() {
super("Welcome To Home Pizza Shop");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
t = new Topping();
ps = new PizzaSize();
pt = new PizzaType();
createPanel();
add(t, BorderLayout.NORTH);
add(ps, BorderLayout.WEST);
add(pt, BorderLayout.CENTER);
setVisible(true);
}
private void createPanel() {
buttonPanel = new JPanel();
ProcessSelection = new JButton("Process Selection");
ProcessSelection.addActionListener(new calButton());
buttonPanel.add(ProcessSelection);
}
private class calButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
double subtotal;
subtotal = t.getTopping() + ps.getPizzaSize();
JOptionPane.showMessageDialog(null, "Your Order \n" + "Pizza Type" + pt.getPizzaType() + "\n" + "Amount Due" + subtotal);
}
}
private class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
}
我忘了在某处添加一些代码吗?我很难过。
答案 0 :(得分:3)
您没有将buttonPanel
添加到主视图
更新:您应该这样做:
private void createPanel() {
buttonPanel = new JPanel();
ProcessSelection = new JButton("Process Selection");
ProcessSelection.addActionListener(new calButton());
buttonPanel.add(ProcessSelection);
add(buttonPanel, BorderLayout.SOUTH);
}
答案 1 :(得分:2)
您需要将按钮面板添加到框架中:
add(buttonPanel, BorderLayout.SOUTH);
答案 2 :(得分:0)
将您的createPanel()更改为:
private JPanel createPanel() {
buttonPanel = new JPanel();
ProcessSelection = new JButton("Process Selection");
ProcessSelection.addActionListener(new calButton());
buttonPanel.add(ProcessSelection);
return buttonPanel;
}
并将以下代码添加到您的PizzaShop()方法中:
add(createPanel(), BorderLayout.SOUTH);
或者你可以按照Adel Boutros所说的那样。