?? sliderdemo.java
字號:
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> * @author 杜江 * @version 1.0 */public class SliderDemo extends JPanel implements ActionListener, WindowListener, ChangeListener { //設(shè)置圖片的參數(shù) static final int FPS_MIN = 0; //設(shè)置最小值 static final int FPS_MAX = 30; //設(shè)置最大值 static final int FPS_INIT = 15; //初始數(shù)值 int frameNumber = 0; int NUM_FRAMES = 14; ImageIcon[] images = new ImageIcon[NUM_FRAMES]; int delay; Timer timer; boolean frozen = false; //這個標(biāo)簽用來顯示這只小狗 JLabel picture; public SliderDemo() { setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); delay = 1000 / FPS_INIT; //信息提示標(biāo)簽 JLabel sliderLabel = new JLabel("調(diào)整滑動桿,改變播放速度!", JLabel.CENTER); sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT); //創(chuàng)建一個滑動桿,定義了最小值和最大值以及初始值 JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, FPS_MIN, FPS_MAX, FPS_INIT); framesPerSecond.addChangeListener(this); //定義滑動桿參數(shù) framesPerSecond.setMajorTickSpacing(10);//每10刻度標(biāo)注一次 framesPerSecond.setMinorTickSpacing(1);//最小刻度為1 framesPerSecond.setPaintTicks(true);//繪制滑動桿上的刻度 framesPerSecond.setPaintLabels(true);//在滑動過程中繪制滑動塊 framesPerSecond.setBorder( BorderFactory.createEmptyBorder(0,0,10,0)); //定義一個用來顯示圖片的標(biāo)簽 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)); //設(shè)置一個定時器來觸發(fā)這個事件 timer = new Timer(delay, this); timer.setInitialDelay(delay * 7); //在每輪循環(huán)停頓時間 timer.setCoalesce(true);//設(shè)置重復(fù)循環(huán) }/** *<br>方法說明:添加一個窗體監(jiān)聽 *<br>輸入?yún)?shù): *<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 windowClosing(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {}/** *<br>方法說明:對滑動桿進(jìn)行監(jiān)聽 *<br>輸入?yún)?shù):ChangeEvent e 滑動桿變動事件 *<br>返回類型: */ public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); if (!source.getValueIsAdjusting()) { int fps = (int)source.getValue();//獲得滑動桿的值 if (fps == 0) { if (!frozen) stopAnimation(); } else { delay = 1000 / fps; timer.setDelay(delay); timer.setInitialDelay(delay * 10); if (frozen) startAnimation(); } } }/** *<br>方法說明:開始動畫 *<br>輸入?yún)?shù): *<br>返回類型: */ public void startAnimation() { timer.start(); frozen = false; }/** *<br>方法說明:停止動畫 *<br>輸入?yún)?shù): *<br>返回類型: */ public void stopAnimation() { timer.stop(); frozen = true; }/** *<br>方法說明:事件監(jiān)聽 *<br>輸入?yún)?shù): *<br>返回類型: */ public void actionPerformed(ActionEvent e) { //改變圖片幀 if (frameNumber == (NUM_FRAMES - 1)) { frameNumber = 0; } else { frameNumber++; } updatePicture(frameNumber); //顯示下張圖 if ( frameNumber==(NUM_FRAMES - 1) || frameNumber==(NUM_FRAMES/2 - 1) ) { timer.restart(); } }/** *<br>方法說明:繪制當(dāng)前幀 *<br>輸入?yún)?shù):int frameNum 圖片幀數(shù)數(shù) *<br>返回類型: */ protected void updatePicture(int frameNum) { if (images[frameNumber] == null) { images[frameNumber] = createImageIcon("images/doggy/T" + frameNumber + ".gif"); } //繪制圖片 if (images[frameNumber] != null) { picture.setIcon(images[frameNumber]); } else { //如果沒有發(fā)現(xiàn)圖片 picture.setText("image #" + frameNumber + " not found"); } }/** *<br>方法說明:獲取圖片 *<br>輸入?yún)?shù):String path 圖片路徑 *<br>返回類型:ImageIcon 圖片對象 */ protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = SliderDemo.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } }/** *<br>方法說明:主方法 *<br>輸入?yún)?shù): *<br>返回類型: */ public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); //定義窗體 JFrame frame = new JFrame("SliderDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //實例化本類 SliderDemo animator = new SliderDemo(); animator.setOpaque(true); frame.setContentPane(animator); //顯示窗體 frame.pack(); frame.setVisible(true); animator.startAnimation(); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -