【问题标题】:How to properly format an ActionEvent so JButtons will work如何正确格式化 ActionEvent 以便 JButtons 可以工作
【发布时间】: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


【解决方案1】:

您可以使用 actionCommand 通过反射调用方法,例如

private void init() {
    JButton left = new JButton("Go left");
    // This
    left.setActionCommand("left");
    left.addActionListener(new MethodCallActionHandler());
    // OR that
    left.addActionListener(new MethodCallActionHandler("left"));
}

private void left() {
    // go left...
}

private class MethodCallActionHandler implements ActionListener {

    private String methodName;

    private MethodCallActionHandler() {
        super();
    } 

    private MethodCallActionHandler(String methodName) {
        this.methodName = methodName;
    } 

    public void actionPerformed(ActionEvent e)
    {
        String button = methodName != null ? methodName : e.getActionCommand();
        SurroundingClass.this.getClass().getMethod(button, new Class[] {}).invoke(SurroundingClass.this);
    }
}

您还可以将操作命令作为字符串传递给 MethodCallActionHandler。

【讨论】:

    【解决方案2】:

    您可以将 Action Listener 类继承到当前类中,然后添加所需的方法。然后你可以做 takeoff.add(this)... 等等。

    您也没有在任何地方设置动作命令,这是在按钮设置中完成的。

    当你设置时

    String button = e.getActionCommand();
    

    这不是你设置的内容

    JButton button = new JButton("Takeoff"); <-- This is the text associated with the button
    
    
    button.setActionCommand("Takeoff");
    

    然后它应该可以工作了。

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 2022-07-13
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      相关资源
      最近更新 更多