javax.swing 如何使用我自己的方法监听事件
How to use my own method for event listening in javax.swing
我实际上已经开始学习 javax.swing 图形。对于像 KeyListener、MouseListener 等事件监听,我必须用它们自己的 headers.
实现它们的所有方法
我以前是做JS的,那里的事件监听很简单
所以我决定创建自己的 class myButton,它扩展了 JButton 并实现了所有需要的侦听器。但是单击按钮时必须执行的操作的语句通常应该放在 mouseClicked 中,但是在外部使用它我不知道如何使用它。另一件事是我想在 myButton class 中创建一个 onClick 方法,该方法具有与 JS 相同的函数名称的参数。但我不知道如何创建该功能。此外 Java 中不存在函数传递。还有一个问题是如何告诉程序onClick方法是点击按钮时应该执行的方法。
所以这是我想澄清并知道如何实现它们的两件事。
- 使用我自己的方法 (onClick) 而不是默认方法 (mouseClicked)。
- 如何像在 Javascript.
中那样将函数名称或匿名函数(应该实际实现)作为参数传递给 onClick 方法
提前致谢
说真的,我认为你需要去阅读 How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener 这最终会做你想做的事
所以像...
JButton btn = new JButton("Click me");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("I was clicked");
}
});
将完全按照您刚才的描述进行操作。事实上,大多数组件已经内置了一些鼠标 and/or 键盘控件,因此您“不必”重新发明整个轮子。
So I decided to create my own class myButton which extends JButton and also implements all the listeners required. But the statements for what has to be done when the button is clicked should be placed in mouseClicked normally, but using that externally I'm not getting how to use that.
是的,这就是自定义 interface
的用途 - 但问题是,当“鼠标”动作发生时按钮生成事件是否与“键盘”并列" 操作还是其他操作?
这就是通常使用 ActionListener
的原因,因为它提供了一个不可知的事件处理程序来处理按钮何时被“激活”,通常,无论它是如何触发的
Another thing is that I want to create a method onClick inside myButton class which has the parameter of the function name same as JS. But I don't know how to create that function. Moreover function passing is not there in Java. And another problem is that how can I tell the program the onClick method is the method that should be implement when the button is clicked.
Java中没有函数,只有对象。这是 interface
有用的地方,它是一个合同,描述了期望实现提供的功能。
您已经使用过它们,您只需要定义您预期的“触发”/method/callback 将用于您的“onClick”操作。
Ok but I want the button to listener for more events like hover, drag etc.
好吧,拖放由 Drag and Drop and Data Transfer API 处理,并且再次旨在为老实说非常复杂的功能提供不可知且可重复使用的工作流程.
And using actionListener might be useless for that moreover I'm keeping different responses for different kinds of events .
不,但是您可以改用 ButtonModel
,例如...
btn.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (btn.getModel().isRollover()) {
System.out.println("You have entered the spiders web");
} else {
System.out.println("You have escaped");
}
}
});
btn.setRolloverEnabled(true);
(注意 - JButton
还可以自动支持“翻转”图像)
或者如果您只想显示工具提示,您可以使用...
btn.setToolTipText("What are you waiting for");
如果您想要一些自定义键盘操作,您可以使用 Key Bindings API,例如...
InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = btn.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_M, InputEvent.ALT_DOWN_MASK), "pressed");
am.put("pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Activiated via the keyboard");
JButton btn = (JButton) e.getSource();
btn.doClick();
}
});
我还建议您看看 Introduction to Event Listeners。
现在,如果您真的一心想制作自己的按钮,那么 JButton
真的不是开始的地方。您可能想要查看 ButtonUI
并感受 API,但这是 Swing 的真正高级水平,老实说,没有多少经验丰富的开发人员能够达到这个水平。
But what about MouseWheel?
除了向 JButton
的现有实例添加 MouseWheelListener
之外,为什么还需要做其他事情?您打算提供哪些不需要委托给其他侦听器的“额外”功能?
当然,也许你可以用它来增加或减少我猜的计数器,但是,我可能只会使用一个单独的 MouseWheelListener
无论如何,这不是我想要的功能考虑定期有用...事实上,我不能说我曾经一起使用过这两个。
OO 中有一个通用的概念,"prefer composition over inheritance",你可能想熟悉它。
可运行示例...
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JButton btn = new JButton("Click me");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("I was clicked");
}
});
btn.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (btn.getModel().isRollover()) {
System.out.println("You have entered the spiders web");
} else {
System.out.println("You have escaped");
}
}
});
btn.setRolloverEnabled(true);
btn.setMnemonic('m');
InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = btn.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_M, InputEvent.ALT_DOWN_MASK), "pressed");
am.put("pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Activiated via the keyboard");
JButton btn = (JButton) e.getSource();
btn.doClick();
}
});
btn.setToolTipText("What are you waiting for");
btn.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
System.out.println("I've been wheeled");
}
});
add(btn);
}
}
}
请理解,我不是建议你“不能”做你想做的所有事情,我想建议的是,首先弄清楚那些事情可能“如何”已经完成,然后探索哪些对您有用,哪些不适合您。从那里您可以开始探索替代工作流程。
我实际上已经开始学习 javax.swing 图形。对于像 KeyListener、MouseListener 等事件监听,我必须用它们自己的 headers.
实现它们的所有方法我以前是做JS的,那里的事件监听很简单
所以我决定创建自己的 class myButton,它扩展了 JButton 并实现了所有需要的侦听器。但是单击按钮时必须执行的操作的语句通常应该放在 mouseClicked 中,但是在外部使用它我不知道如何使用它。另一件事是我想在 myButton class 中创建一个 onClick 方法,该方法具有与 JS 相同的函数名称的参数。但我不知道如何创建该功能。此外 Java 中不存在函数传递。还有一个问题是如何告诉程序onClick方法是点击按钮时应该执行的方法。
所以这是我想澄清并知道如何实现它们的两件事。
- 使用我自己的方法 (onClick) 而不是默认方法 (mouseClicked)。
- 如何像在 Javascript. 中那样将函数名称或匿名函数(应该实际实现)作为参数传递给 onClick 方法
提前致谢
说真的,我认为你需要去阅读 How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener 这最终会做你想做的事
所以像...
JButton btn = new JButton("Click me");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("I was clicked");
}
});
将完全按照您刚才的描述进行操作。事实上,大多数组件已经内置了一些鼠标 and/or 键盘控件,因此您“不必”重新发明整个轮子。
So I decided to create my own class myButton which extends JButton and also implements all the listeners required. But the statements for what has to be done when the button is clicked should be placed in mouseClicked normally, but using that externally I'm not getting how to use that.
是的,这就是自定义 interface
的用途 - 但问题是,当“鼠标”动作发生时按钮生成事件是否与“键盘”并列" 操作还是其他操作?
这就是通常使用 ActionListener
的原因,因为它提供了一个不可知的事件处理程序来处理按钮何时被“激活”,通常,无论它是如何触发的
Another thing is that I want to create a method onClick inside myButton class which has the parameter of the function name same as JS. But I don't know how to create that function. Moreover function passing is not there in Java. And another problem is that how can I tell the program the onClick method is the method that should be implement when the button is clicked.
Java中没有函数,只有对象。这是 interface
有用的地方,它是一个合同,描述了期望实现提供的功能。
您已经使用过它们,您只需要定义您预期的“触发”/method/callback 将用于您的“onClick”操作。
Ok but I want the button to listener for more events like hover, drag etc.
好吧,拖放由 Drag and Drop and Data Transfer API 处理,并且再次旨在为老实说非常复杂的功能提供不可知且可重复使用的工作流程.
And using actionListener might be useless for that moreover I'm keeping different responses for different kinds of events .
不,但是您可以改用 ButtonModel
,例如...
btn.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (btn.getModel().isRollover()) {
System.out.println("You have entered the spiders web");
} else {
System.out.println("You have escaped");
}
}
});
btn.setRolloverEnabled(true);
(注意 - JButton
还可以自动支持“翻转”图像)
或者如果您只想显示工具提示,您可以使用...
btn.setToolTipText("What are you waiting for");
如果您想要一些自定义键盘操作,您可以使用 Key Bindings API,例如...
InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = btn.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_M, InputEvent.ALT_DOWN_MASK), "pressed");
am.put("pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Activiated via the keyboard");
JButton btn = (JButton) e.getSource();
btn.doClick();
}
});
我还建议您看看 Introduction to Event Listeners。
现在,如果您真的一心想制作自己的按钮,那么 JButton
真的不是开始的地方。您可能想要查看 ButtonUI
并感受 API,但这是 Swing 的真正高级水平,老实说,没有多少经验丰富的开发人员能够达到这个水平。
But what about MouseWheel?
除了向 JButton
的现有实例添加 MouseWheelListener
之外,为什么还需要做其他事情?您打算提供哪些不需要委托给其他侦听器的“额外”功能?
当然,也许你可以用它来增加或减少我猜的计数器,但是,我可能只会使用一个单独的 MouseWheelListener
无论如何,这不是我想要的功能考虑定期有用...事实上,我不能说我曾经一起使用过这两个。
OO 中有一个通用的概念,"prefer composition over inheritance",你可能想熟悉它。
可运行示例...
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JButton btn = new JButton("Click me");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("I was clicked");
}
});
btn.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (btn.getModel().isRollover()) {
System.out.println("You have entered the spiders web");
} else {
System.out.println("You have escaped");
}
}
});
btn.setRolloverEnabled(true);
btn.setMnemonic('m');
InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = btn.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_M, InputEvent.ALT_DOWN_MASK), "pressed");
am.put("pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Activiated via the keyboard");
JButton btn = (JButton) e.getSource();
btn.doClick();
}
});
btn.setToolTipText("What are you waiting for");
btn.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
System.out.println("I've been wheeled");
}
});
add(btn);
}
}
}
请理解,我不是建议你“不能”做你想做的所有事情,我想建议的是,首先弄清楚那些事情可能“如何”已经完成,然后探索哪些对您有用,哪些不适合您。从那里您可以开始探索替代工作流程。