日期:2014-05-20  浏览次数:20863 次

如何设置回车为快捷键,就像QQ发送消息一样!
我在文本框设置了键盘监听 当释放enter时,方法内容跟发送按钮的一样,但是会多发送一行空格。
标准的设置Enter为发送是怎么设置的,只有一个ENTER键!

------解决方案--------------------
Java code
JTextField textfield = new JTextField(20);
KeyStroke enter_keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0);
Action send_message_then_clear_action = new AbstractAction(){
    @Override public void actionPerformed(ActionEvent e){               
        JTextField tf = (JTextField)e.getSource();
        sendMessage(tf.getText());
        tf.setText("");
    }
};
InputMap inputmap = textfield.getInputMap();
ActionMap actionmap = textfield.getActionMap();
inputmap.put(enter_keystroke,"send-message-then-clear");
actionmap.put("send-message-then-clear",send_message_then_clear_action);

------解决方案--------------------
Java code
package com.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ChatWin extends JFrame {

    private static final long serialVersionUID = -5418344602348249043L;

    private JPanel pup = new JPanel();
    private JPanel pdown = new JPanel();
    private JTextField txtCommand = new JTextField(45);
    private JTextArea txtContent = new JTextArea();
    private JButton btnExec = new JButton("Execute");

    public ChatWin() {
        // 指定框架的布局管理器
        setLayout(new BorderLayout());
        // 设置文本框,文本域字体
        txtCommand.setFont(new Font("", Font.BOLD, 13));
        txtContent.setFont(new Font("", Font.BOLD, 13));
        // 指定面板的布局
        pup.setLayout(new BorderLayout());
        pdown.setLayout(new FlowLayout());

        // 将文本域添加导面板中
        pup.add(txtContent);
        // 为文本域添加滚动条
        pup.add(new JScrollPane(txtContent));
        // 将文本框,按钮添加到面板中
        pdown.add(txtCommand);
        pdown.add(btnExec);

        // 添加面板到框架中
        this.add(pup, BorderLayout.CENTER);
        this.add(pdown, BorderLayout.SOUTH);

        // 设置事件

        // 添加按钮事件
        btnExec.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sendMsg();
            }
        });

        // 添加键盘Enter事件
        txtCommand.addKeyListener(new KeyListener() {

            public void keyPressed(KeyEvent e) {
                // 当按下回车时
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    sendMsg();
                }
            }

            public void keyReleased(KeyEvent e) {
            }

            public void keyTyped(KeyEvent e) {
            }
        });
    }

    private void sendMsg() {
        String command = txtCommand.getText().trim();
        txtContent.append("You: " + command + "\r\n");
        txtCommand.setText("");
    }

    public static void main(String[] args) {
        ChatWin frame = new ChatWin();
        frame.setTitle("ChatWin");
        frame.setSize(666, 444);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}