package ch12;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BindKeyTest {
JFrame jf = new JFrame("测试键盘绑定");
JTextArea jta = new JTextArea(5,30);
JButton jb = new JButton("发送");
JTextField jtf = new JTextField(15);
public void init() {
jf.add(jta);
JPanel jp = new JPanel();
jp.add(jtf);
jp.add(jb);
jf.add(jp, BorderLayout.SOUTH);
Action sendMsg = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jta.append(jtf.getText() + "\n");
jtf.setText("");
}
};
jb.addActionListener(sendMsg);
jtf.getInputMap().put(KeyStroke.getKeyStroke('\n',java.awt.event.InputEvent.CTRL_MASK),"send");
jtf.getActionMap().put("send",sendMsg);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new BindKeyTest().init();
}
}
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6151582.html,如需转载请自行联系原作者