我在我的GUI中有兩個JFrame
。每次用戶點擊按鈕時,如何添加到數量變量?
一個是order
幀,另一個是payment
幀。
我想添加到quantity
變量每次當用戶點擊button
。我在按鈕方法下添加了增量邏輯,但似乎只適用於該方法。它不會將+1
添加到我的數組變量中。
這裏是我的這段代碼:
class Bun extends javax.swing.JFrame {
String decimal = "0.00";
DecimalFormat df = new DecimalFormat(decimal);
public final String [] bname = new String [] {"Tuna Sandwich","Garlic Bread","Redbean Bun"};
public final double [] bprice = new double [] {1.20, 1.50, 1.50};
public double [] bsub = new double [9];
public int [] bquantity = new int[]{0,0,0};
public double bsubtotal;
private void BtnTunaSandwichActionPerformed(java.awt.event.ActionEvent evt) {
bquantity[0]++;
numTunaSandwich.setText(Integer.toString(bquantity[0]));
}
private void BtnGarlicBreadActionPerformed(java.awt.event.ActionEvent evt) {
bquantity[1]++;
numGarlicBread.setText(Integer.toString(bquantity[1]));
}
private void BtnRedbeanBunActionPerformed(java.awt.event.ActionEvent evt) {
bquantity[2]++;
numRedbeanBun.setText(Integer.toString(bquantity[2]));
}
public void calculatesub()
{
for(int counter=0;counter<bsub.length;counter++)
{
bsub[counter]=bquantity[counter]*bprice[counter];
}
}
public void setsubtotal()
{
for(int counter=0;counter<bsub.length;counter++)
{
bsubtotal += bsub[counter];
}
}
public String getsubtotal()
{
return (df.format(bsubtotal));
}
這是我的支付框架代碼:
private void subtotalActionPerformed(java.awt.event.ActionEvent evt) {
Bun bun = new Bun();
bun.calculatesub();
bun.setsubtotal();
subtotal.setText(bun.getsubtotal());
}
每當我運行該程序,並嘗試計算分類彙總,它表明0
。
我的界面:
您在哪裏調用'calculateub'? – Berger
我想你可能需要將'bquantity [0] ++'改成'bquantity [0] = bquantity [0] + 1' - 我認爲'++'意思是將它加1並返回舊值,但是'bquantity [0] = bquantity [0] + 1'表示增加1並返回新值。 – Thraydor
您需要將bquantity對象保存在一個類中。也許這兩個JFrames都有自己的對象?可能有兩個'= new int [3];' –