简体   繁体   English

如何清除JTextField中的输入错误并存储成功的错误

[英]How to clear input errors from JTextField and store successful ones

Please be patient, I am very new to Java. 请耐心等待,我对Java很新。 I'm creating a simple application that creates an essay outline and formats references into APA format. 我正在创建一个简单的应用程序,创建一个论文大纲并将引用格式化为APA格式。 I have an object myOutline in class Outline with setter and getter methods. 我在类Outline中有一个对象myOutline,带有setter和getter方法。 After all the GUI input, the outline and references are printed to a txt file. 在所有GUI输入之后,大纲和引用被打印到txt文件。

I have a method called pronoun that checks the user's thesis for personal pronouns. 我有一种称为代词的方法,用于检查用户的论文中的人称代词。 I hardcoded all the pronouns (I know using an array list is better but am not at a level where I can code that yet). 我硬编码所有的代词(我知道使用数组列表更好,但我不能达到我可以编码的水平)。 If it finds a pronoun, a dialog box pops up and alerts the user, asking them to write it again. 如果找到代词,会弹出一个对话框并提醒用户,要求他们再次写入。

My problem is that once the user receives the error message, all subsequent inputs are also errors, regardless of pronoun. 我的问题是,一旦用户收到错误消息,所有后续输入也是错误,无论代词如何。 My question: How can I clear the JTextField input so that the method checks a new String, not the previous one? 我的问题:我如何清除JTextField输入,以便该方法检查一个新的String,而不是前一个? I tried using the setter method and ("") or (null) but it's not working. 我尝试使用setter方法和(“”)或(null)但它不起作用。

I will only post the actionListener section and pronoun method. 我只会发布actionListener部分和代名方法。

public void actionPerformed(ActionEvent e){

            Object source = e.getSource();                    

            if (e.getSource() == btnThesisInput) {

                String thesisInput = txtThesisInput.getText();
                pronoun(thesisInput);
                }
}

private void pronoun (String thesisInput){  

            if (thesisInput.contains(" I ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" me ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" you ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" we ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" us ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" our ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" he ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" him ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" she ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" her ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" they ")){
                personalPronoun = true;
            }
            if (thesisInput.contains(" them ")){
                personalPronoun = true;
            }

            if (personalPronoun == true){   
                JOptionPane.showMessageDialog(null, "Oops! Looks like your thesis contains personal pronouns. Try again."); 
                myOutline.setThesis("");
                txtThesisInput.setText(null);  

            }

            else{  
                myOutline.setThesis(thesisInput);
                panelThesisInput.setVisible(false);
                getContentPane().add (panelArgumentInput);
                panelArgumentInput.setVisible(true);   
            }

}

personalPronoun is never set to false personalPronoun永远不会被设置为false

Try adding personalPronoun = false; 尝试添加personalPronoun = false; to private void pronoun (String thesisInput){ to private void pronoun (String thesisInput){

For example... 例如...

private void pronoun (String thesisInput){  
    personalPronoun = false;

    if (...)

If you're not actually using personalPronoun anywhere else in the code, it might just be easier to make it a local variable instead... 如果你实际上没有在代码中的任何其他位置使用personalPronoun ,那么将它变成局部变量可能更容易......

private void pronoun (String thesisInput){  
    boolean personalPronoun = false;

You may also find the section Validating Input of How to Use the Focus Subsystem of some interest... 您还可以找到一些感兴趣的如何使用焦点子系统验证输入部分...

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

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