-3
public class RoomListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        double roomtype; 

        if (event.getSource() == room1)
            roomtype = 60;
        else if (event.getSource() == room2)
            roomtype = 75;
        else 
            roomtype = 100;
    }

}


public class CostListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        double NightLength, roomNumber, cost;
        String NightText = NumberOfNights.getText();
        String RoomText = NumberOfRooms.getText();

        NightLength = Double.parseDouble(NightText);
        roomNumber = Double.parseDouble(RoomText);

        RoomListener.actionPerformed(RoomType);
        cost = roomtype * NightLength * roomNumber;

        CostCalculation.setText(Double.toString(cost));
        NumberFormat fmt = NumberFormat.getNumberInstance();
        CostCalculation.setText(fmt.format(cost));
    }
}
4

1 回答 1

0

RoomListener.actionPerformed(RoomType);

  1. 这不是一种static方法。您不能使用类名来调用它。

  2. 是什么RoomType?如果它不是ActionEvent,那么它将不起作用。看方法。

    public void actionPerformed(ActionEvent event)
    
  3. 你真的不应该调用另一个听众的 actionPerformed,或者至少我想不出理由。您需要new RoomListener()在 RadioButtons 上添加一个。不过,我建议您查看 RadioGroup 类,并阅读 JavaDoc 以获得正确的侦听器,从而确定所选单选按钮的来源。

  4. 此方法不返回任何内容。double roomtype是一个局部变量,当你退出这个方法时它会被丢弃(垃圾收集)。也许您打算修改成员变量?this.roomtype? 除了这一点 - 我不知道什么room1room2在这里......他们似乎无法访问。

    public void actionPerformed(ActionEvent event)
    {
        double roomtype; 
    
        if (event.getSource() == room1)
            roomtype = 60;
        else if (event.getSource() == room2)
            roomtype = 75;
        else 
            roomtype = 100;
    }
    
于 2016-12-09T02:39:48.920 回答