ActionPerformed并传递一个对象

时间:2011-11-02 09:46:17

标签: java

`

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()方法起作用,我真的很困惑!我以为将Wine wi对象传递给方法会起作用吗? 这是相关的代码;

1 个答案:

答案 0 :(得分:1)

就这样做

processSale(getWine())

或者

processReturn(getWine())

实际上你一直在发送一个没有任何东西的新葡萄酒实例。所以你的这两个方法总是有一个空的葡萄酒实例