best/easiest 为单击按钮执行操作的解决方案是什么?
What is the best/easiest solution to have an action executed for a button click?
我正在处理以创建我自己的 API,带有按钮等。然而我做了按钮,当你的鼠标悬停在按钮上时,我做了所有关于测试的事情,......但问题是,我真的不知道如何启动按钮操作的方法。
我看到基本的 Java API 是如何做的 - 使用 ActionListener 等等 - 但我就是不喜欢它。我不想要我自己的代码 - 或者至少我理解的代码。
所以我想要一个在单击时启动方法的按钮,我有一个布尔值(当鼠标悬停时为真...工作正常)。
我发现了一些关于反射 API 和使用 method.invoke(); 的事情,但有些人说这会减慢程序速度或对私有方法造成危险。
我看到有人在谈论method();显然可以用来调用方法?我搜索了一下,没有找到更多。
所以它更像是一个思想问题而不是代码问题,我不是在要求代码,而是更多,你将如何创建一个任何人都可以使用的 Button 并且它将具有由方法表示的特定操作。更准确地说,如何启动该方法。
我不确定我是否 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");
}
}
}
P.S。您还可以在 CustomButton
class、and/or 之外制作自定义 ActionListener(或多个 ActionListeners)and/or 自定义 MouseListener(或多个)您也可以 'assign' (将它们添加到 CustomButton
class.
之外的自定义按钮
P.P.S。但如果你想知道如何制作自定义事件监听器,那就是另一个问题了:
http://wiki.bukkit.org/Event_API_Reference
http://www.javaworld.com/article/2077333/core-java/mr-happy-object-teaches-custom-events.html
其他答案和评论都在谈论直接 Java,但您使用的是 Processing。您在 Processing 中采用的方法将比您在 Java.
中使用的方法少一些......经过精心设计
但是,简而言之:你想多了。
您应该首先检查可用的现有 Processing GUI 库 here。真正的答案可能是使用其中之一,因为所有这些工作都已经为您完成。
但是,如果您真的想创建自己的图形用户界面 api,则归结为以下几点:
- 每个组件都应该能够自己绘制。
- 您需要连接到顶级事件,例如
mouseClicked()
或 mouseMoved()
,迭代您的组件,并调用事件函数。
- 如果您想传递单击按钮时调用的方法,那么您可以简单地提供一个界面,您的按钮 class 的用户可以实现。
完全没有必要使用反射来完成这个。这是一个简单的例子:
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
classes 可以有 ActionListeners
在单击按钮时调用。
我正在处理以创建我自己的 API,带有按钮等。然而我做了按钮,当你的鼠标悬停在按钮上时,我做了所有关于测试的事情,......但问题是,我真的不知道如何启动按钮操作的方法。
我看到基本的 Java API 是如何做的 - 使用 ActionListener 等等 - 但我就是不喜欢它。我不想要我自己的代码 - 或者至少我理解的代码。
所以我想要一个在单击时启动方法的按钮,我有一个布尔值(当鼠标悬停时为真...工作正常)。
我发现了一些关于反射 API 和使用 method.invoke(); 的事情,但有些人说这会减慢程序速度或对私有方法造成危险。
我看到有人在谈论method();显然可以用来调用方法?我搜索了一下,没有找到更多。
所以它更像是一个思想问题而不是代码问题,我不是在要求代码,而是更多,你将如何创建一个任何人都可以使用的 Button 并且它将具有由方法表示的特定操作。更准确地说,如何启动该方法。
我不确定我是否 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");
}
}
}
P.S。您还可以在 CustomButton
class、and/or 之外制作自定义 ActionListener(或多个 ActionListeners)and/or 自定义 MouseListener(或多个)您也可以 'assign' (将它们添加到 CustomButton
class.
P.P.S。但如果你想知道如何制作自定义事件监听器,那就是另一个问题了: http://wiki.bukkit.org/Event_API_Reference
http://www.javaworld.com/article/2077333/core-java/mr-happy-object-teaches-custom-events.html
其他答案和评论都在谈论直接 Java,但您使用的是 Processing。您在 Processing 中采用的方法将比您在 Java.
中使用的方法少一些......经过精心设计但是,简而言之:你想多了。
您应该首先检查可用的现有 Processing GUI 库 here。真正的答案可能是使用其中之一,因为所有这些工作都已经为您完成。
但是,如果您真的想创建自己的图形用户界面 api,则归结为以下几点:
- 每个组件都应该能够自己绘制。
- 您需要连接到顶级事件,例如
mouseClicked()
或mouseMoved()
,迭代您的组件,并调用事件函数。 - 如果您想传递单击按钮时调用的方法,那么您可以简单地提供一个界面,您的按钮 class 的用户可以实现。
完全没有必要使用反射来完成这个。这是一个简单的例子:
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
classes 可以有 ActionListeners
在单击按钮时调用。