java 当框架太小而无法显示所有 JList 时,如何让滚动条出现在 JList 上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15980035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:32:19  来源:igfitidea点击:

How to get scrollbar to appear on a JList when the frame is too small to show all of the JList?

javaswingjscrollpanejlist

提问by idungotnosn

I want to make a scrollbar appear on a JList whenever the frame is resized to be too small for the List itself. So far, this is the code I have. Run it, resize the frame, and notice how no scrollbar ever appears on the JList.

我想让一个滚动条出现在 JList 上,只要框架被调整到对于 List 本身来说太小了。到目前为止,这是我拥有的代码。运行它,调整框架的大小,并注意 JList 上从未出现滚动条。

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class JListScroll extends JPanel{

JScrollPane listScrollPane;

public JListScroll() {
    String[] stringArray = {"Testing","This","Stuff"};
    JList<String> rowList = new JList<String>(stringArray);
    listScrollPane = new JScrollPane();
    listScrollPane.getViewport().setView(rowList);
    this.setSize(new Dimension(75,200));
    this.add(listScrollPane);
    this.doLayout();
}

public static void main(String[] args){
    JFrame frame = new JFrame();
    JListScroll scrollPanel = new JListScroll();
    frame.add(scrollPanel);
    frame.setVisible(true);
    frame.setSize(new Dimension(75,300));
}

}

Notice how there is a JScrollPane added, but no scroll bar appears on the list when the window gets really small. This is what I want to fix.

请注意添加了 JScrollPane 的方式,但是当窗口变得非常小时,列表中不会出现滚动条。这就是我想要修复的。

Thanks in advance!

提前致谢!

回答by mKorbel

  • JPanel has implemented FlowLayout

  • FlowLayout accepting only PreferredSize (in your case hardcoded by setSize)

  • FlowLayout isn't designated, not implemented any resize JComponents (layed by FlowLayout) together with container, JComponent isn't resizable, stays as it is

  • don't want to commenting something about your code posted here, see differencies, quite good low level

  • JPanel 已经实现了 FlowLayout

  • FlowLayout 仅接受 PreferredSize(在您的情况下由 setSize 硬编码)

  • FlowLayout 未指定,未与容器一起实现任何调整大小的 JComponents(由 FlowLayout 放置),JComponent 不可调整大小,保持原样

  • 不想评论有关您在此处发布的代码的内容,请查看差异,相当不错的低级

enter image description here

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.Dimension;    
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class JListScroll {

    private JFrame frame = new JFrame();
    private JPanel panel = new JPanel();
    private JScrollPane listScrollPane = new JScrollPane();
    private String[] stringArray = {"Testing", "This", "Stuff"};
    private JList rowList = new JList(stringArray);

    public JListScroll() {
        rowList.setVisibleRowCount(2);
        listScrollPane.setViewportView(rowList);
        panel.setLayout(new BorderLayout());
        panel.add(listScrollPane);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // EDIT 
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JListScroll jListScroll = new JListScroll();
            }
        });
    }
}

回答by ddmps

You can force the visibility of the (vertical) scroll bar with the method listScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS)

您可以使用该方法强制(垂直)滚动条的可见性 listScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS)