java中基於timer計時器的圖片播放

全部註釋放在代碼中...(圖片的加入使用硬代碼)java

package com.sxt.jtime;

import java.awt.BorderLayout;

public class Itimer_test extends JFrame {

    private JPanel contentPane;
    private int index = 0;//圖片播放計數器
    private ImageIcon[] img;//聲明數組用來存放要播放的圖片
    JLabel label;//聲明爲全局變量用來顯示圖片
    private javax.swing.Timer time;//聲明的計數器
    private boolean istime;//用來標記自動播放 是否

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Itimer_test frame = new Itimer_test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Itimer_test() {
        this.istime=true;
        this.img = new ImageIcon[] { new ImageIcon("images/addStu.png"),
                new ImageIcon("images/back.png"),
                new ImageIcon("images/delstu.png"),
                new ImageIcon("images/option.png"),
                new ImageIcon("images/png_1.png") };
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        
        //計時器的聲明
        this.time = new javax.swing.Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (index == img.length - 1) {
                    index = 0;
                } else {
                    index++;
                }
                label.setIcon(img[index]);
            }
        });

        //上一張播放的按鈕事件
        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.SOUTH);

        JButton btnup = new JButton("\u4E0A\u4E00\u5F20");
        btnup.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

            }
        });
        panel.add(btnup);

        //自動比方的按鈕事件
        JButton btngo = new JButton("\u81EA\u52A8");
        btngo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(istime){
                    time.start();
                    istime=false;
                }else{
                    time.stop();
                    istime=true;
                }
                

            }
        });
        panel.add(btngo);

        //下一張圖片的按鈕事件
        JButton btnnext = new JButton("\u4E0B\u4E00\u5F20");
        btnnext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // index++;
                // label = new JLabel(this.img[index]);
                if (index == img.length - 1) {
                    index = 0;
                } else {
                    index++;
                }
                label.setIcon(img[index]);// 爲label設置圖片的額時須要作的是使用set.. 方法
            }
        });
        panel.add(btnnext);

        label = new JLabel(this.img[0]);
        contentPane.add(label, BorderLayout.CENTER);
    }

}
相關文章
相關標籤/搜索