2013-08-29 34 views
-1
/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package pbl2; 


import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.JFrame; 


public class Main 
{ 


    public static void main(String[] args) { 
     JFrame f = new JFrame("WELCOME TO ULEQ MAYANG CAFE"); 
     f.setSize(1200, 500); 
     f.setLocation(0, 0); 
     f.addWindowListener(new WindowAdapter(){ 

     @Override 
     public void windowClosing(WindowEvent we){System.exit(0);} 

     }); 

     JPanel entreePanel = new JPanel(); 
     final ButtonGroup entreeGroup = new ButtonGroup(); 
     JRadioButton radioButton; 
     System.out.print("Please Select Your Food : \n\n"); 

     entreePanel.add(radioButton = new JRadioButton("Uleq Fried Chicken(2 Pieces) = RM6.00")); 
     radioButton.setActionCommand("Uleq Fried Chicken(2 Pieces) = RM6.00"); 
     entreeGroup.add(radioButton); 
     entreePanel.add(radioButton = new JRadioButton("Uleq Fried Chicken(5Pieces) = RM15.00")); 
     radioButton.setActionCommand("Uleq Fried Chicken(5Pieces) = RM15.00"); 
     entreeGroup.add(radioButton); 
     entreePanel.add(radioButton = new JRadioButton("Panera Bread = RM3.00")); 
     radioButton.setActionCommand("Panera Bread = RM3.00"); 
     entreeGroup.add(radioButton); 
     entreePanel.add(radioButton = new JRadioButton("Hoka Hoka Bento = RM4.50")); 
     radioButton.setActionCommand("Hoka Hoka Bento = RM4.50"); 
     entreePanel.add(radioButton = new JRadioButton("Special Uleq Burger = RM6.00")); 
     radioButton.setActionCommand("Special Uleq Burger = RM6.00"); 
     entreeGroup.add(radioButton); 

     final JPanel condimentsPanel = new JPanel(); 
     condimentsPanel.add(new JCheckBox("Orange Pulpy = RM3.80")); 
     condimentsPanel.add(new JCheckBox("Coca Cola = RM2.50")); 
     condimentsPanel.add(new JCheckBox("Pepsi = RM2.50")); 
     condimentsPanel.add(new JCheckBox("Mineral Water = RM1.00")); 
     condimentsPanel.add(new JCheckBox("Special Uleq Latte = RM3.50")); 
     condimentsPanel.add(new JCheckBox("Ribena = RM2.00")); 
     condimentsPanel.add(new JCheckBox("Mango Juice = RM3.00")); 

     JPanel orderPanel = new JPanel(); 
     JButton orderButton = new JButton("THANK YOU FOR PURCHASING AT ULEQ MAYANG CAFE,PLEASE CLICK AND WE WILL PROCEED YOUR ORDER"); 
      orderPanel.add(orderButton); 

    Container content = f.getContentPane(); 
    content.setLayout(new GridLayout(3, 1)); 
    content.add(entreePanel); 

    content.add(condimentsPanel); 
    content.add(orderPanel); 


    orderButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent ae) { 
    String entree = 
    entreeGroup.getSelection().getActionCommand(); 
    System.out.println(entree + " "); 
    Component[] components = condimentsPanel.getComponents(); 
    for (int i = 0; i < components.length; i++) { 
    JCheckBox cb = (JCheckBox)components[i]; 
    if (cb.isSelected()) 
    System.out.println("Drinks order:" + cb.getText()); 
} 
} 
}); 

f.setVisible(true); 


} 

} 

// **幫我!!!! * // 我想計算價格,但我不知道..即時通訊愚蠢的關於Java ..和「食品訂單」和「飲料訂單」不顯示在窗口,而是在淨豆的輸出處。對不起,我的英文破碎。無法計算和netbeans的,而不是窗戶面板中顯示輸出

+0

你在哪裏卡住了?什麼讓你困惑? –

回答

2

你需要打破你的問題。

您有許多項目可以添加到訂單中。有時候,您需要計算該訂單的總額。

一個項目有說明和價格。

訂單可能包含0個或更多項目。

基本上,你需要一些方法來模擬這些元素。當單擊表示該項目的UI元素時,您需要將其添加或從訂單中刪除。

當用戶點擊按鈕時,您需要詢問訂單來計算總數。

這是模型,視圖,控制範式的基本概念。

您不需要將一堆控件轉儲到窗口上,而需要以某種方式對這些不同的元素進行建模並生成一個表示它的UI。

讓我們先從模型...

// The order, which holds a series of items... 
// You should be able to see getTally method :D 
public class Order { 

    private List<Item> items; 

    public Order() { 
     items = new ArrayList<>(25); 
    } 

    public void add(Item item) { 
     items.add(item); 
    } 

    public void remove(Item item) { 
     items.remove(item); 
    } 

    public double getTally() { 

     double tally = 0; 
     for (Item item : items) { 
      tally += item.getPrice(); 
     } 

     return tally; 

    } 

} 

// A basic item, which has a description and a price... 
public class Item { 

    private String text; 
    private double price; 

    public Item(String text, double price) { 
     this.text = text; 
     this.price = price; 
    } 

    public String getText() { 
     return text; 
    } 

    public double getPrice() { 
     return price; 
    } 

} 

接下來,我們需要一些方法來這種模式屏幕...現在,因爲有這麼多的重複的代碼,我會創造出一系列方法來使您的生活更輕鬆......

// Formats the item for the display... 
protected String toString(Item item) { 
    return item.getText() + " (" + NumberFormat.getCurrencyInstance().format(item.getPrice()) + ")"; 
} 

// Creates a radio button for the specified Item... 
protected JRadioButton createRadioButton(ButtonGroup group, Item item) { 
    JRadioButton rb = new JRadioButton(toString(item)); 
    rb.addItemListener(new ItemHandler(order, item)); 
    group.add(rb); 
    return rb; 
} 

現在,你簡單的添加這一樣簡單的UI ......

entreePane.add(createRadioButton(bg, new Item("Uleq Fried Chicken(2 Pieces)", 6.0))); 

現在,我們需要一些方法來知道何時添加或從我們的Order刪除項目。值得慶幸的是,這可以通過使用一個ItemListener,這將讓當一個按鈕選擇或取消選擇我們知道,被照顧......

public class ItemHandler implements ItemListener { 

    private Order order; 
    private Item item; 

    public ItemHandler(Order order, Item item) { 
     this.order = order; 
     this.item = item; 
    } 

    @Override 
    public void itemStateChanged(ItemEvent e) { 
     if (((AbstractButton) e.getSource()).isSelected()) { 
      order.add(item); 
     } else { 
      order.remove(item); 
     } 
    } 

} 

現在,當你需要理貨,你可以問Order爲它...

再仔細看看How to use buttonsHow to write an item listener瞭解更多詳情...

注:我沒有列入的複選框的創作,但基本流程是一樣的創建無線電按鈕,只是沒有按鈕組;)

還應當指出的是,一個JComboBoxJList和/或JTable可用於提供相同的功能...