如果你想购买或不购买,我会使用while循环来循环,这让我对我在程序中使用的循环感到困惑。但问题是当我点击是它会叠加。到目前为止,这是我的代码。请帮助我非常困惑:3。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
public class brie extends JFrame implements ActionListener
{
public static void main(String args [])
{
brie t1 = new brie();
t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t1.setSize(200,300);
t1.setVisible(true);
t1.setLocationRelativeTo(null);
t1.setResizable(false);
}
JPanel panel3 = new JPanel();
JLabel cos1 = new JLabel("Do you want to buy more ?");
JButton yy = new JButton("Yes");
JButton nn = new JButton("No");
JFrame want = new JFrame("Buy AGain");
String namep[] = {
"---------Chocolate---------",
"---------Cake----------",
"---------Candy---------",
"---------Ice Cream---------",
"---------Snacks---------"};
int pricep [] = {1, 2, 3, 4, 5};
JPanel panel2 = new JPanel();
JLabel l = new JLabel("Enter your payment");
JTextField f = new JTextField(15);
JButton tru = new JButton("Yes");
JFrame w = new JFrame("pay");
JList lst = new JList(namep);
JLabel l2 = new JLabel("Enter Quantity");
JTextField t1 = new JTextField(15);
JLabel l3 = new JLabel();
JButton b2 = new JButton("Exit");
JButton b1 = new JButton("Compute");
JFrame ftinda = new JFrame("Item && Prices");
double tet;
Container cong = getContentPane();
String namep1,totallist;
double otpc;
int paym,tot = 0;
int quan,pricep1;
int x,grandtotal,change;
public brie()
{
totallist = "";
namep1 = "";
// t1.setEditable(false);
t1.setText("");
cong.setLayout(new FlowLayout());
cong.add(new JScrollPane(lst));
cong.add(l2);
cong.add(t1);
cong.add(b1);
//cong.add(b2);
cong.add(l3);
b1.addActionListener(this);
//b2.addActionListener(this);
tru.addActionListener(this);
yy.addActionListener(this);
nn.addActionListener(this);
lst.setSelectedIndex(0);
lst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lst.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
t1.setEditable(true);
x = lst.getSelectedIndex();
}
});
}
public void actionPerformed(ActionEvent e)
{
Object sc = e.getSource();
do{
if(sc == b1)
{
String t = t1.getText();
if(t!=(""))
{
quan = Integer.parseInt(t1.getText());
pricep1 = pricep[x];
namep1 = namep[x];
tot = pricep1*quan;
grandtotal += tot;
tet = tot;
totallist += namep1 + " "+quan+ "pcs = "+ tet +" pesos ";
t1.setText("");
t1.setEditable(true);
JOptionPane.showMessageDialog(null,
""+namep1+" = "+pricep1+"php\n"+quan+"pcs = "+tot+"php");
setVisible(false);
panel3.setLayout(null);
cos1.setBounds(70,30,150,20);
yy.setBounds(80,65,150,20);
nn.setBounds(140,65,150,20);
yy.setSize(55,30);
nn.setSize(55,30);
panel3.add(cos1);
panel3.add(yy);
panel3.add(nn);
want.add(panel3);
want.setVisible(true);
want.setSize(300,200);
want.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
want.setLayout(new GridLayout());
return;
}
}
if(sc == nn)
//if the user dont want to buy it will appear the total price.
{
setVisible(false);
want.setVisible(false);
JOptionPane.showMessageDialog(null,
"the product you choose is "+totallist+
"the total of"+ grandtotal);
w.setLayout(new GridLayout());
w.setSize(300,200);
w.setVisible(true);
w.setDefaultCloseOperation(EXIT_ON_CLOSE);
panel2.setLayout(null);
l.setBounds(90,20,150,20);
f.setBounds(60,60,150,20);
tru.setBounds(100,100,60,50);
w.add(panel2);
panel2.add(l);
panel2.add(f);
panel2.add(tru);
}
if(sc == tru)
{
w.setVisible(false);
paym = Integer.parseInt(f.getText());
change =paym-grandtotal;
if(grandtotal <= paym)
{
JOptionPane.showMessageDialog(null,
"the product you buy is"+totallist+
"the total of "+grandtotal+
"your payment is :"+f.getText() +
"your change is :"+change);
}
else if(grandtotal >= paym)
{
System.out.print("Not enough");
}
}
}while(sc == yy);
// to loop if the costumer want to buy again.
}
}