簡體   English   中英

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

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

我已經設置了一些 ActionListeners,但是只有“起飛”有效。 其他按鈕在單擊時不起作用。 當它們被點擊時,什么也沒有發生。

我試圖創建一個新的 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();
          }

        }
    }

您可以使用 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。

您可以將 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");

然后它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM