import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener{
JLabel label;JButton button;
Icon andian,liangdian;
MyFrame(){
setSize(400,300);
getContentPane().setLayout(new FlowLayout());
andian=new ImageIcon( "andian.jpg ");
liangdian=new ImageIcon( "liangdian.jpg ");
label=new JLabel(andian);
button=new JButton( "确定 ");
getContentPane().add(label);
getContentPane().add(button);
button.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
for(int i=1;i <=10;i++){
try{
label.setIcon(liangdian);
Thread.sleep(50);
label.setIcon(andian);
Thread.sleep(50);
}
catch(InterruptedException e1){}
}
}
}
}
public class Cc{
public static void main(String args[]){
new MyFrame();
}
}
//JLabel不能像Label那样循环中运行一次就显示一次结果,一定要全部运行完只显示最后结果的吗?
//如何才能在JLabel中显示出闪的效果?
------解决方案--------------------
又是同样的问题,这样的问题我回答不知道多少个了。
new Thread(){
public void run(){
for(int i=1;i <=10;i++){
try{
label.setIcon(liangdian);
Thread.sleep(50);
label.setIcon(andian);
Thread.sleep(50);
}
catch(InterruptedException e1){}
}
}
}.start();