一个例子,循环显示图片,满足你的要求的! (见注释处)import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;public class TimerDemo implements ActionListener,ChangeListener{

JFrame f=null;
ImageIcon[] icons;
JSlider slider;
JLabel label;
JToggleButton toggleb1,toggleb2;
JButton b;
javax.swing.Timer timer;
int index=0;

public TimerDemo(){
f=new JFrame("Timer Example");
Container contentPane=f.getContentPane();
icons=new ImageIcon[5];
for(int i=0;i<icons.length;i++){
icons[i]=new ImageIcon("doc"+(i+1)+".gif");
}

label=new JLabel(icons[0]);
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(2,1));
slider=new JSlider();
slider.setPaintTicks(true);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(10);
slider.setPaintLabels(true);
slider.addChangeListener(this);
panel1.add(slider);

JPanel buttonPanel=new JPanel();
buttonPanel.setLayout(new GridLayout(1,3));
toggleb1=new JToggleButton("Start");
toggleb1.addActionListener(this);
buttonPanel.add(toggleb1);

b=new JButton("Restart");
b.addActionListener(this);
buttonPanel.add(b);

toggleb2=new JToggleButton("Don't Repeat");
toggleb2.addActionListener(this);
buttonPanel.add(toggleb2);
panel1.add(buttonPanel);

Hashtable table=new Hashtable();
table.put(new Integer(0),new JLabel("低"));
table.put(new Integer(50),new JLabel("中"));
table.put(new Integer(100),new JLabel("高"));
slider.setLabelTable(table);

timer=new javax.swing.Timer(slider.getValue()*10,this);

contentPane.add(label,BorderLayout.CENTER);
contentPane.add(panel1,BorderLayout.SOUTH);

f.pack();
f.show();

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
f.dispose();
System.exit(0);
}
});
}

public static void main(String args[]){
new TimerDemo();
}

public void actionPerformed(ActionEvent e){
if(e.getSource()==toggleb1){
if(e.getActionCommand().equals("Start")){
timer.start();
toggleb1.setText("Stop");
}
if(e.getActionCommand().equals("Stop")){
timer.stop();
toggleb1.setText("Start");
}
}
if(e.getSource()==toggleb2){
if(timer.isRepeats()){
timer.setRepeats(false);
}
else{
timer.setRepeats(true);
timer.start();
}
}
if(e.getSource()==b){
slider.setValue(50);
timer.restart();
}
if(e.getSource()==timer){
if(index==5){
index=0;
}
label.setIcon(icons[index]);//就在这里了
label.repaint();
index++;
}
}

public void stateChanged(ChangeEvent e){
timer.setDelay(slider.getValue()*10);
}
}

解决方案 »

  1.   

    tomcatjava(小鱼儿) ,你好!我的意思是不改变载入LABEL的图片的名字,而是去改变图片的内容,在程序中能够得到更新,你给我的例子是改变了载入图片的名称了.有什么好的办法吗?谢谢了
     chenhailiangbiy(我要学)   做策略文件是怎么做呀,教教我.谢谢!
      

  2.   

    我试了,没问题啊,是不是图片咱径有误啊package test;import javax.swing.*;
    import java.awt.*;
    import com.borland.jbcl.layout.*;
    import java.awt.event.*;public class Frame1 extends JFrame {
      JButton jButton1 = new JButton();
      static Frame1 frame1;
      XYLayout xYLayout1 = new XYLayout();  public Frame1() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        frame1 = new Frame1();
        frame1.setSize(320,240);
        frame1.setVisible(true);
      }
      private void jbInit() throws Exception {
        jButton1.setText("jButton1");
        jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
        this.getContentPane().setLayout(xYLayout1);
        this.getContentPane().add(jButton1,  new XYConstraints(163, 5, -1, -1));
      }  void jButton1_actionPerformed(ActionEvent e) {
       JLabel jLabel1 = new JLabel();
       jLabel1.setBorder(BorderFactory.createLineBorder(Color.black));
       jLabel1.setText("jLabel1");
       Icon ss = new ImageIcon("c:\\nh.gif");
       jLabel1.setIcon(ss);
       frame1.getContentPane().add(jLabel1,   new XYConstraints(82, 56, 251, 148));
       jLabel1.repaint();
       frame1.repaint();
       frame1.validate();
      }
    }class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
      Frame1 adaptee;  Frame1_jButton1_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
      }
      public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
      }
    }
      

  3.   

    这段代码是没有问题.
    void button1_actionPerformed(ActionEvent e) {
        JLabel jLabel1 = new JLabel();
        jLabel1.setBorder(BorderFactory.createLineBorder(Color.black));
        jLabel1.setText("jLabel1");
        Icon ss = new ImageIcon("c:\\img\\1.jpg");
        jLabel1.setIcon(ss);
        contentPane.add(jLabel1,   new XYConstraints(82, 56, 251, 148));
        jLabel1.repaint();
        this.repaint();
        this.validate();
      }
    我的意思是在程序运行的时候,我点击button1后,c:\\img\\1.jpg会显示在jLabel1上面,这时候我进入C:\img\,用WINDOWS画笔等一类的绘图软件编辑图片1.jpg,保存改变之后的1.jpg.然后我在去运行的程序中点击button1,怎么才能使刚才用绘图软件保存的结果正确的显示在jLabel1上呢??(在编辑图片的过程中程序一直是处于运行状态.)
      

  4.   

    哈哈啊
    我来接分了!200分也!
    首先,看看Toolkit的getImage方法的说明
      Returns an image which gets pixel data from the specified URL. The pixel data referenced by the specified URL must be in one of the following formats: GIF, JPEG or PNG. The underlying toolkit attempts to resolve multiple requests with the same URL to the same returned Image. Since the mechanism required to facilitate this sharing of Image objects may continue to hold onto images that are no longer of use for an indefinite period of time, developers are encouraged to implement their own caching of images by using the createImage variant wherever available
      他提到了个createImage,(我英文不是太好,不敢瞎翻译,大虾可以解释一下)
    用这个方法就可以解决这个问题了,web应用的时候也有这个问题,也可以 try 一下
      

  5.   

    大家好像都没有注意到这个问题
    当你的图片,被改变了,getImage方法,是不会体现这一改变的
    到底什么原因,有没有大虾给解决一下呢?
    给大家讲一讲
      

  6.   

    具体的就是这样!
        Image imge = Toolkit.getDefaultToolkit().createImage("c:\\1.GIF");
        ImageIcon lala = new ImageIcon(imge);
        jLabel1.setIcon(lala);