Swing - GridLayout组件未填满所有可用空间

时间:2017-02-05 16:33:53

标签: java swing jscrollpane layout-manager grid-layout

我已经完成了对StackOverflow的搜索,但所有问题似乎都与我的问题完全相反。

我正在编写代码,动态地将JLabels添加到JPanel GridLayoutJScrollPane全部包含在private JFrame frame; private JPanel panel; static Test window; private JScrollPane scrollPane; public static void main(final String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { window = new Test(); window.frame.setVisible(true); } catch (final Exception e) { e.printStackTrace(); } } }); for (int i = 0; i < 100; i++) { try { EventQueue.invokeAndWait (new Runnable() { @Override public void run() { final JLabel label = new JLabel("Test"); label.setSize(160, 40); label.setHorizontalAlignment(SwingConstants.CENTER); // Finalise GUI window.panel.add(label); window.panel.revalidate(); window.panel.repaint(); try { Thread.sleep(100); } catch (final Exception e) { e.printStackTrace(); } } }); } catch (final Exception e) { e.printStackTrace(); } } } public Test() { initialize(); } private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 500, 200); frame.getContentPane().setLayout(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new JPanel(); panel.setLayout(new GridLayout(0, 3)); final JPanel outerPanel = new JPanel(); outerPanel.setLayout(new FlowLayout()); outerPanel.add(panel); scrollPane = new JScrollPane(outerPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setBounds(12, 0, 460, 160); frame.getContentPane().add(scrollPane); } 中。这是一个SSCCE:

GridLayouts

根据我的理解,在JLabels中,每个组件都占用其单元格中的所有可用空间。“然而,JPanel实际上并没有占用GridLayout中的所有可用空间。

我不确定我的错误在哪里。它是Components还是周围的GridLayout没有给android-studio\bin足够的空间?

谢谢大家。

1 个答案:

答案 0 :(得分:3)

您的JLabels 占用所有可用空间。你的JPanel很小。用边框自己测试一下:

    panel = new JPanel();
    panel.setLayout(new GridLayout(0, 3));

    // **** add this to see ****
    panel.setBorder(BorderFactory.createLineBorder(Color.BLUE));

如果您希望标签填写顶部,则为外部面板使用不同的布局。请注意标有// !!评论的更改:

    final JPanel outerPanel = new JPanel();
    // !! outerPanel.setLayout(new FlowLayout());
    outerPanel.setLayout(new BorderLayout()); // !!
    // !! outerPanel.add(panel);
    outerPanel.add(panel, BorderLayout.PAGE_START); // !!

此外,Thread.sleep(...)是Swing GUI中的危险代码。如果您想延迟添加组件,请为作业使用最佳的Swing工具:Swing Timer。例如

    final int timerDelay = 100;
    final int maxLabelCount = 100; 
    new Timer(timerDelay, new ActionListener() {
        private int labelCount = 0;
        @Override
        public void actionPerformed(ActionEvent evt) {
            if (labelCount < maxLabelCount) {
                final JLabel label = new JLabel("Test");
                // !! label.setSize(160, 40); // NO!
                label.setHorizontalAlignment(SwingConstants.CENTER);
                // Finalise GUI
                window.panel.add(label);
                window.panel.revalidate();
                window.panel.repaint();
            } else {
                // stop this timer
                ((Timer) evt.getSource()).stop();
            }
            labelCount++;
        }
    }).start();