用于多个标签的 JScrollPane

发布于 2025-01-07 17:09:24 字数 563 浏览 1 评论 0原文

我正在开发一个具有流程布局的程序,里面有一组标签,因为标签太多,所以它们不会全部显示。无论如何,是否要添加一个滚动窗格来水平滚动所有这些标签?

JPanel mainpanel = new JPanel();
     mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
        pane.add(mainpanel, BorderLayout.NORTH);

        JPanel rightpanel = new JPanel();
        rightpanel.setLayout(new FlowLayout());
        for (int i = 0; i < 100; i++)
        {
            rightpanel.add(new JLabel("Label " + i));
        }
        mainpanel.add(new JLabel("Left label"));
        mainpanel.add(new JScrollPane(rightpanel));

I am working on a program that has a flow layout, inside are a set of labels and because there are so many they do not all display. Is there anyway to add a scroll pane to scroll through all of these labels horizontally?

JPanel mainpanel = new JPanel();
     mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
        pane.add(mainpanel, BorderLayout.NORTH);

        JPanel rightpanel = new JPanel();
        rightpanel.setLayout(new FlowLayout());
        for (int i = 0; i < 100; i++)
        {
            rightpanel.add(new JLabel("Label " + i));
        }
        mainpanel.add(new JLabel("Left label"));
        mainpanel.add(new JScrollPane(rightpanel));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花似海 2025-01-14 17:09:24

我建议不要使用 JListJTable 具有一个 ColumnRow(取决于或方向),JListJTable中的对象默认为JLabel/JComponent

I'd suggest ot use JList or JTable with one Column or Row (depends or direction), Object in the JList or JTable is JLabel/JComponent by default

把昨日还给我 2025-01-14 17:09:24

不确定您的问题到底是什么,因为您已经知道需要使用 JScrollPane。怎么样:

public class ScrollLabels
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Labels");

        JPanel mainpanel = new JPanel();
        mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
        frame.add(mainpanel);

        JPanel rightpanel = new JPanel();
        rightpanel.setLayout(new FlowLayout());
        for (int i = 0; i < 100; i++)
        {
            rightpanel.add(new JLabel("Label " + i));
        }
        mainpanel.add(new JLabel("Left label"));
        mainpanel.add(new JScrollPane(rightpanel));
        frame.setSize(500, 100);
        frame.setVisible(true);
    }
}

Not sure what your question really is, since you already know you need to use a JScrollPane. How about:

public class ScrollLabels
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Labels");

        JPanel mainpanel = new JPanel();
        mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
        frame.add(mainpanel);

        JPanel rightpanel = new JPanel();
        rightpanel.setLayout(new FlowLayout());
        for (int i = 0; i < 100; i++)
        {
            rightpanel.add(new JLabel("Label " + i));
        }
        mainpanel.add(new JLabel("Left label"));
        mainpanel.add(new JScrollPane(rightpanel));
        frame.setSize(500, 100);
        frame.setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文