如何检查用户是不是正在按下特定键,即 Enter Key Like python's keyboard module?
Posted
技术标签:
【中文标题】如何检查用户是不是正在按下特定键,即 Enter Key Like python\'s keyboard module?【英文标题】:How can I check if a user is pressing a specific key i.e. Enter Key Like python's keyboard module?如何检查用户是否正在按下特定键,即 Enter Key Like python's keyboard module? 【发布时间】:2021-11-24 15:35:43 【问题描述】:我正在用 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
enter image description here 如果我问错了问题,请帮助我并原谅我,因为我不太明白如何使用 KeyListener 类...## Heading ##
【问题讨论】:
我建议将ActionListener
附加到JTextField
,可能使用send
按钮正在使用的实例。请参阅How to Use Text Fields 和How to Write an Action Listener 了解更多详情
【参考方案1】:
所以正如疯狂程序员告诉我并帮助我的那样,我在该文本字段中使用了一个动作监听器,并将我用于发送消息的代码复制到文本字段 var tf 的动作监听器中。所以在伪代码中是:
tf.addActionListener(e ->
String msg = tf.getText();
tf.setText(null);
ta.append(msg+"\n");
);
【讨论】:
创建一个Action
。然后,您可以将相同的 Action 实例添加到您的 JTextField 和您的 JButton。阅读 How to Use Actions 上的 Swing 教程中的部分以获取更多信息。或者,如果您要使用 lambda,那么您应该创建一个可以从两个组件调用的方法,这样您就不会重复代码。以上是关于如何检查用户是不是正在按下特定键,即 Enter Key Like python's keyboard module?的主要内容,如果未能解决你的问题,请参考以下文章
QTableWidget中编辑单元格,如何实现按下Enter键实现对文件名是不是重复的判断?