java 使用类的普通方法调用 actionPerformed 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16250669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call actionPerformed Method using normal method of class
提问by Vighanesh Gursale
I am trying to callthe actionPerformed()
in normal methodof class. I know that it get automatically executed on whenever button get pressed. But I want to call that method when ENTER button get pressed on Specific textfield. Is it possible to call actionPerformed()
in keyPressed()
or in normal function/method.
我想呼吁的actionPerformed()
在正常的方法类的。我知道只要按下按钮它就会自动执行。但是我想在特定文本字段上按下 ENTER 按钮时调用该方法。是否有可能调用actionPerformed()
的keyPressed()
或正常功能/方法。
The following code will give you rough idea what I want to do.
下面的代码会让你大致了解我想要做什么。
void myFunction()
{
actionPerformed(ActionEvent ae);
}
public void actionPerformed(ActionEvent ae)
{
//my code
}
Thanks in advance
提前致谢
回答by nIcE cOw
If you want, some actionPerformed()
method of a JButton
to be executed on pressing ENTERinside a JTextField
, then I guess you can use the doClick(), method from AbstractButton
class to achieve this. Though this approach, might can override the original behaviour of the JTextField
on press of the ENTERkey :(
如果需要,actionPerformed()
在 a内部JButton
按下时执行a 的某些方法,那么我想您可以使用类中的doClick()方法来实现此目的。虽然这种方法,可能可以覆盖按下键的原始行为:(ENTERJTextField
AbstractButton
JTextField
ENTER
Please have a look at this code pasted below, to see if this is what, stands fit for your needs :-) !!!
请看看下面粘贴的这段代码,看看这是否符合您的需求:-) !!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonClickExample
{
private JTextField tfield;
private JButton button;
private JLabel label;
private ActionListener actions = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == button)
{
label.setText(tfield.getText());
}
else if (ae.getSource() == tfield)
{
button.doClick();
}
}
};
private void displayGUI()
{
JFrame frame = new JFrame("Button Click Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JPanel centerPanel = new JPanel();
tfield = new JTextField("", 10);
button = new JButton("Click Me or not, YOUR WISH");
tfield.addActionListener(actions);
button.addActionListener(actions);
centerPanel.add(tfield);
centerPanel.add(button);
contentPane.add(centerPanel, BorderLayout.CENTER);
label = new JLabel("Nothing to show yet", JLabel.CENTER);
contentPane.add(label, BorderLayout.PAGE_END);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new ButtonClickExample().displayGUI();
}
});
}
}
回答by Janon
I know this is an old thread, but for other people seeing this my recomendation is something like this:
我知道这是一个旧线程,但对于其他人来说,我的推荐是这样的:
// This calls the method that you call in the listener method
void performActionPerformedMethod(){
actionPerformed(ActionEvent e);
}
// This is what you want the listener method to do
void actionPerformedMethod(){
// Code...
}
// This is the interface method
public void actionPerformed(ActionEvent e){
actionPerformedMethod()
}