尺寸比Scrollpane大的JPanel

时间:2013-02-04 08:33:26

标签: java swing jpanel jscrollpane preferredsize

我已经实现了一个对话框,我在JPanel中添加了一些复选框,其中还有一个滚动条。每次运行我的程序时,复选框的数量都不固定,但在创建对话框之前我就知道了。

不幸的是,当我有大量的复选框添加对话框时,如下所示:

enter image description here

我的代码是:

        JPanel listPanel = new JPanel(new GridLayout(files.size() + 2, 1));

        for (int i = 0; i < files.size(); i++) {
            listPanel.add(files.get(i));
        }

        listPanel.setPreferredSize(new Dimension(600, 1400));
        JScrollPane sp =
                new JScrollPane(listPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp.setPreferredSize(new Dimension(300, 200));

        mainPanel.add(sp, BorderLayout.CENTER);
        mainPanel.add(buttonPane, BorderLayout.EAST);

        getContentPane().setLayout(new FlowLayout());

        getContentPane().add(mainPanel);

        pack();
        setResizable(false);

如何在不影响框架的情况下将Scrollpane内的面板大小设置为更大的尺寸?

2 个答案:

答案 0 :(得分:3)

避免使用setPreferredSize,让布局经理决定他们实际需要的尺寸......

更新了示例

滚动窗格的JViewport使用listPane的首选大小来确定它需要多少可见空间,这很多......

为了克服这个问题,您需要为视图端口提供更好的提示,以了解组件实际可能具有的可视大小。该组件的preferredSize无法在此处运行,您需要从preferredScrollableViewportSize界面提供Scrollable

enter image description here

public class BadList {

    public static void main(String[] args) {
        new BadList();
    }

    public BadList() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            List<String> files = new ArrayList<>(2000);
            for (int index = 0; index < 2000; index++) {
                files.add(Integer.toString(index));
            }

            ListPane listPanel = new ListPane();
            listPanel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            for (String file : files) {
                listPanel.add(new JCheckBox(file), gbc);
            }

            JScrollPane sp = new JScrollPane(listPanel);

            JPanel mainPanel = new JPanel(new BorderLayout());
            JPanel buttonPane = new JPanel(new GridBagLayout());
            buttonPane.add(new JButton("Select All"), gbc);
            buttonPane.add(new JButton("Deselect All"), gbc);
            gbc.weighty = 1;
            buttonPane.add(new JPanel(), gbc);

            mainPanel.add(sp, BorderLayout.CENTER);
            mainPanel.add(buttonPane, BorderLayout.EAST);

            setLayout(new BorderLayout());

            add(mainPanel);
        }
    }

    public class ListPane extends JPanel implements Scrollable {

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(200, 200);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 128;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 128;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return false;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }

    }
}

nb-在这个例子中我使用了一些任意的魔术数字,你将会为你的需要提供更实际的价值

答案 1 :(得分:3)

  • JPanel已实施FlowLayout

  • FlowLayout仅接受PreferredSize,然后JScrollPane正确放置

  • 使用JTable(放置Boolean value,代表JCheckBox),而不是将子项添加到JPanel(非自然滚动,必须更改scroll increment })

  • listPanel.setPreferredSize(new Dimension(600, 1400));不是很好Dimmension,请注释此代码行,让pack()

  • 此作业(此处发布的代码)我会直接将JScrollPanebuttonPane添加到JFrame,而不是使用mainPanel(),否则发布SSCCE