如何从头到尾制作jscrollpane卷轴?

时间:2015-07-29 10:54:56

标签: java jscrollpane

我试图从头到尾制作一个jscorllpane自动滚动。我对verticalbar使用set value方法,但它不起作用。我怎么能让它滚动? 这是我的代码:当我运行此代码时,它只是跳到jscorllpane的末尾,而不是一步一步地进行scorlling

import java.awt.event.*;
import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class JScrollPaneToTopAction implements ActionListener {
    JScrollPane scrollPane;

    public JScrollPaneToTopAction(JScrollPane scrollPane) {
        if (scrollPane == null) {
            throw new IllegalArgumentException("JScrollPaneToTopAction: null JScrollPane");
        }
        this.scrollPane = scrollPane;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        run();
    }

    public void run() {
        JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
        int i = verticalBar.getMinimum();
        while (i < verticalBar.getMaximum()) {
            verticalBar.setValue(verticalBar.getMinimum()+i);
            i += 50;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            scrollPane.getParent().invalidate();
            scrollPane.repaint();
            scrollPane.invalidate();
            System.out.println("changing " + i);
        }
    }

}

public class Draft20 {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Tabbed Pane Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea tr = new JTextArea();

        JLabel label = new JLabel("Label");
        label.setPreferredSize(new Dimension(1000, 1000));
        JScrollPane jScrollPane = new JScrollPane(label);

        JButton bn = new JButton("Move");

        bn.addActionListener(new JScrollPaneToTopAction(jScrollPane));

        frame.add(bn, BorderLayout.SOUTH);
        frame.add(jScrollPane, BorderLayout.CENTER);
        frame.setSize(400, 150);
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:2)

我认为问题是你的重绘调用是由EDT排队等待计算的,但是EDT正在忙着执行你的actionPerformed。只有在run()完成后,EDT才会执行重绘请求。这就是为什么你只能看到最终结果的原因。

我不确定我是否可以说清楚...查看此代码,它适用于我:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

class JScrollPaneToTopAction implements ActionListener, Runnable {

    JScrollPane scrollPane;

    public JScrollPaneToTopAction(JScrollPane scrollPane) {
        if (scrollPane == null) {
            throw new IllegalArgumentException("JScrollPaneToTopAction: null     JScrollPane");
        }
        this.scrollPane = scrollPane;
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        new Thread(this).start();
    }

    @Override
    public void run() {
        JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
        int i = verticalBar.getMinimum();
        while (i < verticalBar.getMaximum()) {
            verticalBar.setValue(verticalBar.getMinimum() + i);
            i += 50;
            try {
                Thread.sleep(100);
            }
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    scrollPane.getParent().invalidate();
                    scrollPane.repaint();
                    scrollPane.invalidate();
                }
            });
            System.out.println("changing " + i);
        }
    }
}

public class Draft20 {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Tabbed Pane Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea tr = new JTextArea();

        JLabel label = new JLabel("Label");
        label.setPreferredSize(new Dimension(1000, 1000));
        JScrollPane jScrollPane = new JScrollPane(label);

        JButton bn = new JButton("Move");

        bn.addActionListener(new JScrollPaneToTopAction(jScrollPane));

        frame.add(bn, BorderLayout.SOUTH);
        frame.add(jScrollPane, BorderLayout.CENTER);
        frame.setSize(400, 150);
        frame.setVisible(true);
    }
}

我希望它有所帮助。