简体   繁体   English

在我的JList上添加滚动

[英]Add scroll on my JList

My ScrollPane won't show to my JList. 我的ScrollPane不会显示在我的JList中。 I tried using this , this , and many other ways to add a scroll on my jlist but it wont just show. 我尝试使用thisthis和许多其他方式在jlist上添加滚动条,但它不会仅仅显示。 Here is the sample of my code. 这是我的代码示例。

        JScrollPane scrollPane = new JScrollPane(list_1);
    list_1 = new JList();
    scrollPane.setViewportView(list_1);
    list_1.setBounds(16, 94, 579, 248);
    contentPane.add(list_1);
    contentPane.add(scrollPane);

My JList consists of array of files (paths) that came from my database. 我的JList包含来自数据库的文件(路径)数组。

The list needs to be created before being added to the scrollpane. 在将列表添加到滚动窗格之前,需要先创建该列表。 The code should be: 代码应为:

list_1 = new JList();
JScrollPane scrollPane = new JScrollPane(list_1);
//list_1 = new JList();

A component can only have a single parent. 一个组件只能有一个单亲。 The code should be: 代码应为:

//contentPane.add(list_1); // this will remove the list from the scrollpane
contentPane.add(scrollPane);

Don't use setBounds(): 不要使用setBounds():

list_1.setBounds(16, 94, 579, 248);

The scrollpane uses its own layout manager so the above code does nothing. 滚动窗格使用其自己的布局管理器,因此上述代码不执行任何操作。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。

3 problems with 6 lines of code. 6行代码有3个问题。 I suggest you start by reading the section from the Swing tutorial on How to Use Lists for more information and working examples. 建议您先阅读Swing教程中有关如何使用列表的部分, 获取更多信息和工作示例。

The tutorial is full of basic Swing information and will help you get started with simple examples containing better structured code. 本教程包含了Swing的基本信息,将帮助您开始使用包含更好的结构化代码的简单示例。

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

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