简体   繁体   English

如何正确格式化 ActionEvent 以便 JButton 可以工作

[英]How to properly format an ActionEvent so JButtons will work

I have set up some ActionListeners, however only 'Takeoff' works.我已经设置了一些 ActionListeners,但是只有“起飞”有效。 The other buttons do not work when they are clicked.其他按钮在单击时不起作用。 When they are clicked, nothing happens.当它们被点击时,什么也没有发生。

I have tried to create a new ButtonHandler, but that did not work.我试图创建一个新的 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);

What I want to do is move the robot drone in the correct direction, rather than just letting it sit still.我想做的是将机器人无人机朝正确的方向移动,而不是让它静止不动。

I am not sure if this part will help, but I have where my ButtonHandler implements ActionListener.我不确定这部分是否会有所帮助,但我有我的 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();
          }

        }
    }

You could use the actionCommand to invoke a method via reflection, eg您可以使用 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);
    }
}

You could also pass the action command as String to the MethodCallActionHandler.您还可以将操作命令作为字符串传递给 MethodCallActionHandler。

You can inherit the Action Listener class into your current class and then add the required methods.您可以将 Action Listener 类继承到当前类中,然后添加所需的方法。 Then you can do takeoff.add(this)... etc.然后你可以做 takeoff.add(this)... 等等。

Also nowhere are you setting the action command, that is done in the button settings.你也无处设置动作命令,这是在按钮设置中完成的。

When you are setting当你设置

String button = e.getActionCommand();

That is not what is being set when you do这不是你做的时候设置的

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


button.setActionCommand("Takeoff");

and then it should work.然后它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM