简体   繁体   中英

Append() doesn't work on JTextArea element

I want to create graphic console which will give user information about current state of program. I've planned to use for it JTextArea, but I have a problem with append() method. Even after using it in main class I still have empty JTextArea. What am I doing wrong?

Here is the code of gui of console:

package com.meh;

import javax.swing.*;

public class Controller extends JFrame {
    public JPanel ControlPanel;
    public JTextArea Log;

    static void setView() {
        JFrame frame = new JFrame("Controller");
        frame.setContentPane(new Controller().ControlPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

And this is the code of main class:

package com.meh;

public class Main {
    public static void main(String[] args) {
    Controller controller = new Controller();
        controller.setView();
        controller.Log.append("Hello");
    }
}

If you look up the jTextArea append method, you will see that:

Appends the given text to the end of the document

However, it doesn't do anything if the String is empty or null .

You could use setText() in your case.

If you call getText() is the new string value returned? If so, you might need to call repaint() and/or revalidate() on controller and/or controller.Log after changing the text.

如我所见,您永远不会初始化'ControlPanel',它将始终为null,因此您无法对其进行任何操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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