在Swing中将ScrollBar添加到JList [英] Adding a ScrollBar to JList in Swing

查看:115
本文介绍了在Swing中将ScrollBar添加到JList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Java处理基于Swing的桌面应用程序.我在下面遇到了这个问题.

I'm currently dealing with Swing based desktop application in Java. I have come across this problem below.

我一直试图将滚动条添加到ListArea(JList类型)中,但是我无法做到,尽管我打算这样做.这是代码片段.我正在MainFrame扩展JFrame中编写此代码.任何帮助,将不胜感激.谢谢...

I have been trying to add scrollbars into my ListArea (JList type) but I couldn't do so albeit so many things there were I intended. Here is the code snippet.. I'm writing this code in MainFrame extending JFrame. Any help would be appreciated. Thanks...

super(title)
Jpanel panel = new Panel()
panel(add)
panel.setlayout(null)

final JList<String> listArea = new JList<String>(labels);
    listArea.setBounds(50, 180, 700, 300);
    listArea.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listArea.setFont(new Font("Serif", Font.ITALIC, 14));
    listArea.setVisibleRowCount(-1);
    JScrollPane listScroller = new JScrollPane();
    listScroller.setViewportView(listArea);
    listArea.setLayoutOrientation(JList.VERTICAL);
    panel.add(listArea); 
    panel.add(listScroller);

推荐答案

摆脱panel.add(listArea);,它将从JScrollPane

避免使用null布局,像素完美布局是现代ui设计中的一种幻觉.有太多因素会影响组件的单个大小,您无法控制. Swing旨在与布局管理器为核心一起工作,舍弃这些问题将不会导致问题和问题的终结,您将花费越来越多的时间来进行纠正.

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify.

JScrollPane(和JViewport)有自己的布局例程,您无需控制.将JList的大小传递给JScrollPane后,将不会生效.

JScrollPane (and JViewport) have there own layout routines, which you don't control. Setting the size of the JList will have no effect once you pass it to the JScrollPane.

请参见为什么皱眉要在SWING中使用空布局吗?了解更多详情

我不知道您希望listArea.setVisibleRowCount(-1);做什么,但是我建议您不要这样做.

I don't know what you expect listArea.setVisibleRowCount(-1); to do, but I'd advise against it.

已更新示例

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestList {

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

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

                JPanel panel = new JPanel(new BorderLayout());

                List<String> labels = new ArrayList<>(25);
                for (int index = 0; index < 100; index++) {
                    labels.add("Item " + index);
                }

                final JList<String> listArea = new JList<String>(labels.toArray(new String[labels.size()]));
                listArea.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                listArea.setFont(new Font("Serif", Font.ITALIC, 14));
                JScrollPane listScroller = new JScrollPane();
                listScroller.setViewportView(listArea);
                listArea.setLayoutOrientation(JList.VERTICAL);
                panel.add(listScroller);

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

}

这篇关于在Swing中将ScrollBar添加到JList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆