所有注释放在代码中...(图片的加入使用硬代码)
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); } }