【问题标题】:Call actionPerformed Method using normal method of class使用类的普通方法调用 actionPerformed 方法
【发布时间】:2013-04-21 11:13:40
【问题描述】:

我正在尝试调用类的普通方法中的actionPerformed()。我知道每当按下按钮时它都会自动执行。但是我想在特定 textfield 上按下 ENTER 按钮时调用该方法。是否可以在keyPressed() 或普通函数/方法中调用actionPerformed()

下面的代码会让你大致了解我想要做什么。

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

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

提前致谢

【问题讨论】:

  • 如果您想要执行方法调用然后将您的应用程序逻辑移动到另一个方法并调用它处理事件是 EDT 作业,您不应该这样做。
  • ActionListener 完成工作有什么问题?这是ActionListener 无论如何都会执行的确切行为

标签: java swing actionlistener keylistener keyevent


【解决方案1】:

如果你愿意,一些JButtonactionPerformed() 方法将在JTextField 内按ENTER 时执行,那么我猜你可以使用doClick() 中的方法987654326@ 类来实现这一点。虽然这种方法,可能可以覆盖 JTextField 在按下 ENTER 键时的原始行为:(

请查看下面粘贴的这段代码,看看这是否符合您的需求:-) !!!

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 表示doClick(),也可以看到here 带有键绑定。
  • @VighaneshGursale : 最欢迎你并保持微笑:-)
【解决方案2】:

我知道这是一个旧线程,但对于看到这个的其他人,我的建议是这样的:

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

【讨论】:

  • 这无疑是一个好办法。更基于模块化,我喜欢 +1 :-)
猜你喜欢
  • 2013-03-05
  • 1970-01-01
  • 2018-08-20
  • 2013-03-27
  • 2012-09-13
  • 1970-01-01
  • 2014-06-06
  • 2012-06-23
  • 1970-01-01
相关资源
最近更新 更多