首页 文章

在按钮中添加总计

提问于
浏览
0

我有一些问题在按钮中添加代码所以示例:我在数量上键入2它将*价格和数量示例400 * 2 = 800但是当我再次键入2表示800 x 2 = 1600时,任何人都可以指导我吗?谢谢错误显示在最后2行 .
`private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
int id = Integer.parseInt(jTextField1.getText()); int qty = Integer.parseInt(jTextField2.getText());

purchasecontroller.PurchaseProduct(id, qty);
       String getname = displaycontroller.SearchbyProductName(id);
        jLabel4.setText( "" + getname );
        jLabel3.setText("" + qty);
          jList1.addElement(getname + qty);
  //     jList1.add(new Product("Hello", 1));
        String getprice = displaycontroller.SearchbyProductPrice(id);
      int total = qty * Integer.parseInt (getprice);
      jLabel11.setText("" + total );
       int finals = (total * qty);
       jLabel12.setText("" + finals );
}

`

2 回答

  • 1

    应该

    int total = qty * Integer.parseInt (getprice);
    

    代替

    int total = qty * getprice;
    
  • 1

    在这一行:

    int total = qty * getprice;
    

    您试图将int(qty)乘以String(getprice),这是无法完成的 .

    您需要将getprice解析为整数,然后在 total 赋值中为getprice替换该新整数 .

相关问题