0

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

当所有内容加载完毕并且我开始向下滚动时,我注意到组件在进入该Viewport区域时正在对齐并设置它们的位置。这导致我JScrollPane高于必要的水平。这使它在我到达终点时“突然”(组件突然向上移动(正确对齐),视口也是如此也是如此)。

我尝试调用repaint()and validate(),但没有任何效果。我究竟做错了什么?

4

1 回答 1

7

我建议发布SSCCE以准确复制您的特定问题。

我做了一个简短的例子,可能会引导你走向正确的方向。

基本上只会添加 225JButtonJPanelGridLayout然后添加到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();
            }
        });
    }
}
于 2012-11-05T18:14:51.490 回答