使用 JButton 时如何重用 actionListener 的按键方法?
How to reuse actionListener's method for key-press when using a JButton?
我正在 Java 中创建计算器 GUI 应用程序。我已经实现了用鼠标按下 JButton 的计算器。我也想听小键盘按下数字的声音,但我不想在 ActionListener 中重新创建该方法。
例如,这就是我在按下 JButton 时实现 listenOne 的方式。
class ListentoOne implements ActionListener{
public void actionPerformed(ActionEvent arg) {
if(floating)
aftDec+="1";
else if(!operanD)
ans=(ans*10)+1;
else
operand=(operand*10)+1;
screen.setText(output+="1");
}
}
在面板的构造函数中 class 我是这样构造 JButton 的:
one=new JButton("1");
one.addActionListener(new ListentoOne());
我会为此使用 Key Bindings,而不是 KeyListener
。当然,有多种方法可以通过键绑定实现这一点,但在我的示例中,我创建了新的 class,如果您按下 1
.
,它只会调用按钮上的 doClick()
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
public class Example {
public Example() {
JLabel label = new JLabel("0");
ShortCutButton button = new ShortCutButton("1", KeyStroke.getKeyStroke(KeyEvent.VK_1, 0));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(Integer.parseInt(label.getText()) + 1));
}
});
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.add(button);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
public class ShortCutButton extends JButton {
public ShortCutButton(String text, KeyStroke keyStroke) {
setText(text);
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "42");
getActionMap().put("42", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
doClick();
}
});
}
}
}
for(ActionListener a: button.getActionListeners()) {
a.actionPerformed(yourActionEvent);
}
查看 Calculator Panel 示例。
它展示了如何为所有按钮共享一个 ActionListener。该示例还使用 KeyBindings,以便您可以单击按钮或键入按钮上显示的数字来调用操作。
我正在 Java 中创建计算器 GUI 应用程序。我已经实现了用鼠标按下 JButton 的计算器。我也想听小键盘按下数字的声音,但我不想在 ActionListener 中重新创建该方法。
例如,这就是我在按下 JButton 时实现 listenOne 的方式。
class ListentoOne implements ActionListener{
public void actionPerformed(ActionEvent arg) {
if(floating)
aftDec+="1";
else if(!operanD)
ans=(ans*10)+1;
else
operand=(operand*10)+1;
screen.setText(output+="1");
}
}
在面板的构造函数中 class 我是这样构造 JButton 的:
one=new JButton("1");
one.addActionListener(new ListentoOne());
我会为此使用 Key Bindings,而不是 KeyListener
。当然,有多种方法可以通过键绑定实现这一点,但在我的示例中,我创建了新的 class,如果您按下 1
.
doClick()
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
public class Example {
public Example() {
JLabel label = new JLabel("0");
ShortCutButton button = new ShortCutButton("1", KeyStroke.getKeyStroke(KeyEvent.VK_1, 0));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(Integer.parseInt(label.getText()) + 1));
}
});
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.add(button);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
public class ShortCutButton extends JButton {
public ShortCutButton(String text, KeyStroke keyStroke) {
setText(text);
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "42");
getActionMap().put("42", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
doClick();
}
});
}
}
}
for(ActionListener a: button.getActionListeners()) {
a.actionPerformed(yourActionEvent);
}
查看 Calculator Panel 示例。
它展示了如何为所有按钮共享一个 ActionListener。该示例还使用 KeyBindings,以便您可以单击按钮或键入按钮上显示的数字来调用操作。