2011-11-02 82 views
0

`的actionPerformed以及使

public void actionPerformed(ActionEvent e) 
    { 
     if (e.getSource() == saleButton) 
     { 
      processSale(Wine wi); 
      //System.out.println("fvghj"); 
     } 
     else if (e.getSource() == returnButton) 
     { 
       processReturn(Wine wi); 
      //System.out.println("fdgchj"); 
     } 

    } 


    //create a wine object to be returned - responsible for getting details from textfields when either sale or return is pressed 
    //this object is then passed to the relevant method in LWMGUI to process either sale or return 


public Wine getWine() 
    { 
     String wName = nameWineText.getText(); 

     String costBottles = costBottleText.getText(); 
     int cBottle = Integer.parseInt(costBottles); 
     //System.out.println(cBottle); 

     //get numBottle 
     String numBottles = numBottlesText.getText(); 
     int nBottle = Integer.parseInt(numBottles); 
     //System.out.println(nBottle); 

     Wine wi = new Wine(wName, cBottle, nBottle); 

     return wi; 

    } 


    private int processSale(Wine wi) 
    { 
     int totalSaleAmount = wi.getNumBottle() * (int) wi.getCostBottle(); 
     //System.out.println(totalAmount); 
     transactionText.setText("" + totalSaleAmount); 
     return totalSaleAmount; 
    } 

    private int processReturn(Wine wi) 
    { 
     int totalReturnAmount = wi.getNumBottle() * (int) wi.getCostBottle(); 
     //System.out.println(totalReturnAmount); 
     transactionText.setText("" + totalReturnAmount); 
     return totalReturnAmount; 
    } 

`我在與一個actionPerformed方法的問題的對象。我已經從用戶輸入的文本字段獲得文本,以便爲我提供一個葡萄酒的成本,數量和名稱,以便進行銷售或退貨處理。我用這些信息創建了一個Wine對象(wi),並且希望將它傳遞給其他方法,無論是銷售還是返回。

我無法得到processSale()或processReturn()方法的工作,我真的很困惑!我認爲通過葡萄酒wi對象的方法將工作? 這裏是相關的代碼;

+0

processSale(Wine wi);在這裏你正在創建一個變量。但是你應該傳遞一個對象,如processSale(new Wine()) – blessenm

回答

1

只要做你發送一無所有酒的新實例,這

processSale(getWine()) 

或者

processReturn(getWine()) 

其實所有的時間。所以你的這兩種方法總是有一個空實例的酒