自动滚动JPanel

时间:2015-03-14 06:54:07

标签: java swing jscrollpane

告诉我如何自动滚动JPanel

JScrollBar sb = scroll.getVerticalScrollBar();
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    testPanel.setLayout(new BoxLayout(testPanel, BoxLayout.Y_AXIS));
    scroll.getViewport().add(testPanel);
    scroll.getViewport().setAutoscrolls(true);
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            testPanel.add(new JLabel("<html><img  height='80' width='100' src='https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png' /></html>"));
            testPanel.updateUI();
            sb.setValue(sb.getMaximum());
        }
    });

一切似乎都在起作用,但滚动效果还不算结束,图片不在视线范围内。

0 个答案:

没有答案