如何创建一个水平滚动条,它会在一段时间间隔后自动滚动

时间:2009-08-29 10:15:50

标签: java swing

嗨我想要水平滚动条,每隔1秒后会自动从左向右滚动。

由于 Sunil Kumar Sahoo

2 个答案:

答案 0 :(得分:3)

此代码示例将为您提供所需内容。它会创建一个滚动窗格并滚动水平滚动条。它在完成时也会从头开始循环。

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

import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class Test {

    static String ss = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lectus ligula, ultricies at varius ut, condimentum eget dui. Cras sapien justo, fermentum vulputate commodo vel, aliquam vitae neque. Mauris cursus bibendum accumsan. Etiam euismod dapibus pellentesque. Phasellus et velit nunc, eget dignissim odio. Duis et nulla eget mauris laoreet venenatis eget a purus. Phasellus congue massa in nulla pellentesque mollis. Proin id lorem ut odio fringilla laoreet et vitae nunc. Nunc non arcu justo, pellentesque posuere nulla. Ut eget sollicitudin libero. Ut at luctus arcu.";
    static int percent = 0;

    public static void main(String[] args) {
        JFrame f = new JFrame();
        JTextArea t = new JTextArea(10,50);
        t.setText(ss);
        final JScrollPane s = new JScrollPane(t);
        f.getContentPane().add(s);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
        Timer time = new Timer(100, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                percent++;
                if (percent>100)
                    percent = 0;
                JScrollBar sb = s.getHorizontalScrollBar();
                sb.setValue((int)(sb.getMaximum()*(percent/100.0)));
            }
        });
        time.start();
    }

}

答案 1 :(得分:0)

如何激活滚动条的timer set the value到所需的值?