import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent; public class BindKeyTest {
JFrame jf = new JFrame("test keyboard bind");
JTextArea jta = new JTextArea(,);
JButton jb = new JButton("发送");
JTextField jtf = new JTextField();
public void init(){
jf.add(jta);
var 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', InputEvent.CTRL_DOWN_MASK), "send");
jtf.getActionMap().put("send", sendMsg); jf.pack();
jf.setVisible(true);
} public static void main(String[] args) {
new BindKeyTest().init();
} }
outpu: