简体   繁体   English

从另一个类清除JTextArea

[英]Clearing a JTextArea from another class

I'm very new to Java and I'm setting myself the challenge on writing a Caesar shift cipher decoder. 我对Java还是很陌生,因此在编写Caesar移位密码解码器方面面临挑战。 I'm basically trying to clear a JTextArea from another class. 我基本上是想从另一个类中清除JTextArea。 I have two classes, a GUI class called CrackerGUI and a shift class. 我有两个类,一个称为CrackerGUI的GUI类和一个shift类。 The JtextArea is in the GUI class along with the following method: JtextArea与以下方法一起位于GUI类中:

public void setPlainTextBox(String text)
{
    plainTextBox.setText(text);
}

The GUI class also has a clear button with the following: GUI类还具有一个带有以下内容的清除按钮:

private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Shift classShift = new Shift();
    classShift.btnClear(); 
}   

Lastly i have the method in the shift class to clear the JTextArea. 最后,我在shift类中具有清除JTextArea的方法。

public class Shift extends CrackerGUI {

public void btnClear()
{
    CrackerGUI gui = new CrackerGUI();
    gui.setPlainText(" ");
    System.out.println("testing");
} 
}

The testing text is printing out to console but the JTextArea wont clear. 测试文本正在打印到控制台,但JTextArea无法清除。 I'm not sure as to why :). 我不确定为什么:)。 I am sure it's a very simple mistake but it has me baffled. 我敢肯定这是一个非常简单的错误,但这使我感到困惑。 Any help would be appreciated. 任何帮助,将不胜感激。

Thank you in advance. 先感谢您。

You're misusing inheritance to solve a problem that doesn't involve inheritance. 您正在滥用继承来解决不涉及继承的问题。 Don't have Shift extend CrackerGUI and don't create a new CrackerGUI object inside of the btnClear() method since neither CrackerGUi is the one that's displayed. 不要使用Shift扩展CrackerGUI,也不要在btnClear()方法内部创建新的CrackerGUI对象,因为这两个CrackerGUi都不显示。 Instead have Shift hold a reference to the displayed CrackerGUI object and have it call a public method of this object. 而是让Shift持有对所显示的CrackerGUI对象的引用,并使它调用此对象的公共方法。

eg, 例如,

public class Shift  {
  private CrackerGUI gui;

  // pass in a reference to the displayed CrackerGUI object
  public Shift(CrackerGUI gui) {
    this.gui = gui;
  }

  public void btnClear() {
    //CrackerGUI gui = new CrackerGUI();
    gui.setPlainText(" ");
    System.out.println("testing");
  } 
}

You also should probably not be creating new Shift objects in your GUI's actionPerformed methods, but rather use only one Shift object that is a class field. 您也可能不应该在GUI的actionPerformed方法中创建新的Shift对象,而应该仅使用一个作为类字段的Shift对象。

The btnClear method clears the text area of a new CrackerGUI instance. btnClear方法清除新的CrackerGUI实例的文本区域。 It's like if you wanted to clear a drawing on a sheet of paper by taking a new blank sheet and clearing it. 这就像您要通过取出新的空白纸并清除它来清除一张纸上的图形。 The original sheet of paper will keep its drawing. 原始纸张将保留其图纸。

You need to pass the gui instance to your Shift: 您需要将gui实例传递给Shift:

public class Shift { 
    private CrackerGUI gui;

    public Shift(CrackerGUI gui) {
        this.gui = gui;
    }

    public void btnClear() {
        this.gui.setPlainText(" ");
    }
}

and in the CrackerGUI class : 在CrackerGUI类中:

private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Shift classShift = new Shift(this);
    classShift.btnClear(); 
}   

Assuming CrackerGUI is your GUI, you should have the following instead: 假设CrackerGUI是您的GUI,则应改为以下内容:

public class CrackerGUI {

  public void setPlainTextBox(String text)
  {
    plainTextBox.setText(text);
  }
  public void btnClear()
  {
    setPlainTextBox("");
    System.out.println("testing");
  } 
}

One last thing, never make your GUI elements public! 最后一件事,永远不要公开您的GUI元素! You should ask the GUI to clear itself and leave that knowledge of clearing elements hidden inside it. 您应该要求GUI清除自身,并将清除元素的知识隐藏在其中。

You could try using static methods, as you would end up creating a new gui, then displaying that one, in stead of the current one already displayed. 您可以尝试使用静态方法,因为最终将创建一个新的gui,然后显示该gui,而不是已经显示的当前gui。

This would require the parent class to be static too, which may cause errors in some of your methods, just a heads up. 这也将要求父类也是静态的,这可能会导致某些方法出错,请注意。

Or else, you could create your own setText method: 否则,您可以创建自己的setText方法:

void setText(JTextField t, String s){
  t.setText(s);
}

that may enable you to directly edit components in the current GUI. 这可能使您可以直接在当前GUI中编辑组件。

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

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