求助:swing 方面的 谁告诉我有哪个控件可以让图片自动移动的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我可以给你写个,不过,需要money 将图片放在Label中,开个线程定时替换Label中的图片,假定图片名为1.gif,2.gif,3.gif,4.gif示意代码如下label.setIcon(new ImageIcon("1.gif") );int count=1;while(true){ try{ label.setIcon(new ImageIcon(count+".gif")); count++; if(count>4) {count=1;} repaint(); sleep(1000/25); } catch(Exception e) { System.out.println("线程错误"+e.toString()); }} 写个换图片的线程吧,不过由于swing并不是线程安全的,所以将换图片这个动作放入到swing event dispatch thread中 java核心技术第二卷有 自己看看应该可以写出来 也可以不需要线程...把图片放到一个Component里...paint会时时刷新组件,只需要在这个方法里面改变图片需要显示的位置即可... package day20;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Shape;import java.awt.geom.Ellipse2D;import javax.swing.JFrame;import javax.swing.JPanel;public class ThreadTest { private JFrame frame; public ThreadTest(){ frame=new JFrame("自动"); GraphPanel gp=new GraphPanel(); frame.add(gp,BorderLayout.CENTER); } public void showMe(){ frame.setVisible(true); frame.setSize(500,500); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); } public static void main(String[] args) { new ThreadTest().showMe(); }}class GraphPanel extends JPanel { private int x,y; private Color c; private int i=0; private boolean stop = false; public GraphPanel(){ Thread t=new moveThread(); t.start(); } class moveThread extends Thread{ public void run() { while(stop == false){ System.out.println("被刷新"); try { Thread.sleep(3000); x=(int)(Math.random()*200+1); y=(int)(Math.random()*200+1); repaint(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public void paintComponent(Graphics g){ super.paintComponent(g); if(i%2==0){ c=Color.BLUE; }else{ c=Color.RED; } i++; System.out.println(x); Graphics2D g2=(Graphics2D)g; Shape s=new Ellipse2D.Double(x,y,55,55); g2.setColor(c); g2.draw(s); g2.fill(s); }}LZ看看,这段代码是我用来使一个圆随机变动的,使用线程实现的,可以参照一下 找一下jdk包中的demo程序,里边有比较多的程序涉及这方面得问题。 类创建方法有四个:New 反射。 新手问个问题 java GUI 中如何嵌入网页,并跟网页交互 用什么方法可以知道在本计算机上某个端口已经被站用了? 问个问题?关于追加对象输出流的 求助,servlet输入流附件下载时中文文件名问题 关于URL编码 SCJP中JavaApplet会怎么考?我在考纲里没有看到明确内容 关于Observable的问题,有谁遇到过? 如何打开eclipse的assert功能 求一个 网络搜索引擎的 例子 请教,先感谢一下
示意代码如下
label.setIcon(new ImageIcon("1.gif") );
int count=1;
while(true)
{
try{
label.setIcon(new ImageIcon(count+".gif"));
count++;
if(count>4)
{count=1;}
repaint();
sleep(1000/25);
}
catch(Exception e)
{
System.out.println("线程错误"+e.toString());
}
}
把图片放到一个Component里...
paint会时时刷新组件,只需要在这个方法里面改变图片需要显示的位置即可...
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;import javax.swing.JFrame;
import javax.swing.JPanel;public class ThreadTest {
private JFrame frame;
public ThreadTest(){
frame=new JFrame("自动");
GraphPanel gp=new GraphPanel();
frame.add(gp,BorderLayout.CENTER);
}
public void showMe(){
frame.setVisible(true);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ThreadTest().showMe(); }}class GraphPanel extends JPanel {
private int x,y;
private Color c;
private int i=0;
private boolean stop = false;
public GraphPanel(){
Thread t=new moveThread();
t.start();
}
class moveThread extends Thread{
public void run() {
while(stop == false){
System.out.println("被刷新");
try {
Thread.sleep(3000);
x=(int)(Math.random()*200+1);
y=(int)(Math.random()*200+1);
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(i%2==0){
c=Color.BLUE;
}else{
c=Color.RED;
}
i++;
System.out.println(x);
Graphics2D g2=(Graphics2D)g;
Shape s=new Ellipse2D.Double(x,y,55,55);
g2.setColor(c);
g2.draw(s);
g2.fill(s);
}
}
LZ看看,这段代码是我用来使一个圆随机变动的,使用线程实现的,可以参照一下