java 实现label里面图片的交替显示
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclasstestextendsJFr...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame implements ActionListener{
Boolean start;
String path="E:/pic/";
Icon p[]=new Icon[3];//一定要指定长度
JLabel label;
public test() {
this.setTitle("图片的交替显示");
this.setSize(400,200);
this.setVisible(true);
Container c=this.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
label=new JLabel();
JButton b=new JButton("前面");
c.add(b);
c.add(label);
b.addActionListener(this);
p[0]=new ImageIcon(path+"g.gif");
p[1]=new ImageIcon(path+"o.gif");
p[2]=new ImageIcon(path+"p.gif");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
start=true;
while(start){
for(int i=0;i<3;i++){
label.setIcon(p[i]);
}
}
}
public static void main(String args[]){
new test();
}
}
为什么在按了按钮之后,窗口就貌似死掉了,图片也没有显示,窗口都关不掉
如何实现图片的交替显示呢 展开
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame implements ActionListener{
Boolean start;
String path="E:/pic/";
Icon p[]=new Icon[3];//一定要指定长度
JLabel label;
public test() {
this.setTitle("图片的交替显示");
this.setSize(400,200);
this.setVisible(true);
Container c=this.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
label=new JLabel();
JButton b=new JButton("前面");
c.add(b);
c.add(label);
b.addActionListener(this);
p[0]=new ImageIcon(path+"g.gif");
p[1]=new ImageIcon(path+"o.gif");
p[2]=new ImageIcon(path+"p.gif");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
start=true;
while(start){
for(int i=0;i<3;i++){
label.setIcon(p[i]);
}
}
}
public static void main(String args[]){
new test();
}
}
为什么在按了按钮之后,窗口就貌似死掉了,图片也没有显示,窗口都关不掉
如何实现图片的交替显示呢 展开
展开全部
actionPerformed方法中不能写死循环或者耗时过长的代码,否则肯定会导致窗口死掉
利用线程来实现
public class test extends JFrame implements ActionListener,Runnable{
Boolean start;
Thread timer;.
int index = 0;
String path="E:/pic/";
Icon p[]=new Icon[3];//一定要指定长度
JLabel label;
public test() {
this.setTitle("图片的交替显示");
this.setSize(400,200);
this.setVisible(true);
Container c=this.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
label=new JLabel();
JButton b=new JButton("前面");
c.add(b);
c.add(label);
b.addActionListener(this);
p[0]=new ImageIcon(path+"g.gif");
p[1]=new ImageIcon(path+"o.gif");
p[2]=new ImageIcon(path+"p.gif");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
start=true;
try {
timer.interrupt() ;
} catch(Exception exp) {}
timer = new Thread(this);
timer.start();
}
public static void main(String args[]){
new test();
}
public void run() {
while (true) {
if (index >= 3) index = 0;
label.setIcon(p[index]);
index++;
try {
Thread.sleep(1000);
} catch(Exception e) {}
}
}
}
}
利用线程来实现
public class test extends JFrame implements ActionListener,Runnable{
Boolean start;
Thread timer;.
int index = 0;
String path="E:/pic/";
Icon p[]=new Icon[3];//一定要指定长度
JLabel label;
public test() {
this.setTitle("图片的交替显示");
this.setSize(400,200);
this.setVisible(true);
Container c=this.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
label=new JLabel();
JButton b=new JButton("前面");
c.add(b);
c.add(label);
b.addActionListener(this);
p[0]=new ImageIcon(path+"g.gif");
p[1]=new ImageIcon(path+"o.gif");
p[2]=new ImageIcon(path+"p.gif");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
start=true;
try {
timer.interrupt() ;
} catch(Exception exp) {}
timer = new Thread(this);
timer.start();
}
public static void main(String args[]){
new test();
}
public void run() {
while (true) {
if (index >= 3) index = 0;
label.setIcon(p[index]);
index++;
try {
Thread.sleep(1000);
} catch(Exception e) {}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询