[JAVA100例]023、滑动杆
时间:2011-01-09
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* <p>Title: 滑动杆演示</p>
* <p>Description: 使用滑动杆控制定时器,来控制图片的播放速度</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: SliderDemo.java</p>
* @version 1.0
*/
public class SliderDemo extends JPanel
implements ActionListener,
WindowListener,
ChangeListener {
//设置图片的参数
static final int FPS_MIN = 0; //设置最小值
static final int FPS_MAX = 30; //设置最大值
static final int FPS_INIT = 15; //初始数值
int frameNumber = 0;
int NUM_FRAMES = 14;
ImageIcon[] images = new ImageIcon[NUM_FRAMES];
int delay;
Timer timer;
boolean frozen = false;
//这个标签用来显示这只小狗
JLabel picture;
public SliderDemo() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
delay = 1000 / FPS_INIT;
//信息提示标签
JLabel sliderLabel = new JLabel("调整滑动杆,改变播放速度!", JLabel.CENTER);
sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//创建一个滑动杆,定义了最小值和最大值以及初始值
JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL,
FPS_MIN, FPS_MAX, FPS_INIT);
framesPerSecond.addChangeListener(this);
//定义滑动杆参数
framesPerSecond.setMajorTickSpacing(10);//每10刻度标注一次
framesPerSecond.setMinorTickSpacing(1);//最小刻度为1
framesPerSecond.setPaintTicks(true);//绘制滑动杆上的刻度
framesPerSecond.setPaintLabels(true);//在滑动过程中绘制滑动块
framesPerSecond.setBorder(
BorderFactory.createEmptyBorder(0,0,10,0));
//定义一个用来显示图片的标签
picture = new JLabel();
picture.setHorizontalAlignment(JLabel.CENTER);
picture.setAlignmentX(Component.CENTER_ALIGNMENT);
picture.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(),
BorderFactory.createEmptyBorder(10,10,10,10)));
updatePicture(0); //显示第一张图
//将成员添加到面板上
add(sliderLabel);
add(framesPerSecond);
add(picture);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//设置一个定时器来触发这个事件
timer = new Timer(delay, this);
timer.setInitialDelay(delay * 7); //在每轮循环停顿时间
timer.setCoalesce(true);//设置重复循环
}
/**
*<br>方法说明:添加一个窗体监听
*<br>输入参数:
*<br>返回类型:
*/
void addWindowListener(Window w) {
w.addWindowListener(this);
}
public void windowIconified(WindowEvent e) {
stopAnimation();
}
public void windowDeiconified(WindowEvent e) {
startAnimation();
}
public void windowOpened(WindowEvent e) {}
public void
|