gpt4 book ai didi

java - 如何在 JLabel 的多行上打印 setText()?

转载 作者:行者123 更新时间:2023-12-01 18:17:09 24 4
gpt4 key购买 nike

当我从网站打印原始文本时,它只会在 JLabel 中放入一行文本,但在控制台中,它会在自己的行上打印多行文本。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class closinggui extends JFrame {

private JPanel contentPane;
JLabel label;
JButton button;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
closinggui frame = new closinggui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void code() throws IOException
{
Document document = Jsoup.connect("http://www.nbcwashington.com/weather/school-closings/").get();
Elements tags = document.select("p");

for (Element tag : tags) {
System.out.println(tag.text());
label.setText(tag.text());
}
}
public closinggui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 449, 524);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

label = new JLabel("");
label.setBounds(10, 45, 414, 440);
contentPane.add(label);

button = new JButton("get closings");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
code();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
button.setBounds(164, 11, 89, 23);
contentPane.add(button);
}
}

作为示例,它应该打印出多个学校关闭的信息,如下所示:

Washington Yu Ying Public Charter School Closed
Whitman-Walker Health Open at 10am
Woodyard Road Nursery Open at 10am

但是在标签上它显示的只是 1 行,甚至不是结束语,它只是与其他标签位于相同的 HTML 标签中。那么如何缩进呢?

最佳答案

尝试使用 html 标签:

String txt = "<html>";
for (Element tag : tags) {
txt += tag.text() + "<br/>";
}
txt += "</html>";
label.setText(txt);

关于java - 如何在 JLabel 的多行上打印 setText()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28910200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com