简体   繁体   中英

How to output text into a window in java

I am looking for a way to create a window (with Jpanel or whatever), get a certain amount of text off of a website (this part is working), and display it in a text field. I know this seems fairly simple but i am new at this. Thanks! So far I got this example from the Jsoup website:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class closings {

public static void main(String[] args) throws IOException {

    Document document = Jsoup.connect("http://www.wjla.com/weather/virginia-school-closings-delays/").get();
    Elements tags = document.select("#closingsList");

    for (Element tag : tags) {
        System.out.println(tag.text());
    }
}}

Assuming that your jsoup works and that your for each loop prints out the tags that you need.... I have written up some code that prints the contents of an arraylist to a TextArea using Java swing. The only thing that I did different from you is use Strings instead of Elements (because I dont have the jsoup library downloaded.

class stackExchangeHelp

package stackExchangeHelp;

import java.util.ArrayList;

public class stackExchangeHelp
{
    public static void main(String[] args)
    {
        //this should be a list of elements (not strings)
        ArrayList<String> listToSend = new ArrayList<String>();

        //use your for each loop to add elements to the list
        listToSend.add("First element");
        listToSend.add("Second Element");
        listToSend.add("Third Element");

            DisplayGuiHelp gui = new DisplayGuiHelp(listToSend);
    }
}

class DisplayGuiHelp

package stackExchangeHelp;

import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class DisplayGuiHelp
{
    public DisplayGuiHelp(ArrayList<String> list) //constructor of the DisplayGuiHelp object that has the list passed to it on creation
    {
        final JFrame theFrame = new JFrame();
        theFrame.setTitle("Stack exchange help");
        theFrame.setSize(500, 500);
        theFrame.setLocation(550, 400);

        JPanel mainPanel = new JPanel();

        JTextArea theText = new JTextArea(5,25); //create the text area

        for(String text : list)
        {
            theText.append(text + "\n"); //append the contents of the array list to the text area

        }
        mainPanel.add(theText); //add the text area to the panel

        theFrame.getContentPane().add(mainPanel); //add the panel to the frame
        theFrame.pack();
        theFrame.setVisible(true);

    }
}

Let me know if you have any questions or would like me to expand upon it more.

You can create a JLabel

Step 1: Add this line to your code:

import javax.swing.*;

Step 2: Add this code:

JFrame f = new JFrame();
JPanel p = new JPanel();
JLabel l = new JLabel(tag.text());

p.add(l);
f.add(p);
f.setVisible(true);

You're all set! Enjoy your code!

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