2012-06-18 54 views
2

對於我在Java類中的第二個編程任務,我們必須創建一個Pizza Shop菜單GUI。除了用於計算總成本的按鈕(「過程選擇」)之外,一切都出現在我的GUI上(包括選項,框,單選按鈕等)。以下是我的代碼:沒有出現在Swing 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到主視圖

UPDATE:你應該這樣做:

private void createPanel() { 
    buttonPanel = new JPanel(); 
    ProcessSelection = new JButton("Process Selection"); 
    ProcessSelection.addActionListener(new calButton()); 
    buttonPanel.add(ProcessSelection); 
    add(buttonPanel, BorderLayout.SOUTH); 
} 
2

您需要的按鈕面板添加到框架:

add(buttonPanel, BorderLayout.SOUTH); 
+0

確切地說'createPanel()'返回類型是'void'。你可能指的是阿德爾布特羅斯寫的 –

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); 

或者,你可以關注你阿德爾·布特羅斯說。