繁体   English   中英

什么是对按钮单击执行操作的最佳/最简单的解决方案?

[英]What is the best/easiest solution to have an action executed for a button click?

我正在使用Processing创建带有按钮等的自己的API。 但是我做了按钮,当鼠标悬停在按钮上时,我做了所有有关测试的事情,但是...问题是,我真的不知道如何为按钮的操作启动方法。

我看到了基本的Java API的运行方式(通过ActionListener等),但我只是不喜欢它。 我不需要自己的代码-或至少是à我理解的代码。

所以我想要一个Button,当它被单击时启动一个方法,我有一个布尔值(当鼠标悬停它时为true,这很好用)。

我发现了有关反射API以及使用method.invoke();的一些信息,但有些人说这会使程序变慢或对私有方法很危险。

我看到有人在谈论method(); 抽搐显然可以用来调用方法吗? 我进行了搜索,但没有发现更多内容。

因此,与其说是代码问题,不如说是一个思考问题,我不是在问代码,而是,您将如何创建一个可以被任何人使用的按钮,并且该按钮具有某种由方法表示的特定动作。 更确切地说,如何启动该方法。

我不确定我100%理解这个问题,但实际上,使用ActionListener可以进行自定义操作并调用所需的任何方法,也可以使用MouseListener

例如:

public class MyFrame extends JFrame {
    private CustomButton customButton;

    public static void main(String[] args) {
        MyFrame frame = new MyFrame();
        frame.setTitle("Testing MyFrame");
        frame.createGUI();
        frame.pack();
        frame.setVisible(true);
    }

    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        customButton = new CustomButton("Perform Custom Action");
        add(customButton);
    }
}

public class CustomButton extends JButton {

    public CustomButton(String text) {
        super(text);
        this.addActionListener(new CustomAction());
        this.addMouseListener(new CustomMouseAction());
    }

    private void doSomething() {
        doSomething("Custom Button Clicked!");
    }

    private void doSomething(String message) {
        System.out.println(message);
    }

    private class CustomAction implements ActionListener {
         @Override
        public void actionPerformed(ActionEvent ae) {
            doSomething();
        }
    }

    private class CustomMouseAction implements MouseListener {
        @Override
        public void mouseClicked(MouseEvent me) {
            doSomething("Mouse clicked on Custom Button");
        }

        @Override
        public void mousePressed(MouseEvent me) {
            doSomething("Mouse pressed on Custom Button");
        }

        @Override
        public void mouseReleased(MouseEvent me) {
            doSomething("Mouse released on Custom Button");
        }

        @Override
        public void mouseEntered(MouseEvent me) {
            doSomething("Mouse entered on Custom Button");
        }

        @Override
        public void mouseExited(MouseEvent me) {
            doSomething("Mouse exited the Custom Button");
        }
    }
}

PS您还可以在CustomButton类之外创建一个自定义ActionListener(或多个ActionListener)和/或自定义MouseListener(或多个),和/或也可以将它们“分配”(添加)到CustomButton类之外的自定义按钮。

PPS但是,如果您想知道如何制作自定义事件侦听器,则这是另一个问题: http : //wiki.bukkit.org/Event_API_Reference

http://www.javaworld.com/article/2077333/core-java/mr-happy-object-teaches-custom-events.html

其他答案和评论都在谈论纯Java,但是您正在使用Processing。 与在Java中使用的方法相比,您在处理中采用的方法将少一些。

但是,简而言之: 您想得太多了。

您应该首先查看此处提供的现有Processing GUI库。 真正的答案可能是使用其中之一,因为所有这些工作已经为您完成。

但是,如果您确实要创建自己的gui api,则可以归结为以下几件事:

  • 每个组件都应能够绘制自己。
  • 您需要加入诸如mouseClicked()mouseMoved()类的顶级事件,遍历组件,并调用事件函数。
  • 如果要传递单击按钮时调用的方法,则可以简单地提供Button类的用户可以实现的接口。

绝对不需要使用反射来完成此操作。 这是一个简单的示例:

interface ClickAction {
  public void clicked();
}

class Button {

  float x;
  float y;
  float width;
  float height;
  ClickAction action;

  public Button(float x, float y, float width, float height, ClickAction action) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.action = action;
  }

  void draw() {
    rect(x, y, width, height);
  }

  void click() {
    action.clicked();
  }
}

ArrayList<Button> buttons = new ArrayList<Button>();

void setup() {

  ClickAction action1 = new ClickAction() {
    public void clicked() {
      println("Clicked button one.");
    }
  };

  ClickAction action2 = new ClickAction() {
    public void clicked() {
      println("Clicked button two.");
    }
  };

  buttons.add(new Button(25, 25, 50, 20, action1));
  buttons.add(new Button(50, 60, 20, 40, action2));
}

void mouseClicked() {
  for (Button b : buttons) {
    if (mouseX > b.x && mouseX < b.x+b.width) {
      if (mouseY > b.y && mouseY < b.y+b.height) {
        b.click();
      }
    }
  }
}

void draw() {
  background(0);

  for (Button b : buttons) {
    b.draw();
  }
}

这是非常基本的面向对象的代码,不需要通过使用反射来使事情复杂化。 只需使用将要使用的对象即可。

请注意,这种方法几乎完全是Swing使用的方法: JButton类可以具有单击按钮时将调用的ActionListeners

暂无
暂无

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

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