如何让滚动条的默认位置位于顶部?

时间:2017-02-01 07:46:07

标签: java swing jscrollpane

我在JScrollPane中有一个JPanel,而JPanel包含许多小的JPanel。但是滚动条的默认位置不在顶部,因此第一个不显示小JPanel。如何让滚动条的默认位置位于顶部?

我的代码已简化,如下所示:

JFrame resultFrame = new JFrame("Searching Result");

JPanel listPanel = new JPanel();
listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
for (int i=0; i<5; i++) {
    listPanel.add(smallPanel) // smallPanel is created in another class, which is related to i.
}
JScrollPane scrollPanel = new JScrollPane(listPanel);
scrollPanel.setPreferredSize(new Dimension(500, 750));

resultFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
resultFrame.setContentPane(scrollPanel);
resultFrame.pack();
resultFrame.setLocationRelativeTo(null);
resultFrame.setVisible(true);

0 个答案:

没有答案