计算总值,直到单击按钮

时间:2014-12-02 16:41:13

标签: java swing

我已经创建了一个披萨订购程序,它可以选择比萨饼和披萨配料。但是这个程序一次只能订购一个披萨,而且只为该特定披萨生成了数量。我可以制作这个程序吗?根据客户可以订购比萨饼的逻辑,直到点击(查看金额)按钮并且所有比萨饼的总金额显示在文本字段中。 然后循环重复另一个客户。 有没有更好的方法告诉我。

import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.io.*;


class p1 extends JFrame implements ActionListener
{

JLabel l,l1;
JTextField t1;
JButton b1,b2,b3;
JRadioButton R1,R2;
Font f1,f2,f3,f4,f5,f6;
JCheckBox c1,c2,c3,c4;
int Tomato=10;
int Capsicum=15;
int Onion=20;
int Paneer=30;
int Soft=30;
int Hard=40;



    p1(String q)
    {   
    super(q);
    Container cc=getContentPane();
    cc.setLayout(null);
    f1=new Font(Font.SANS_SERIF,Font.PLAIN,16);
    l=new JLabel("SELECT BASE");
    cc.add(l);
    l.setFont(f1);
    l.setBounds(130,50,150,50);

    R1=new JRadioButton("SOFT--Rs.30");
    cc.add(R1);
    R1.setBounds(140,100,200,50);
    R1.addActionListener(this);
    R1.setSelected(false);

         R2=new JRadioButton("HARD--Rs.40");
    cc.add(R2);
    R2.setBounds(140,150,200,50);
    R2.addActionListener(this);

    ButtonGroup group = new ButtonGroup();
    group.add(R1);
    group.add(R2);

    f2=new Font(Font.SANS_SERIF,Font.PLAIN,16);
    l1=new JLabel("SELECT TOPPINGS");
    cc.add(l1);
    l1.setFont(f2);
    l1.setBounds(430,50,150,50);

    c1=new JCheckBox("Tomato-Rs.10");
    cc.add(c1);
    f3=new Font("Arial",1,15);
    c1.setFont(f3);
    c1.setBounds(440,100,300,50);
    c1.addActionListener(this);

    c2=new JCheckBox("Capsicum-Rs.15");
    cc.add(c2);
    f4=new Font("Arial",1,15);
    c2.setFont(f4);
    c2.setBounds(440,145,300,50);
    c2.addActionListener(this);

    c3=new JCheckBox("Onion-Rs.20");
    cc.add(c3);
    f5=new Font("Arial",1,15);
    c3.setFont(f5);
    c3.setBounds(440,190,300,50);
    c3.addActionListener(this);

    c4=new JCheckBox("Paneer-Rs.30");
    cc.add(c4);
    f6=new Font("Arial",1,15);
    c4.setFont(f6);
    c4.setBounds(440,235,300,50);
    c4.addActionListener(this);

    b1=new JButton("EXIT");
    cc.add(b1);
    b1.setBounds(550,350,70,30);
    b1.addActionListener(this);

    b2=new JButton("VIEW AMOUNT");
    cc.add(b2);
    b2.setBounds(50,350,150,30);
    b2.addActionListener(this);

    t1=new JTextField();
    cc.add(t1);
    t1.setBounds(50,300,70,30);


    b3=new JButton("PAY BILL");
    cc.add(b3);
    b3.setBounds(300,350,150,30);
    b3.addActionListener(this);


    }

public void actionPerformed(ActionEvent t)
{   
    int total=0;
    int toppingCost=0;
    int baseCost=0;

    if(t.getSource()==b3)
    {

    }


    if(R1.isSelected())

        baseCost+=Soft;

    if(R2.isSelected())

        baseCost+=Hard;

     if (c1.isSelected())

        toppingCost +=Tomato ;

    if (c2.isSelected())
         toppingCost += Capsicum;

    if (c3.isSelected())
        toppingCost += Onion;


    if (c4.isSelected())

        toppingCost += Paneer;

    total=baseCost+toppingCost;

    if(t.getSource()==b1)
        {

        this.setVisible(false);
        }

    if(t.getSource()==b2)
        {

        t1.setText("Rs:"+toString().valueOf(total));
        t1.setEditable(false);

        }

    }
   }

class pizzabase
{
public static void main(String args[])

{
    p1 obj=new p1("PIZZA HOUSE");
    obj.setSize(700,450);
    obj.setVisible(true);
    obj.setLocation(200,200);
    obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

0 个答案:

没有答案