有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

程序的swing Java按钮

我需要java程序代码方面的帮助。基本上,它是一个程序,允许用户请求一个项目和他们想要的金额。如果有库存,他们可以点击“购买”按钮。单击后,将弹出确认对话框,询问用户是否愿意以“y”金额购买“x”单位。他们可以选择是、否或取消。如果他们选择“是”,将出现一个带有Receipt的弹出框(尚未完成此部分)。我遇到的问题是,如果他们单击“否”或“取消”,Receipt帧仍会显示-我不希望它这样做。请告诉我我的代码有什么问题,因为它没有按我希望的方式执行。p、 我是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);
        }


    }
}

}


共 (1) 个答案

  1. # 1 楼答案

    if (Yes == JOptionPane.YES_OPTION) {总是trueYes等于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) {
        ...
    }