我有一个扩展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上。有人有任何想法吗?
答案 0 :(得分: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);