我正在寻找一种方法来创建一个窗口(使用 Jpanel 或其他),从网站上获取一定量的文本(这部分正在工作),并将其显示在文本字段中。我知道这看起来相当简单,但我对此很陌生。谢谢!到目前为止,我从 Jsoup 网站获得了这个示例:
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());
}
}}
最佳答案
假设你的jsoup工作正常,并且你的foreach循环打印出你需要的标签......我已经编写了一些代码,使用Java swing将数组列表的内容打印到TextArea。我做的唯一与你不同的是使用字符串而不是元素(因为我没有下载 jsoup 库。
类 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);
}
}
类 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);
}
}
如果您有任何问题或希望我进一步阐述,请告诉我。
关于java - java中如何将文本输出到窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28843822/