简体   繁体   English

如何在Java中将动作执行应用于Jtextfields?

[英]how to apply actionperformed to Jtextfields in java?

i am buiding a small jframe with 2 lables and 2 textfields(Jtextfield1,Jtextfield2) and a jbutton .i want that when the user enters some data in the Jtextfield1,the data is retrieved from database regarding user and his details are displayed in the Jtextfield2. 我正在构建一个带有2个标签和2个文本字段(Jtextfield1,Jtextfield2)和一个jbutton的小型jframe。我希望当用户在Jtextfield1中输入一些数据时,从数据库中检索有关用户的数据,并且他的详细信息显示在Jtextfield2中。 i know how to retrieve data from database but how to apply actionperformed to Jtextfield1.please help and give an example too. 我知道如何从数据库检索数据,但如何将执行的操作应用于Jtextfield1。请帮助并给出示例。

i want that when the user enters some data in the Jtextfield1,the data is retrieved from database 我希望当用户在Jtextfield1中输入一些数据时,该数据是从数据库中检索的

What does this mean? 这是什么意思? Does it mean: 它的意思是:

a) that as a user types each character you want to query the database? a)当用户键入要查询数据库的每个字符时? If so, then add a DocumentListener to the Document of the text field 如果是这样,则将DocumentListener添加到文本字段的Document中

b) that when a user is finished typing you want to query the database? b)当用户完成输入后,您要查询数据库吗? If so, then how does the program know when the user is finished typing? 如果是这样,那么程序如何知道用户何时输入? If the user is finished typing when 如果用户在完成输入时

i) the Enter key is pressed, then add an ActionListener to the text field. i)按下Enter键,然后将ActionListener添加到文本字段。

ii) the text field loses focus, then add a FocusListener to the text field. ii)文本字段失去焦点,然后将FocusListener添加到文本字段。

Or are you simply talking about the user clicking a button when the text has been entered in both text fields? 还是您只是在谈论在两个文本字段中都输入了文本时用户单击按钮? If so then you add an ActionListener to the button. 如果是这样,则将ActionListener添加到按钮。

Also you can invoke the button even when focus isn't on the button by using the Enter key with the following code: 此外,即使在焦点不在按钮上的情况下,也可以通过使用Enter键和以下代码来调用按钮:

frame.getRootPane().setDefaultButton( button );

The question is too vague to give a specific answer. 这个问题太含糊,无法给出具体答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM