简体   繁体   中英

How to properly format an ActionEvent so JButtons will work

I have set up some ActionListeners, however only 'Takeoff' works. 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.

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.

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

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.

You can inherit the Action Listener class into your current class and then add the required methods. Then you can do takeoff.add(this)... etc.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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