-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 很愚蠢.. 并且“食物订单”和“饮料订单”不是显示在窗口而是显示在网络豆的输出..对不起我的英语不好。

4

1 回答 1

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它索取...

仔细查看如何使用按钮如何编写项目侦听器以获取更多详细信息...

注意:我没有包括创建复选框,但基本过程与创建单选按钮相同,只是没有按钮组;)

还应注意,aJComboBoxJList/或JTable可用于提供相同的功能......

于 2013-08-29T03:19:55.253 回答