简体   繁体   English

如何使用Netbeans不使用Jbutton来更改Jtextbox值

[英]how to change Jtextbox values without using Jbutton using netbeans

I want to create a small Netbeans application related to school students. 我想创建一个与学生有关的小型Netbeans应用程序。 I have two JTextFields . 我有两个JTextFields In this JTextFields I want to show some words like "apple" , then want to show "mango" etc. 在这个JTextFields中,我想显示一些单词,例如“ apple” ,然后显示“ mango”等。

The details are as follows. 详情如下所示。

  1. When the application is start I want to show in JtextField1 as "apple" . 当应用程序启动时,我想在JtextField1中显示为“ apple” When the student type it as same or wrong entry then auto move to next word "mango" in same JTextfield1. 当学生键入相同或错误的条目时,然后自动移至同一JTextfield1中的下一个单词“ mango”

  2. In this application JButton will not be use. 在此应用程序中,将不使用JButton。

I'm guessing that you have two JTextFields, one that the user is not supposed to edit and is for display only -- so make that field non-editable by 我猜您有两个JTextField,一个用户不应该编辑,只能显示-因此,请使该字段不可编辑

  1. calling setFocusable(false) so the JTextField can never receive focus 调用setFocusable(false)因此JTextField永远无法获得焦点
  2. And if you desire, calling setEditable(false) 如果需要,可以调用setEditable(false)

In your 2nd JTextField, give it an ActionListener via addActionListener(...) that inside of the listener have the code check the 2nd JTextField's text and if incorrect, change the text in the first JTextField. 在您的第二个JTextField中,通过addActionListener(...)给它一个ActionListener,以使侦听器内部的代码可以检查第二个JTextField的文本,如果不正确,请更改第一个JTextField中的文本。

firstTextField.setFocusable(false);
firstTextField.setEditable(false);

secondTextField.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String text = secondTextField.getText();

        // here check the text String and if incorrect
        // call setText(...) on the firstTextField
    }
});

Note that by adding an ActionListener to the JTextField itself, you now have a listener that is activated when the user presses the enter button when this field has focus. 请注意,通过将动作侦听器添加到JTextField本身,您现在有了一个侦听器,当用户在此字段具有焦点时按下Enter键时,该侦听器将被激活。 No need for a JButton for this to work. 无需JButton即可工作。

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

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