import java.awt.*;
import javax.swing.*;
class demo extends JFrame implements Runnable
{
private int sleeptime;
private Thread thread;
private JLabel label;
private JPanel panel;
private ImageIcon background;
private int temp;
public demo(int stime)
{
temp=1;
panel=new JPanel();
label=new JLabel();
panel.add(label);
getContentPane().add(panel);
sleeptime=stime;
setSize(600,400);
setVisible(true);
thread=new Thread(this);
thread.start();
}
public void run()
{
try
{
while(true)
{
if (temp>3)
{
temp=1;
}
background=new ImageIcon(temp+".jpg");
label.setIcon(background);
this.setSize(background.getIconWidth(),background.getIconHeight());
temp=temp+1;
thread.sleep(sleeptime);
}
}
catch(Exception e)
{
System.out.print("Error!");
}
}
public static void main(String args[])
{
new demo(1000);
}
}
怎么做才能frame适应图片大小,我上面虽然写了一句但效果不好,用了布局也不行
import javax.swing.*;
class demo extends JFrame implements Runnable
{
private int sleeptime;
private Thread thread;
private JLabel label;
private JPanel panel;
private ImageIcon background;
private int temp;
public demo(int stime)
{
temp=1;
panel=new JPanel();
label=new JLabel();
panel.add(label);
getContentPane().add(panel);
sleeptime=stime;
setSize(600,400);
setVisible(true);
thread=new Thread(this);
thread.start();
}
public void run()
{
try
{
while(true)
{
if (temp>3)
{
temp=1;
}
background=new ImageIcon(temp+".jpg");
label.setIcon(background);
this.setSize(background.getIconWidth(),background.getIconHeight());
temp=temp+1;
thread.sleep(sleeptime);
}
}
catch(Exception e)
{
System.out.print("Error!");
}
}
public static void main(String args[])
{
new demo(1000);
}
}
怎么做才能frame适应图片大小,我上面虽然写了一句但效果不好,用了布局也不行
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货