vendredi 6 mars 2015

how to print setText() on multiple lines of a JLabel



When I print out the raw text from a website, it will only put one line of text in the JLabel, but in the console it will do multiple lines each on their own line.



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://ift.tt/1iKUgFd").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);
}


}


As a sample it should print out multiple school closings like this:


Washington Yu Ying Public Charter School Closed


Whitman-Walker Health Open at 10am


Woodyard Road Nursery Open at 10am


but on the JLabel all it shows is 1 line that isn't even a closing it is just in the same html tag as the others. So how do I make indents?




Aucun commentaire:

Enregistrer un commentaire