尝试向 JTextField 添加事件侦听器 [英] Trying to add an event listener to a JTextField

查看:86
本文介绍了尝试向 JTextField 添加事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一个事件侦听器,因此当您在 JTextField 中按 Enter 时,会发生一些事情.到目前为止,我已经有了这个

I'm trying to add an event listener so when you press enter in the JTextField something happens. So far I've got this

SendingHandler sendingHandler;
...
JTextField draftMessage = new JTextField("field");
draftMessage.addActionListener(sendingHandler);

...

private class SendingHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        System.out.println(command);
    }
}

command 但是永远不会被打印出来.这不应该工作吗?

command however is never printed out. Shouldn't this be working?

推荐答案

由于您编辑了问题并说明您使用的是 JTextField,我将对答案重新排序:

Since you edited the question and clarified that you are using a JTextField I will reorder the answer:

您看不到任何输出,因为尚未设置操作命令,因此没有可显示的内容.

You don't see any output because the action command has not been set so there is nothing to display.

尝试在您的 ActionListener 中使用以下内容:

Try using the following in your ActionListener:

JTextField textField = (JTextField)e.getSource();
System.out.println( textField.getText() );

当然,这只会在用户在文本字段中输入内容时显示内容.关键是只有在有东西要显示时你才会看到输出.您始终可以通过显示硬编码字符串来验证一段代码是否正在执行.

Of course this will only display something if the user typed something into the text field. The point is you will only see output if there is something to display. You can always verify that a piece of code is being executed by displaying a hard coded string.

但是,如果您的问题是关于 JTextArea,那么在 JTextArea 上使用 Enter 键 的默认 Action 是插入换行符在文本区域.

However, if you question was about a JTextArea, then default Action for using the Enter key on a JTextArea is to insert a newline in the text area.

如果你想调用一个动作,那么你需要替换默认的动作:

If you want to invoke an action then you need to replace the default Action:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextAreaEnter extends JPanel
{
    private JTextArea message = new JTextArea(5, 20);
    private JTextArea display = new JTextArea(5, 20);

    public TextAreaEnter()
    {
        display.setEditable( false );

        add( new JScrollPane(message) );
        add( new JScrollPane(display) );

        Action enter = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                display.append( message.getText() + "\n" );
                message.setText("");
            }
        };

        message.getActionMap().put("insert-break", enter);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("TextAreaEnter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TextAreaEnter() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

请参阅:键绑定以获取默认列表每个 Swing 组件的绑定.

See: Key Bindings for a list of the default bindings for each Swing component.

这篇关于尝试向 JTextField 添加事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆