如何创建水平滑块

时间:2009-08-31 03:44:53

标签: java swing

如何创建一个水平滑块,每隔1秒后自动从左向右滑动。

2 个答案:

答案 0 :(得分:2)

static final int FPS_MIN = 0;
static final int FPS_MAX = 30;
static final int FPS_INIT = 15;    

JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL,
                                      FPS_MIN, FPS_MAX, FPS_INIT);

framesPerSecond.setMajorTickSpacing(10);
framesPerSecond.setMinorTickSpacing(1);
framesPerSecond.setPaintTicks(true);
framesPerSecond.setPaintLabels(true);

http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html

答案 1 :(得分:1)

/*
 * Copyright © 2009. Artificial Machines Pvt. Ltd. India.
 * All rights reserved.
 * This file is a property of Artificial Machines Pvt. Ltd.
 * It is illegal to modify, copy or use any part of this file, for
 * any purpose outside of Artificial Machines Pvt. Ltd. products.
 */

package trypls;

/**
 *
 * @author sunil.s
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.Timer;

public class Test {

    static int percent = 0;

    public static void main(String[] args) {
        JFrame f = new JFrame();
        final JSlider s = new JSlider();
        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;
//                        s.setMajorTickSpacing(10);
//s.setMinorTickSpacing(1);
//s.setPaintTicks(true);
//s.setPaintLabels(true);

//                        JScrollBar sb = s.getHorizontalScrollBar();
                        s.setValue((int)(s.getMaximum()*(percent/100.0)));
                        s.setAutoscrolls(true);
                }
        });
        time.start();
    }

}