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

标签 java swing oop methods

请耐心等待,我对 Java 很陌生。我正在创建一个简单的应用程序,用于创建论文大纲并将引用文献格式化为 APA 格式。我在 Outline 类中有一个带有 setter 和 getter 方法的对象 myOutline 。所有 GUI 输入后,大纲和引用文献将打印到 txt 文件中。

我有一个名为代词的方法,用于检查用户论文中的人称代词。我对所有代词进行了硬编码(我知道使用数组列表更好,但还没有达到可以编码的水平)。如果它找到代词,则会弹出一个对话框并警告用户,要求他们重新编写。

我的问题是,一旦用户收到错误消息,所有后续输入也都是错误,无论代词如何。我的问题:如何清除 JTextField 输入以便该方法检查新字符串而不是前一个字符串?我尝试使用 setter 方法和 ("") 或 (null) 但它不起作用。

我只会发布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 永远不会设置为 false

尝试将 personalPronoun = false; 添加到 private void pronoun (String thesisInput){

例如...

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

    if (...)

如果您实际上并未在代码中的其他任何地方使用 personalPronoun,那么将其设为局部变量可能会更容易...

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

您还可以找到 Validating Input 部分的How to Use the Focus Subsystem有一些兴趣...

关于java - 如何清除 JTextField 中的输入错误并存储成功的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20603188/

相关文章:

java - spring-aop 是否可以抛出检查异常?

java - 是否有任何启发式/模式来记录用户操作

java - 非法状态异常 : get field slot from row 0 col -1 failed

java - 覆盖 Java 中的抽象方法

java - 我试图在java中绘制正方形,但它不起作用

java - 在 spring mvc 中自定义 swagger 注解

Java:JTextAreas后面的JPanel的MouseListener

java - 从构造函数创建对象时执行某些方法

c# - 具有不同返回类型的方法重载

python - 减少 Dart 中包装对象的数量