如何设置回车为快捷键,就像QQ发送消息一样! 我在文本框设置了键盘监听 当释放enter时,方法内容跟发送按钮的一样,但是会多发送一行空格。标准的设置Enter为发送是怎么设置的,只有一个ENTER键! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在 MSN 中回车就是发送了 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); 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); }} 事件在 KeyRelease 事件里面做 文本框用ActionListener来监听 呵呵,火龙果大哥这个说的不全了,哈哈,也能指正一下火龙果大哥不容易呀QQ里可调的,可以回车发送,也可以Ctrl+Enter发送 this.getRootPane().setDefaultButton(button); 怎么动态生成树? java File的一个小问题 (火急) 打开src,想看看eventListerner里边写了点啥,结果一看,大吃一惊,什么也没有.这是为何??? 怎样返回数组? 通过tomcat连接池在java类访问数据库失败! 解决这种乱码问题立即给分 在线等待:这个类偶在JSP里不会调用了,求肋。。。。(这是个加密的类) 请教一个基础问题,关于堆和栈,以及内存回收的相关知识,请高手进来帮忙,谢谢!平时不关心内存的人也进来学习一下吧。 如何让文本框只能输入一个数字后就将光标跳到下一个文本框里? 请教一个内存回收问题 关于Java泛型的问题,知道的请进!!! iframe自适应高度
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);
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);
}}
QQ里可调的,可以回车发送,也可以Ctrl+Enter发送