首页 > 解决方案 > 如何检查用户是否按下特定键,即 Enter Key Like python 的键盘模块?

问题描述

我正在用java(我知道太雄心勃勃)制作一个gui聊天应用程序,它可以向连接的房间发送消息。我还没有做网络的东西,但只是简单的 gui。我有一个输入消息所在的文本字段和一个发送消息的文本区域,其中带有一个按钮来发送它。我想要它,这样当我按下回车键时,发送任务就会发生。我的代码是

import javax.swing.*;
import java.awt.*;

public class Jabba {
    //Class
    public static void main(String args[]) {
    //Main Method
        //main frame
        JFrame frame = new JFrame("Chat Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);

        //The Menu that will later allow us to connect and create rooms to join a chat
        JMenuBar mb = new JMenuBar();
        JMenu m1 = new JMenu("Connect");
        JMenu m2 = new JMenu("Help");
        //This is for help
        mb.add(m1);
        mb.add(m2);
        JMenuItem m11 = new JMenuItem("Create new room");
        JMenuItem m22 = new JMenuItem("Join an Existing Room");
        m1.add(m11);
        m1.add(m22);

        //Our panel
        JPanel panel = new JPanel();
        
        JLabel label = new JLabel("Enter Text");
        //The Message field
        JTextField tf = new JTextField(15);
        //The Sending button
        JButton send = new JButton("Send");
        
        
        //The resetting button
        JButton reset = new JButton("Reset");
        //Adding the panel
        panel.add(label); 
        panel.add(tf);
        panel.add(send);
        panel.add(reset);

        //The text area where the messages will go
        JTextArea ta = new JTextArea();
        //Adding it to the Scroll Pane so that It has scrolls
        JScrollPane sp = new JScrollPane(ta);
        
        //Actionlisteners allow us to listen to the actions that take place with 
        //The Specific components like here when the button is pressed
        send.addActionListener(e ->{
            //It will first store the text of the the text field in a
            //variable called msg
            String msg = tf.getText();  
            //Then Will remove the Text from the field so that new messages can be 
            //sent
            tf.setText(null);
            //Now it will send The message to the message text area
            ta.append(msg+"\n");
            });
        reset.addActionListener(e ->{
            //This is for the reset option
                ta.setText(null);
            //It will jus set all the text of the message area to null
            //i.e. Nothing
        }
        );  
        
        //adds all the content and components to the main frame.
        frame.getContentPane().add(BorderLayout.SOUTH, panel);
        frame.getContentPane().add(BorderLayout.NORTH, mb);
        /*notice the BorderLayout here, it is from the awt package and it 
        is letting us lay the components in a specific layout. 
        Also we have changed the component below after the center to sp
        i.e. it is the scrollpane instead of our textarea to get the Scroll Bars!!*/
        frame.getContentPane().add(BorderLayout.CENTER, sp);
        frame.setVisible(true);
        //Pretty Self-Explanatory
    }
}

在此处输入图像描述 如果我问错了问题,请帮助我并原谅我,因为我不太明白如何使用 KeyListener 类...## Heading ##

标签: javaswingawtkeylistener

解决方案


因此,正如疯狂程序员告诉我并帮助我的那样,我在该文本字段中使用了一个动作监听器,并将我用于发送消息的代码复制到文本字段 var tf 的动作监听器中。所以在伪代码中是:

tf.addActionListener(e ->{
            String msg = tf.getText();
            tf.setText(null);
            ta.append(msg+"\n");
        });

推荐阅读