简体   繁体   中英

How to set data in JTextArea in Java?

Good Evening, I going to take the output of a program and show it in a simple Text Area GUI. I try to follow certain tutorials but the output still show on the NetBean console but not at the GUI.

public class PingAndTransmit extends JPanel{//This is the First brace({)

JTextArea audioData = new JTextArea();
JTextArea pingData = new JTextArea();
String audioLine;
String pingLine;



public PingAndTransmit() {

   super(new BorderLayout());

    while(...) {
         if(...) {
            audioLine = "\nI am handsome";
            pingLine  = "\nShe is pretty";
         } else {
             audioLine = "\nI am not handsome";
             pingLine  = "\nShe not pretty";
         }
     }

    audioData.append(audioLine);
    pingData.append(pingLine);

    JPanel controls2 = new JPanel(new GridLayout(1, 2));
    controls2.add(audioData);
    controls2.add(pingData);
    add(controls2, BorderLayout.CENTER);
}//this is not the last brace (})

For the GUI

public static void createAndShowGui() {
    JFrame frame = new JFrame("Sender");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new PingAndTransmit());
    frame.setSize(400, 200);
    frame.setVisible(true);
    frame.toFront();
}

Main

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                        createAndShowGui();
                }
        });

}

}//Here is the last brace (})

How can I display the output on the Text Area GUI? Need some hints and guidelines, thanks^^"

what is PingAndTransmit()? does it extend a JComponent?

You need to add the textarea at some point in your frame.

Your createAndShowGui() method only creates a frame and tries to add that "PingAndTransmit()" object, but it looks like that object isn't really a Swing component.

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