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));
}
}
问问题
32 次
1 回答
0
RoomListener.actionPerformed(RoomType);
这不是一种
static
方法。您不能使用类名来调用它。是什么
RoomType
?如果它不是ActionEvent
,那么它将不起作用。看方法。public void actionPerformed(ActionEvent event)
你真的不应该调用另一个听众的 actionPerformed,或者至少我想不出理由。您需要
new RoomListener()
在 RadioButtons 上添加一个。不过,我建议您查看 RadioGroup 类,并阅读 JavaDoc 以获得正确的侦听器,从而确定所选单选按钮的来源。此方法不返回任何内容。
double roomtype
是一个局部变量,当你退出这个方法时它会被丢弃(垃圾收集)。也许您打算修改成员变量?this.roomtype
? 除了这一点 - 我不知道什么room1
和room2
在这里......他们似乎无法访问。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 回答