简体   繁体   English

JPanel无法在JScrollPane中滚动

[英]JPanel not scrolling in JScrollPane

I have a JPanel that I create like so 我有一个这样创建的JPanel

    JPanel statsKeysPanel = new JPanel(new MigLayout("insets 0", "[]", ""));

and populate with a dynamic number of JLabels stacked on top of each other. 并以相互堆叠的动态数量的JLabel进行填充。 For the sake of an example: 为了举例:

    for(int i = 0; i < 30; i++) {
        statsKeysPanel.add(new JLabel("" + i + " key value"), "wrap");
    }

I then create and add the scroller like so 然后,我像这样创建并添加滚动条

    JPanel panel = new JPanel(new MigLayout("insets 0", "[center][][center][]", "[][]"));
    final JScrollPane keysScroller = new JScrollPane(this.statsKeysPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    keysScroller.setMaximumSize(new Dimension(100, 300));
    panel.add(keysScroller, "cell 0 1");

The max of 300 is applied but the 15 out of 30 JLabels that don't fit in 300px are hidden, and scrolling doesn't work. 最多可应用300个,但不适合300px的30个JLabel中有15个被隐藏,并且滚动不起作用。 What am I doing wrong? 我究竟做错了什么? (image below) (下图)

在此处输入图片说明

final JScrollPane keysScroller = new JScrollPane(this.statsKeysPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

Why are you using NEVER for both the horizontal and vertical scrollbar? 为什么在水平和垂直滚动条上都NEVER使用NEVER I would think this would prevent a scrollbar from appearing. 我认为这将阻止滚动条出现。

I generally don't set either property and just let the scrollpane determine when to display the scrollbar. 我通常不设置任何属性,而只是让滚动窗格确定何时显示滚动条。 Sometimes I use ALWAYS to reserve space for the scrollbar. 有时我ALWAYS使用滚动条来保留空间。

You are using unnecessarily two panels; 您不必要使用了两个面板。 one will suffice. 一个就足够了。 I think that you have omitted the code that caused your error. 我认为您已经省略了导致错误的代码。

Here is a working example: 这是一个工作示例:

package com.zetcode;

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;

public class StatsKeyEx extends JFrame {

    public StatsKeyEx() {

        initUI();
    }

    private void initUI() {

        JPanel pnl = new JPanel(new MigLayout());

        for (int i = 0; i < 60; i++) {
            pnl.add(new JLabel("" + i + " key value"), "wrap");
        }

        JScrollPane spane = new JScrollPane(pnl);
        spane.setPreferredSize(new Dimension(150, 200));
        add(spane);

        pack();

        setTitle("Scrolling");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                StatsKeyEx ex = new StatsKeyEx();
                ex.setVisible(true);
            }
        });
    }
}

The scrollbars are shown as needed. 根据需要显示滚动条。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM