【发布时间】:2019-04-01 14:18:49
【问题描述】:
我已经设置了一些 ActionListener,但只有“起飞”有效。其他按钮在单击时不起作用。当它们被点击时,什么也没有发生。
我尝试创建一个新的 ButtonHandler,但没有成功。
ButtonListener l = new ButtonListener();
JButton takeoff = new JButton("Takeoff");
takeoff.addActionListener(new ButtonHandler());
takeoff.addActionListener();
grid[0][2].add(takeoff);
JButton land = new JButton("Land");
land.addActionListener(new ButtonHandler());
grid[1][2].add(land);
JButton forward = new JButton("Forward");
forward.addMouseListener(new MouseHandler(l));
forward.addActionListener();
grid[2][1].add(forward);
JButton left = new JButton("Left");
left.addMouseListener(new MouseHandler());
left.addActionListener(new ButtonHandler());
left.addActionListener();
grid[3][0].add(left);
takeoff.addActionListener(l);
land.addActionListener(l);
forward.addActionListener(l);
backward.addActionListener();
left.addActionListener(l);
right.addActionListener(l);
turnLeft.addActionListener(l);
turnRight.addActionListener(l);
up.addActionListener(l);
down.addActionListener(l);
stop.addActionListener(l);
我想做的是将机器人无人机朝正确的方向移动,而不是让它静止不动。
我不确定这部分是否有帮助,但我的 ButtonHandler 实现了 ActionListener。
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String button = e.getActionCommand();
if (button.equals("Takeoff")) {
RobotModel.takeoff();
}
else if (button.equals("Land")) {
RobotModel.land();
}
else if(button.equals("Left")) {
RobotModel.left();
}
}
}
【问题讨论】:
-
可以加
ButtonListener的代码吗? -
@Arnaud,我添加了它
标签: java swing jbutton actionevent