Java 如何重用actionListener';使用JButton时的按键方法?
我正在用Java创建一个计算器GUI应用程序。我已经实现了用鼠标按下按钮的计算器。我想听听numpad的按键,但是我不想在ActionListener中重新创建这个方法 例如,当按下JButton时,我就是这样实现listenOne的Java 如何重用actionListener';使用JButton时的按键方法?,java,swing,jbutton,windowbuilder,Java,Swing,Jbutton,Windowbuilder,我正在用Java创建一个计算器GUI应用程序。我已经实现了用鼠标按下按钮的计算器。我想听听numpad的按键,但是我不想在ActionListener中重新创建这个方法 例如,当按下JButton时,我就是这样实现listenOne的 class ListentoOne implements ActionListener{ public void actionPerformed(ActionEvent arg) { if(floating)
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");
}
}
在panel类的构造函数中,我以这种方式构造了JButton:
one=new JButton("1");
one.addActionListener(new ListentoOne());
我会使用它,而不是键侦听器。当然,有多种方法可以通过键绑定实现这一点,但在我的示例中,我创建了一个新类,如果按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();
}
});
}
}
}
我会使用它,而不是键侦听器。当然,有多种方法可以通过键绑定实现这一点,但在我的示例中,我创建了一个新类,如果按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();
}
});
}
}
}
看看这个例子
它显示了如何为所有按钮共享一个ActionListener。该示例还使用键绑定,以便您可以单击按钮或键入按钮上显示的数字来调用该操作。查看该示例
它显示了如何为所有按钮共享一个ActionListener。该示例还使用了键绑定,以便您可以单击按钮或键入按钮上显示的数字来调用该操作。这是bunch的最佳答案这是回答中bunchelaborate的最佳答案详细说明您的答案