将组件粘贴到JScrollPane上

将组件粘贴到JScrollPane上

本文介绍了将组件粘贴到JScrollPane上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,可以将一堆组件添加到JPanel(在JScrollbar中).但是,由于它添加了太多的组件,因此大多数组件都不适合可见区域(Viewport).

I have a program that adds bunch of components to a JPanel (in JScrollbar). However, since it adds so many components, most of them don't fit into the visible area (Viewport).

当所有内容加载完毕并且开始向下滚动时,我注意到当组件进入Viewport区域时,它们正在对齐并设置其位置.这导致我的JScrollPane高于必要水平.到最后时,它就捕捉"了(组件突然向上移动(正确对齐),视口也是如此).

When everything loads and I start to scroll down, I notice that components, as they get into the Viewport area, are aligning and setting their positions. That causes my JScrollPane to be higher than necessary. That makes it "snap" when I get to the end (components abruptly move up (align properly), and so does the viewport).

我尝试调用repaint()validate(),但没有任何效果.我在做什么错了?

I tried calling repaint() and validate(), but with no effect whatsoever. What am I doing wrong?

推荐答案

我建议按顺序发布 SSCCE 准确地复制您的特定问题.

I would suggest posting an SSCCE in order to exactly replicate your specific problem.

我做了一个简短的例子,可以引导您朝正确的方向前进.

I did a short example that may lead you in the right direction.

基本上,只需将GridLayout加225个JButtonJPanel中,然后将GridLayout加到JScrollPane中.

Basically will just add 225 JButtons to JPanel with GridLayout which in turn is added to JScrollPane.

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JScrollPaneOfComponents {

    /**
     * Default constructor for ScrollBarOfComponents.class
     */
    public JScrollPaneOfComponents() {
        initComponents();
    }

    /**
     * Initialize GUI and components (including ActionListeners etc)
     */
    private void initComponents() {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new GridLayout(15, 15));

        //create 225 JButtons and add them to JPanel;
        for (int i = 0; i < (15*15); i++) {
            panel.add(new JButton(String.valueOf((i + 1))) {
                //make buttons bigger for demonstartion purposes
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(100, 100);
                }
            });
        }

        JScrollPane scrollpane = new JScrollPane(panel) {
            //size the JScrollPane purposelfully smaller than all components
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 300);
            }
        };

        //add scrollpane to frame
        jFrame.add(scrollpane);

        //pack frame (size JFrame to match preferred sizes of added components and set visible
        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {

        /**
         * Create GUI and components on Event-Dispatch-Thread
         */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    //set nimbus look and feel
                    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
                    e.printStackTrace();
                }
                //create new instance of GUI
                JScrollPaneOfComponents test = new JScrollPaneOfComponents();
            }
        });
    }
}

这篇关于将组件粘贴到JScrollPane上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:37