Java 吉多希洛普的麻烦
hellow我对我在程序中使用的循环感到困惑,因为如果你想买或不买,我使用do while循环对循环。但问题是,当我点击yes时,它会堆积起来。这是到目前为止我的代码。请帮帮我,我很困惑Java 吉多希洛普的麻烦,java,swing,loops,user-interface,Java,Swing,Loops,User Interface,hellow我对我在程序中使用的循环感到困惑,因为如果你想买或不买,我使用do while循环对循环。但问题是,当我点击yes时,它会堆积起来。这是到目前为止我的代码。请帮帮我,我很困惑 import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListen
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.
}
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
导入javax.swing.event.ListSelectionEvent;
导入javax.swing.event.ListSelectionListener;
导入java.awt.event.TextEvent;
导入java.awt.event.TextListener;
公共类brie扩展JFrame实现ActionListener
{
公共静态void main(字符串参数[])
{
brie t1=新brie();
t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t1.设置大小(200300);
t1.setVisible(真);
t1.setLocationRelativeTo(空);
t1.可设置大小(假);
}
JPanel panel3=新的JPanel();
JLabel cos1=新JLabel(“您想买更多吗?”);
JButton yy=新JButton(“是”);
JButton nn=新JButton(“否”);
JFrame want=新JFrame(“再次购买”);
字符串名称p[]={
“----巧克力-----”,
“------蛋糕------------”,
“----糖果----”,
“------冰淇淋------------”,
“----小吃----------”;
int pricep[]={1,2,3,4,5};
JPanel panel2=新的JPanel();
JLabel=新JLabel(“输入您的付款”);
JTextField=新的JTextField(15);
JButton tru=新JButton(“是”);
JFrame w=新JFrame(“付款”);
JList lst=新JList(namep);
JLabel l2=新JLabel(“输入数量”);
JTextField t1=新的JTextField(15);
JLabel l3=新的JLabel();
JButton b2=新JButton(“退出”);
JButton b1=新JButton(“计算”);
JFrame ftinda=新JFrame(“项目和价格”);
双tet;
容器cong=getContentPane();
字符串名称p1,totalist;
双otpc;
int paym,tot=0;
int quan,价格P1;
整数x,总计,变化;
公共布里
{
totalist=“”;
名称1=“”;
//t1.可编辑设置(假);
t1.setText(“”);
cong.setLayout(新的FlowLayout());
cong.add(新的JScrollPane(lst));
cong.add(l2);
cong.add(t1);
丛加(b1);
//cong.add(b2);
丛加(l3);
b1.添加ActionListener(本);
//b2.addActionListener(本);
tru.addActionListener(this);
yy.addActionListener(本);
nn.addActionListener(本);
第一组选定的索引(0);
lst.setSelectionMode(ListSelectionModel.SINGLE_选择);
lst.addListSelectionListener(新的ListSelectionListener()
{
public void值已更改(ListSelectionEvent e)
{
t1.可编辑设置(真);
x=lst.getSelectedIndex();
}
});
}
已执行的公共无效操作(操作事件e)
{
对象sc=e.getSource();
做{
如果(sc==b1)
{
字符串t=t1.getText();
如果(t!=(“”)
{
quan=Integer.parseInt(t1.getText());
价格P1=价格P[x];
namep1=namepp[x];
tot=价格P1*quan;
总计+=总计;
tet=tot;
totallist+=namep1+“”+quan+”pcs=“+tet+”比索”;
t1.setText(“”);
t1.可编辑设置(真);
JOptionPane.showMessageDialog(null,
“+namep1+”=“+pricep1+”php\n“+quan+”pcs=“+tot+”php”);
setVisible(假);
panel3.setLayout(空);
cos1.立根(70,30150,20);
yy.立根数(80,65150,20);
nn.立根数(140,65150,20);
yy.设定尺寸(55,30);
nn.设置大小(55,30);
第3组。添加(cos1);
第3组。添加(yy);
第3组:添加(nn);
添加(第3组);
want.setVisible(true);
想要。设置大小(300200);
want.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
want.setLayout(新的GridLayout());
返回;
}
}
如果(sc==nn)
//如果用户不想购买,将显示总价格。
{
setVisible(假);
want.setVisible(false);
JOptionPane.showMessageDialog(null,
“您选择的产品是”+totallist+
“总计”+总计);
w、 setLayout(新的GridLayout());
w、 设置大小(300200);
w、 setVisible(真);
w、 setDefaultCloseOperation(关闭时退出);
panel2.setLayout(空);
l、 挫折(90,20150,20);
f、 立根(60,60150,20);
真立根数(100100,60,50);
w、 增加(第2组);
第2组:添加(l);
第2组:添加(f);
第2组。添加(tru);
}
如果(sc==tru)
{
w、 setVisible(假);
paym=Integer.parseInt(f.getText());
变化=总工资;
如果(总计=paym)
{
系统输出打印(“不够”);
}
}
}而(sc==yy);
//如果客户想再次购买,则循环。
}
}
不清楚您在问什么(什么会“叠加”?)。您是否可以尝试创建一段较小的代码,它仍然显示问题?确保您没有多次调用addActionListener。我的代码是可运行的,先生。看看“你想买更多”部分。如果我单击yes,它需要循环,因为我在m中声明