2013-08-29 1 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); 


} 

} 

가 // ** 것이 나에게 도움이 대신 창 패널의 넷빈즈에 표시 !!!! * // 내가 가격을 계산 싶어하지만 난 잘 모릅니다 .. 자바에 대한 메신저 바보 .. 그리고 "음식 주문"과 "음료 주문"은 창에 표시되지 않지만 그물 콩의 출력에 표시됩니다.계산할 수없고 출력이

+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; 
    } 

} 

다음으로, 우리는 너무 많은 반복 코드가 있기 때문에, 지금은 ... 화면이를 모델링 할 수있는 방법이 필요합니다, 나는 일련의를 만들 것 방법은 ...처럼 간단하여 UI에 이것을 추가하면 간단 ... 지금 여러분의 인생을 더 쉽게

// 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; 
} 

를 만들기 위해

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

이제 Order에서 항목을 추가하거나 제거 할시기를 알아야합니다. 다행히, 이것은 당신이 집계를 필요로 할 때

이제
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을 요청할 수 있습니다 ... 버튼을 선택 또는 선택 해제 될 때 알려 드릴 것입니다 ItemListener를 사용하여 처리 될 수 있습니다 내가 확인란의 생성을 포함하지했지만, 기본적인 과정은 라디오를 만드는 것과 동일합니다 : 그것은 ...

은 ... How to use buttons 및 자세한 내용은 How to write an item listener에서

주를 자세히 살펴 보자 버튼 그룹없이 버튼 만)

또한 주목해야하는 JComboBox, JList 및/또는 JTable가 동일한 기능을 제공하는 데 사용될 수는 ...