不能计算和输出netbeans而不是窗口面板显示

时间:2013-08-29 03:04:38

标签: java swing

/*
 * 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 ..和“食品订单”和“饮料订单”没有显示在窗口,但在网络的输出..抱歉我的英语坏了。

1 个答案:

答案 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可用于提供相同的功能...