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(5,30); JButton jb = new JButton("发送"); JTextField jtf = new JTextField(15); 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() + " "); jtf.setText(""); } }; jb.addActionListener(sendMsg); jtf.getInputMap().put(KeyStroke.getKeyStroke(' ', 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: