2013-04-27 181 views
3

我正在尝试调用actionPerformed()正常方法的类。我知道只要按下按钮,它就会自动执行。但是我想在按下ENTER按钮时调用该方法。具体的textfield。是否可以在keyPressed()或正常的函数/方法中调用actionPerformed()调用actionPerformed方法使用普通方法的类

下面的代码会给你粗略的想法,我想要做什么。

void myFunction() 
{ 
     actionPerformed(ActionEvent ae); 
} 

public void actionPerformed(ActionEvent ae) 
{ 
     //my code 
} 

在此先感谢

+1

,如果你想在执行方法调用然后移动你不应该这样做应用程序逻辑到另一种方法并称之为处理事件是一项EDT工作。 – 2013-04-27 09:56:58

+1

让ActionListener执行它的工作有什么问题?这是'ActionListener'为'doClick()'执行的无论如何 – MadProgrammer 2013-04-27 10:18:19

回答

5

如果你想,就按ENTER一个JTextField内执行的JButton的一些actionPerformed()方法,那么我想你可以使用doClick(),方法从AbstractButton类做到这一点。虽然这种做法,可能可以覆盖的ENTER键 :(

请看看这段代码粘贴下面按JTextField的原始行为,看看这是什么,代表适合您的需要: - )!!!

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(); 
      } 
     }); 
    } 
} 
+1

+的确切行为,也可以通过键绑定看到[这里](http://stackoverflow.com/a/5797965/230513)。 – trashgod 2013-04-27 12:35:40

+0

嘿,谢谢你的代码对我很有用,非常感谢........:D – 2013-05-04 06:25:53

+0

@VighaneshGursale:你最欢迎并保持微笑:-) – 2013-05-04 07:51:15

2

我知道这是一个古老的线程,但对于其他人看到这我recomendation是这样的:

// 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() 
} 
+0

这是一个不容置疑的好方法。更基于模块化,我喜欢+1 :-) – 2014-11-10 15:48:05