Swing 桌面 Java 应用程序滚动 [英] Swing desktop java application scroll

查看:67
本文介绍了Swing 桌面 Java 应用程序滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swing Java 应用程序的新手,我正在开发一个小型桌面应用程序,每次单击按钮都会在运行时创建 jlabels.

I am new in swing java application and I am developing a small desktop application that creates jlabels at run time with every button click.

问题是当jlabels数量太多时,我需要在jframe中显示一个滚动条,让用户能够看到所有的jlabels(jlabel是在jframe的高度之后创建的).

The problem is when the jlabels number is too much, I need to display a scroll in the jframe to make the user able to see all jlabels (jlabel created after the height of the jframe).

我尝试了 jscroll 窗格和 jscroll bar 但没有用,请提供一个完整的例子.

I tried jscroll pane and jscroll bar but with no use , please provide a complete example.

预先感谢您的努力.

推荐答案

似乎对我有用,请发布 SSCCE要显示特定问题,唯一想到的是您将 JLabel 添加到容器而不是 JScrollPane

Seems to work for me, please post SSCCE to show specific problems, the only thing that springs to mind is you added you JLabel to the container and not the JScrollPane

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class JLabelJScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame() {
                    @Override
                    public Dimension getPreferredSize() {//size frame purposefully smaller
                        return new Dimension(100, 100);
                    }
                };
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JLabel label = new JLabel("WWWWWWWWWoooooorrrrkkkk");
                JScrollPane jsp = new JScrollPane(label);

                frame.add(jsp);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

更新

我看到你说:

(在 jframe 的高度之后创建的 jlabel).

(jlabel created after the height of the jframe).

所以我认为 JFrame 是可见的?如果是这样:

So i take it the JFrame is visible? if so:

这是一个例子,显示了如何在显示 JFrame 2 秒后将 JLabel 添加到 JScrollPane,最重要的是调用 revalidate()repaint() 添加/删除/更改组件大小后的容器实例.

Here is an example which shows how to add JLabel to JScrollPane after 2 seconds of showing JFrame, most important is to call revalidate() and repaint() on the containers instance after adding/removing/changing component sizes.

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class JLabelJScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(100, 100);
                    }
                };
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JScrollPane jsp = new JScrollPane();
                frame.add(jsp);
                frame.pack();
                frame.setVisible(true);

               Timer t= new Timer(2000, new AbstractAction() {//create timer to add JLabel to scrollPane after 2 seconds
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        final JLabel label = new JLabel("WWWWWWWWWoooooorrrrkkkk");
                        jsp.setViewportView(label);
                        //refelect changes
                        frame.revalidate();
                        frame.repaint();
                    }
                });
                t.setRepeats(false);
                t.start();
            }
        });
    }
}

UPADTE 2

根据评论,它是完全相同的,只是稍有改动:

as per comment its the exact same thing with minor alterations:

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class JLabelJScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(100, 100);
                    }
                };
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JPanel labelPanel = new JPanel(new GridLayout(4, 1));

                final JScrollPane jsp = new JScrollPane(labelPanel);
                frame.add(jsp);
                frame.pack();
                frame.setVisible(true);

                Timer t = new Timer(2000, new AbstractAction() {//create timer to add JLabel to scrollPane after 2 seconds
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        final JLabel label = new JLabel("WWWWWWWWWoooooorrrrkkkk");
                        final JLabel label2 = new JLabel("Try it");
                        final JLabel label3 = new JLabel("Noooooo reealllly");
                        final JLabel label4 = new JLabel("Yes");
                        labelPanel.add(label);
                        labelPanel.add(label2);
                        labelPanel.add(label3);
                        labelPanel.add(label4);
                        labelPanel.revalidate();
                        labelPanel.repaint();
                    }
                });
                t.setRepeats(false);
                t.start();
            }
        });
    }
}

这篇关于Swing 桌面 Java 应用程序滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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