gpt4 book ai didi

程序的 Java 按钮

转载 作者:行者123 更新时间:2023-12-01 21:58:28 26 4
gpt4 key购买 nike

我需要有关我的 java 程序代码的帮助。基本上,它是一个允许用户请求商品和他们想要的数量的程序。如果有库存,他们可以点击“购买”按钮。单击后,将弹出一个确认对话框,询问用户是否愿意以“y”金额购买“x”单位。他们可以选择是、否或取消。如果他们选择"is",则会出现一个带有收据的弹出框(尚未完成这部分)。我遇到的问题是,如果他们单击“否”或取消,收据框架仍然显示 - 我不希望它这样做。请您告诉我我的代码有什么问题,因为它没有按照我想要的方式执行。附注我是 Java 初学者,因为我只学习了一个月

enter code here

public PurchaseItem() {

this.setLayout(new BorderLayout());

JPanel top = new JPanel();
top.setLayout(new FlowLayout(FlowLayout.CENTER));
JPanel bottom = new JPanel();
bottom.setLayout(new FlowLayout(FlowLayout.CENTER));
bottom.add(Buy);
this.add(bottom, BorderLayout.SOUTH);

setBounds(100, 100, 450, 250);
setTitle("Purchase Item");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

top.add(new JLabel("Enter Item Key:"));
top.add(ItemNo);
top.add(new JLabel ("Enter Amount:"));
top.add(AmountNo);
top.add(check);

Buy.setText("Buy"); Buy.setVisible(true);

check.addActionListener(this);
Buy.addActionListener(this);

add("North", top);
JPanel middle = new JPanel();
middle.add(information);
add("Center", middle);

setResizable(true);
setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
String ItemKey = ItemNo.getText();
String ItemAmount = AmountNo.getText();
String Name = StockData.getName(ItemKey);

int Yes = JOptionPane.YES_OPTION;
int No = JOptionPane.NO_OPTION;

int Amount = Integer.parseInt(ItemAmount);
int Key = Integer.parseInt(ItemKey);

int NewStock = StockData.getQuantity(ItemKey) - Amount;

double Total = Integer.parseInt(ItemAmount) * StockData.getPrice(ItemKey);

if (Name == null){
information.setText("There is no such item");
}
else if (Amount > StockData.getQuantity(ItemKey)) {
information.setText("Sorry there is not enough stock available");
}
else {
information.setText(Name + " selected: " + ItemAmount);
information.append("\nIndividual Unit Price: " + pounds.format(StockData.getPrice(ItemKey)));
information.append("\nCurrent Stock Available: " + StockData.getQuantity(ItemKey));
information.append("\nNew Stock After Sale: " + NewStock);
information.append("\n\nTotal: " + ItemAmount + " Units" + " at " + pounds.format(StockData.getPrice(ItemKey)) + " each");
information.append("\n= " + pounds.format(Total));
}
if (e.getSource() == Buy) {
JOptionPane.showConfirmDialog(null, "Buy " + ItemAmount + " Units" + " for " + pounds.format(Total) + "?");
if (Yes == JOptionPane.YES_OPTION) {
JFrame frame2 = new JFrame();
frame2.pack(); frame2.setBounds(250, 250, 500, 500); frame2.setTitle("Reciept"); frame2.setVisible(true);
JPanel middle = new JPanel();
middle.setLayout(new FlowLayout(FlowLayout.CENTER));
middle.add(reciept);
reciept.setBounds(260,260,400,400);
reciept.setVisible(true);
}


}
}

}

最佳答案

if (Yes == JOptionPane.YES_OPTION) {总是true ( Yes 等于 JOptionPane.YES_OPTIONint Yes = JOptionPane.YES_OPTION; )。

需要从JOptionPane.showConfirmDialog获取返回值并比较一下

int response = JOptionPane.showConfirmDialog(null, "Buy " + ItemAmount + " Units" + " for " + pounds.format(Total) + "?");
if (response == JOptionPane.YES_OPTION) {
...
}

关于程序的 Java 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34097021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com