scrollPane无法正常工作,所以我想知道缺少了什么

时间:2018-09-18 12:12:08

标签: java scrollbar panel jscrollpane scrollpane

我正在尝试使滚动条起作用。它已经显示在面板中,但是其作用就像下面没有任何内容一样,因此它不会下降。这是我的代码(已添加所有库):

JPanel panel = new JPanel();

panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));

panel.setBackground(Color.WHITE);

panel.setBounds(10, 66, 360, 705);

add(panel);

panel.setLayout(null);

JScrollPane scrollPane = new JScrollPane();

scrollPane.setBounds(345, 0, 20, 500);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

scrollPane.getVerticalScrollBar().setUnitIncrement(10);

scrollPane.setVisible(true);

panel.setPreferredSize(new Dimension(WIDTH,10));

scrollPane.setAutoscrolls(true);

panel.add(scrollPane);

1 个答案:

答案 0 :(得分:0)

尝试

JPanel panel = new JPanel();

panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));

panel.setBackground(Color.WHITE);

panel.setBounds(10, 66, 360, 705);

panel.setLayout(null);

JScrollPane scrollPane = new JScrollPane(panel);

scrollPane.setBounds(345, 0, 20, 500);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

scrollPane.getVerticalScrollBar().setUnitIncrement(10);

scrollPane.setVisible(true);

panel.setPreferredSize(new Dimension(WIDTH,10));

scrollPane.setAutoscrolls(true);

add(scrollPane);

注意从add(panel)到add(scrollPane)以及新的JScrollPane(panel)的更改;线