1

我有一个扩展 JPanel 的类。下面的代码:

public class Test extends JPanel implements Testnterface {
    private JScrollPane listScroller;

    DefaultListModel model;
    private JList requestList;    
    public Test() {

        String title = "Stackoverflow Question";
        setBorder(BorderFactory.createTitledBorder(title));
        model = new DefaultListModel();
     requestList = new JList(); // create a list
        listScroller = new JScrollPane(requestList); // create a scrollbar to the list
        listScroller.setPreferredSize(new Dimension(250, 80));

        setLayout(null);
        add(listScroller); // even though I add the scrollPane, nothing is displayed   
    }
 }

我更新了代码。即使我添加了它,我似乎也无法让我的 JScrollPane 显示在我的 JPanel 上。有人有什么想法吗?

4

1 回答 1

3

我遇到了同样的问题,并通过用 JPanel 包装滚动窗格来解决它:

final JScrollPane jScrollPane = new JScrollPane(list,
     JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

final JPanel panel = new JPanel(new BorderLayout(BORDER_SIZE, BORDER_SIZE));
panel.setBorder(BorderFactory.createEmptyBorder(BORDER_SIZE, BORDER_SIZE, 
      BORDER_SIZE, BORDER_SIZE));

panel.add(jScrollPane);
于 2012-04-12T09:37:04.683 回答