Java 在单击按钮之前计算总值

Java 在单击按钮之前计算总值,java,swing,Java,Swing,我已经创建了一个比萨饼订购计划,该计划可以选择比萨饼底料和比萨饼配料。但该计划规定一次只订购一个比萨饼,并且仅为该比萨饼生成金额。我如何使用客户可以订购比萨饼直到(查看金额)的逻辑来制作该计划点击按钮,所有购买的比萨饼的总金额显示在文本字段中。 然后,对另一个客户重复该循环。 有没有更好的方法告诉我 import java.applet.*; import java.awt.event.*; import java.awt.*; import java.sql.*; import javax.s

我已经创建了一个比萨饼订购计划,该计划可以选择比萨饼底料和比萨饼配料。但该计划规定一次只订购一个比萨饼,并且仅为该比萨饼生成金额。我如何使用客户可以订购比萨饼直到(查看金额)的逻辑来制作该计划点击按钮,所有购买的比萨饼的总金额显示在文本字段中。 然后,对另一个客户重复该循环。 有没有更好的方法告诉我

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

您是否尝试过使用while循环?问题是每个循环的金额都是根据选择生成的。客户可以订购许多比萨饼,直到他查看全部账单。我认为while循环是不够的。您能否根据您的观点建议在何处使用它。例如:
while(!payBillPressed){…}
,其中,
payBillPressed
p1
(或包含逻辑的任何类)的
volatile boolean
实例变量。按下该按钮将触发一个事件,该事件将启动另一个线程。触发此事件会将
payBillPressed
设置为true。