将Jlist添加到JScrollPane [英] Add a Jlist to a JScrollPane

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

问题描述

我有一个JList,我需要放在滚动窗格内,因为我从数据库中获取JList,值可以大大增加。我需要能够向下滚动它们,所以我写道:

I have a JList that I need to Place inside a scroll Pane because I am getting the JList from the database and the values can increase greatly. I need to be able to scroll them down so I wrote:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class Checkboxlistener extends JFrame {

private JPanel jpAcc = new JPanel();
private JList checkBoxesJList;

Checkboxlistener() {
   // super("Deposit base", false, true, false, true);
    setSize(1300, 600);
    jpAcc.setLayout(null);
    jpAcc.setBackground(Color.LIGHT_GRAY);
    String labels[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
    checkBoxesJList = new JList(labels);

    checkBoxesJList.setBounds(10, 30, 80, 600);
    checkBoxesJList.setBackground(Color.LIGHT_GRAY);
    checkBoxesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scrollPane = new JScrollPane(checkBoxesJList);

    jpAcc.add(checkBoxesJList);
    jpAcc.add(scrollPane);



    getContentPane().add(jpAcc);
    setVisible(true);
}

public static void main(String args[]) {
   Checkboxlistener cbl = new Checkboxlistener();
}
}

我做错了什么因为我看不到任何错误ScrollPane?

What Am I doing wrong because I do not see any ScrollPane?

推荐答案

列表已包含在滚动窗格内,因此您不能将列表添加到主面板。只有滚动窗格。

The list is already contained inside the scrollpane, so you must not add the list to the main panel. Only the scroll pane.

你做错的另一件事是不使用布局管理器,并设置组件的边界和大小。不要那样做。让布局管理器为您定位和调整组件的大小。

Another thing you're doing wrong is not using a layout manager, and setting the bounds and sizes of your components. Don't do that. Let the layout manager position and size the components for you.

最后,您不应该使用主线程中的Swing组件。仅限于事件派发线程

And finally, you shouldn't use Swing components from the main thread. Only in the event dispatch thread.

这是您的代码的修改版本,工作正常。我删除了背景颜色,因为这应由L& F处理:

Here's a modified version of your code that works fine. I removed the background colors, as this should be handled by the L&F:

public class Checkboxlistener extends JFrame {

    private JPanel jpAcc = new JPanel();
    private JList<String> checkBoxesJList;

    Checkboxlistener() {
        jpAcc.setLayout(new BorderLayout());
        String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
        checkBoxesJList = new JList<String>(labels);

        checkBoxesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane = new JScrollPane(checkBoxesJList);

        jpAcc.add(scrollPane);

        getContentPane().add(jpAcc);
        pack();
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Checkboxlistener cbl = new Checkboxlistener();
                cbl.setVisible(true);
            }
        });
    }
}

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

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