我有一个jLabel,还有两个jpg格式的图片image1,image2,我想使image1和image2在jlabel中切换5次,如何实现?希望各位高手多多指点
我写了一段代码,可是就是不能实现,不知怎么回事
ImageIcon image1 = new ImageIcon("D:\\images\\cputoram1.jpg");
ImageIcon image2 = new ImageIcon("D:\\images\\cputoram.jpg");
jlabel.setIcon(image1);
Delay.executeDelay();
jlabel.setIcon(image2);
Delay.executeDelay();
jlabel.setIcon(image1);
Delay.executeDelay();
jlabel.setIcon(image2);
Delay.executeDelay();
jlabel.setIcon(image1);
Delay.executeDelay();
jlabel.setIcon(image1);// 我算过了executeDelay()能实现延时1秒
public class Delay { public static void executeDelay() {
for (int i = 0; i < 10000; i++) {
System.out.println("");
} }
}
我写了一段代码,可是就是不能实现,不知怎么回事
ImageIcon image1 = new ImageIcon("D:\\images\\cputoram1.jpg");
ImageIcon image2 = new ImageIcon("D:\\images\\cputoram.jpg");
jlabel.setIcon(image1);
Delay.executeDelay();
jlabel.setIcon(image2);
Delay.executeDelay();
jlabel.setIcon(image1);
Delay.executeDelay();
jlabel.setIcon(image2);
Delay.executeDelay();
jlabel.setIcon(image1);
Delay.executeDelay();
jlabel.setIcon(image1);// 我算过了executeDelay()能实现延时1秒
public class Delay { public static void executeDelay() {
for (int i = 0; i < 10000; i++) {
System.out.println("");
} }
}
public class Delay { public static void executeDelay() {
for (int i = 0; i < 10000; i++) {
System.out.println("");
} }
这种延迟不感恭维啊~。
Delay.executeDelay();
换成Thread.sleep(1000)休眠一秒
其他代码我看没问题
package src;import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;public class CutoverPicture extends JFrame { public static ImageIcon image1 = new ImageIcon("e:\\picture\\mian.jpg");
public static ImageIcon image2 = new ImageIcon("e:\\picture\\mini26.jpg");
public static int index = 0;
JLabel label1 = new JLabel();
public CutoverPicture(ImageIcon image) {
this.setLayout(new BorderLayout());
//this.setPreferredSize(new Dimension(500, 300));
label1.setIcon(image);
this.add(label1);
this.setLocation(200, 200);
this.pack();
this.setVisible(true);
} public void setCutoverIcon(ImageIcon image) {
label1.setIcon(image);
this.pack();
this.setVisible(true);
} public static void Cutover(CutoverPicture cutoverPicture) {
Image1Runnable image1Run = new Image1Runnable(cutoverPicture);
Image2Runnable image2Run = new Image2Runnable(cutoverPicture);
new Thread(image1Run).start();
new Thread(image2Run).start();
} public static void main(String[] args) {
CutoverPicture cutoverpicture = new CutoverPicture(image1);
Cutover(cutoverpicture);
}
}
class Image1Runnable implements Runnable {
CutoverPicture cutoverPicture;
Image1Runnable(CutoverPicture cutoverPicture) {
this.cutoverPicture = cutoverPicture;
} public void run() {
while (true) {
try {
Thread.sleep(3000);
} catch (InterruptedException ex1) {
}
if (CutoverPicture.index == 0) {
cutoverPicture.setCutoverIcon(CutoverPicture.image1);
CutoverPicture.index = 1;
System.out.println("11111");
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
}
}
}
}
}
class Image2Runnable implements Runnable {
CutoverPicture cutoverPicture;
Image2Runnable(CutoverPicture cutoverPicture) {
this.cutoverPicture = cutoverPicture;
} public void run() {
while (true) {
try {
Thread.sleep(3000);
} catch (InterruptedException ex1) {
}
if (CutoverPicture.index == 1) {
cutoverPicture.setCutoverIcon(CutoverPicture.image2);
CutoverPicture.index = 0;
System.out.println("22222");
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
}
}
}
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;public class SwitchIcon extends Frame implements Runnable {
JLabel lbl;
ImageIcon ico1, ico2;
int counter;
boolean bRun;
public static void main(String[] args) {
// TODO Auto-generated method stub
SwitchIcon si = new SwitchIcon();
si.setVisible(true);
}
public SwitchIcon() {
this.setBackground(new Color(255, 0, 0));
lbl = new JLabel();
try {
Image tmpImg = ImageIO.read(new File("d:\\a.bmp"));
ico1 = new ImageIcon(tmpImg);
tmpImg = ImageIO.read(new File("d:\\b.bmp"));
ico2 = new ImageIcon(tmpImg);
lbl.setIcon(ico1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setLayout(new BorderLayout(20, 20));
this.add(lbl, BorderLayout.CENTER);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
bRun = true;
counter = 0;
this.setSize(400, 300);
new Thread(this).start();
}
@Override
public void run() {
System.out.println("method run");
while (bRun) {
try {
Thread.sleep(1000);
counter++;
if (counter%2 == 0) {
lbl.setIcon(ico1);
} else {
lbl.setIcon(ico2);
}
this.paintComponents(this.getGraphics());
if (counter > 10) bRun = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}