我不得不在netbeans gui中制作一种动画。因此,我正在研究有关互联网上的摆动计时器,并且从发现的结果中我找到了一种方法,该方法将在特定时间段后更改jLabel中的图像。
public void animation() throws InterruptedException {
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
t++;
System.out.printf("Reading SMTP Info. %d\n",t);
if(t%2==1){
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/oncsreen_keypad/a.jpg")));
}
else{
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/oncsreen_keypad/b.jpg")));
}
}
};
Timer timer = new Timer( 1000 , taskPerformer);
//timer.setRepeats(false);
timer.start();
Thread.sleep(5000);
}
此方法无处。但是,如果System.out.printf有效,则更改jLabel中的图像也应该有效。但是实际上,这些行对jLabel没有任何影响。
那么什么是正确的方法。
不要使用Thread.sleep ..停止主线程,使用Swing计时器并给他延迟,以更改图像。
我为您做了一个小例子。
这是使用JPanel生成JFrame的类,该JPanel包含JLabel。
package timerdemo;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author ottp
* @version 1.0
*/
public class Gui extends JFrame {
private JLabel jLabel;
private Timer timer;
private boolean chromeShown;
public Gui() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
JPanel panel = new JPanel(new BorderLayout());
jLabel = new JLabel(new ImageIcon("/home/ottp/Downloads/chrome.png"));
chromeShown = true;
panel.add(jLabel);
timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(chromeShown) {
jLabel.setIcon(new ImageIcon("/home/ottp/Downloads/ok.png"));
chromeShown = false;
} else {
jLabel.setIcon(new ImageIcon("/home/ottp/Downloads/chrome.png"));
chromeShown = true;
}
}
});
timer.start();
this.getContentPane().add(panel);
this.setVisible(true);
}
}
并开始...
package timerdemo;
import javax.swing.SwingUtilities;
/**
*
* @author ottp
*/
public class TimerDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Gui();
}
});
}
}
在您的Gui类中启动计时器后,JLabel上的图像将每5秒更改一次,其条件是布尔标志。您也可以在其中使用if ... else构造。
希望这会有所帮助
帕特里克
有人可以指出我修改后的Gui类(如下)从资源加载图像的问题,它仍然没有更新图像,它仅显示第一个图像“ /src/main/resources/images/bass_b.png ”。我验证并仔细检查了其他图像“ bass_a.png”,“ bass_e.png”是否正确以及在资源/图像中。预先谢谢你。
package src.main.java.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author ottp
* @version 1.0
*/
public class Gui extends JFrame {
private static final long serialVersionUID = 7444343315460184909L;
private JLabel jLabel;
private Timer timer;
private boolean chromeShown;
static String path;
static ImageIcon ic;
static JPanel panel;
public Gui() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
panel = new JPanel(new BorderLayout());
path = this.getClass().getResource("/src/main/resources/images/bass_b.png").getFile();
ic = new ImageIcon(path);
jLabel = new JLabel(ic);
chromeShown = true;
panel.add(jLabel);
timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(chromeShown) {
path = this.getClass().getResource("/src/main/resources/images/bass_a.png").getFile();
ic = new ImageIcon(path);
jLabel = new JLabel(ic);
chromeShown = false;
} else {
path = this.getClass().getResource("/src/main/resources/images/bass_e.png").getFile();
ic = new ImageIcon(path);
//jLabel.setIcon(ic);
jLabel = new JLabel(ic);
chromeShown = true;
}
}
});
timer.start();
this.getContentPane().add(panel);
this.setVisible(true);
}
}