如何在滚动窗格上放置多个标签,为什么这个标签放在中心? [英] How can I place multiple labels on a scrollpane and why does this label get placed in the center?

查看:101
本文介绍了如何在滚动窗格上放置多个标签,为什么这个标签放在中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 feed-box ,它显示从服务器到客户端的所有更新。在 Jframe 我放置了一个 JScrollPane ,以便客户可以轻松看到更多的Feed。

I am trying to make a feed-box that displays all the updates from the server to the client.On a Jframe I placed a JScrollPane, so that more number of feeds can be easily seen by the client.

超过 JScrollPane ,我试图放置一个 JLabel 然后它看起来像这样:

Over the JScrollPane, I tried to place a JLabel and then it looks like this :

标签获取放置在中心,如果我尝试放置一个新的 JLabel ,虽然IDE的导航窗口显示,它已被放置,我看不到它。附上的 JFrame 默认设置为免费设计。我尝试更改为流程布局,但这没有帮助。

The label gets placed in the center and if I try to place a new JLabel, though navigator window of the IDE shows, it has been placed, I cannot see it.Layout of the enclosing JFrame has been set to free design by default. I tried changing to flow layout but that doesn't help.

为什么 JLabel 放在窗户的中央?这是显示从服务器到客户端的订阅源的正确方法吗?

Why does the JLabel get placed in the center of the window ? Is this the correct way of showing feeds from the server to the client ?

推荐答案

讨论了此处将RSS源中的条目添加到a JTextPane 。我在下面说明了一个变体。

The complete example discussed here adds entries from an RSS feed to a JTextPane. I've illustrated a variation below.

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @see https://stackoverflow.com/a/19164649/230513
 * @see https://stackoverflow.com/q/10461087/230513
 */
public class NewsFeed extends JPanel {
    public NewsFeed() {
        this.setLayout(new GridLayout());
        JTextPane newsPane = new JTextPane();
        newsPane.setContentType("text/html");
        newsPane.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(newsPane);
        scrollPane.setVerticalScrollBarPolicy(
            javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        RSS reader = RSS.getInstance();
        String rssNews = reader.writeNews();
        newsPane.setText(rssNews);
        this.add(scrollPane);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("News");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new NewsFeed());
                f.pack();
                f.setSize(640, 480);
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }

    private static class RSS {

        private static final String NAME =
            "http://thestar.com.my.feedsportal.com/c/33048/f/534600/index.rss";
        private static RSS instance = null;

        public static RSS getInstance() {
            if (instance == null) {
                instance = new RSS();
            }
            return instance;
        }

        public String writeNews() {
            String result = "";
            try {
                DocumentBuilder builder = DocumentBuilderFactory
                    .newInstance().newDocumentBuilder();
                URL u = new URL(NAME);
                Document doc = builder.parse(u.openStream());
                NodeList nodes = doc.getElementsByTagName("item");
                for (int i = 0; i < nodes.getLength(); i++) {
                    Element element = (Element) nodes.item(i);
                    result += "Title: " + getElementValue(element, "title") + "<br>";
                    result += "Link: " + getElementValue(element, "link") + "<br>";
                    result += "Publish Date: " + getElementValue(element, "pubDate") + "<br>";
                    result += "Description: " + getElementValue(element, "description") + "<br>";
                }
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
            return result;
        }

        private String getCharacterDataFromElement(Element e) {
            try {
                Node child = e.getFirstChild();

                if (child instanceof CharacterData) {
                    CharacterData cd = (CharacterData) child;
                    return cd.getData();
                }
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
            return "";
        }

        protected float getFloat(String value) {
            if (value != null && !value.equals("")) {
                return Float.parseFloat(value);
            }
            return 0;
        }

        protected String getElementValue(Element parent, String label) {
            return getCharacterDataFromElement(
                (Element) parent.getElementsByTagName(label).item(0));
        }
    }
}

这篇关于如何在滚动窗格上放置多个标签,为什么这个标签放在中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆