简体   繁体   English

textArea.setText(“”)不会清除JTextArea中的文本

[英]textArea.setText(“”) doesn't clear the text in a JTextArea

I am trying to clear the text in a JTextArea, and looking at other questions, it seems like calling textArea.setText(""/null) will clear the text area. 我试图清除JTextArea中的文本,并查看其他问题,似乎调用textArea.setText(“” / null)会清除文本区域。 This does not seem to be happening with my code, and it appends the new text to the text already in the area. 我的代码似乎没有发生这种情况,它会将新文本追加到该区域中已有的文本中。 Can anyone see something wrong in my code? 有人可以在我的代码中看到错误吗?

public class morseJFrame extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
public JTextPane textPane = new JTextPane();
public JTextArea textArea = new JTextArea();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                morseJFrame frame = new morseJFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public morseJFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 508);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    textPane.setBounds(5, 5, 424, 194);
    textPane.setText("Enter your alphanumberic text here to translate.");
    contentPane.add(textPane);

    JButton btnTranslate = new JButton("Translate");
    btnTranslate.setBounds(5, 419, 213, 41);
    btnTranslate.addActionListener(this);
    add(btnTranslate);
    contentPane.add(btnTranslate);

    textArea.setBounds(5, 210, 424, 203);
    contentPane.add(textArea);

    JButton btnPlaySound = new JButton("Play Morse Sound");
    btnPlaySound.setBounds(228, 419, 201, 41);
    contentPane.add(btnPlaySound);
}

public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if (command.equals("Translate")) {
        String text = textPane.getText();
        String translatedText = MorseTranslate.doMorse(text);
        textArea.setText("");
        textArea.setText(translatedText);
    }
}

} }

This does not seem to be happening with my code, and it appends the new text to the text already in the area 我的代码似乎没有发生这种情况,它会将新文本追加到该区域中已有的文本中

So based on this code... 所以根据这段代码...

String text = textPane.getText();
String translatedText = MorseTranslate.doMorse(text);
textArea.setText("");
textArea.setText(translatedText);

I would suggest that the problem is with your MorseTranslate.doMorse which is probably returning the text appended to itself 我建议问题出在您的MorseTranslate.doMorse ,这可能是返回附加到其自身的文本

But, as you can see, this is a matter of "guess work" as we don't have the complete code to go by. 但是,正如您所看到的,这是“猜测工作”的问题,因为我们没有完整的代码。

Consider providing a runnable example which demonstrates your problem. 考虑提供一个可运行的示例来演示您的问题。 This is not a code dump, but an example of what you are doing which highlights the problem you are having. 这不是代码转储,而是您正在执行的操作的一个示例,突出显示了您所遇到的问题。 This will result in less confusion and better responses 这将减少混乱并改善响应

Try to reverse the order like this: 尝试像这样颠倒顺序:

   textArea.setText(translatedText);
   textArea.setText("");

Use either textArea.setText(null) or textArea.setText("") are the same thing. 使用textArea.setText(null)textArea.setText("")是同一回事。

我认为setText()会替换内容(而不是附加内容),因此您无需先执行setText(“”)然后再执行setText(“您想要的文本”),最新的句子就足够了。

setText("") doesn't clear the text setText("")不会清除文本

Yes it does. 是的,它确实。

textArea.setText("");

Here you are clearing the text area. 在这里,您正在清除文本区域。

    textArea.setText(translatedText);

Here, in the very next line, you are setting it to something else. 在这里,在下一行中,您将其设置为其他内容。

You could alternatively try: 您也可以尝试:

textArea.setText(null);

See if that works. 看看是否可行。 But I agree with Wyatt, you are setting other text right after clearing it. 但是我同意怀亚特(Wyatt)的观点,您是在清除文字后立即设置其他文字。

您是否已插入一个简单的打印语句以查看textPane.getText()在将字符串文本发送到doMorse(String)之前实际上将其设置为什么?

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

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