Java 实现图片切换代码
时间: 2023-10-24 09:08:59 浏览: 118
这是一个很好的问题,以下是一个简单的Java图片切换代码示例:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ImageSlideShow extends JPanel implements ActionListener {
private ImageIcon[] images = { new ImageIcon("image1.jpg"), new ImageIcon("image2.jpg"), new ImageIcon("image3.jpg"), new ImageIcon("image4.jpg") };
private JLabel imageLabel;
private int currentImageIndex = 0;
private Timer timer;
public ImageSlideShow() {
imageLabel = new JLabel(images[currentImageIndex]);
add(imageLabel);
timer = new Timer(2000, this);
timer.setInitialDelay(0);
timer.start();
}
public void actionPerformed(ActionEvent e) {
currentImageIndex++;
if (currentImageIndex == images.length) {
currentImageIndex = 0;
}
imageLabel.setIcon(images[currentImageIndex]);
}
public static void main(String[] args) {
JFrame frame = new JFrame("ImageSlideShow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ImageSlideShow());
frame.pack();
frame.setVisible(true);
}
}
```
希望您会喜欢这个简单的Java图片切换示例,有任何问题请随时与我交流。
阅读全文