java 里怎么做到两张图片先后移动
java里图片移动要用到线程,我想问下我要实现这样的效果,一张图片慢慢移到自己的位置后,另外一张图片才开始移动,这要怎么实现?我试过一个线程等待另一个线程结束以后才开始,...
java里图片移动要用到线程,我想问下我要实现这样的效果,一张图片慢慢移到自己的位置后,另外一张图片才开始移动,这要怎么实现?
我试过一个线程等待另一个线程结束以后才开始,但是这样第一样图片的移动就不会显示出来 展开
我试过一个线程等待另一个线程结束以后才开始,但是这样第一样图片的移动就不会显示出来 展开
2个回答
2009-06-09
展开全部
//;ImageMoving.java
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageMoving extends JWindow implements MouseListener,Runnable{
Image a,b;
JLabel l1,l2;
ImageMoving(String img1,String img2){
this.setSize(400,200);
this.setLocationRelativeTo(null);
this.getContentPane().setLayout(null);
try{
l1=new JLabel(new ImageIcon(ImageIO.read(new URL(img1))));
l2=new JLabel(new ImageIcon(ImageIO.read(new URL(img2))));
}catch(Exception e){}
l2.setLocation(getWidth(),0);
l1.setSize(200,200);
l2.setSize(l1.getSize());
add(l2);
add(l1);
this.addMouseListener(this);
}
public void run(){
while(l1.getX()<getWidth()/2){
l1.setLocation(l1.getX()+1,0);
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
Thread.yield();
}
while(l2.getX()>1){
l2.setLocation(l2.getX()-1,0);
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
Thread.yield();
}
}
public static void main(String[] args) {
ImageMoving im = new ImageMoving(
"https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif",
"http://www.google.cn/intl/zh-CN/images/logo_cn.gif");
im.setVisible(true);
new Thread(im).start();
}
//退出程序
public void mouseClicked(MouseEvent e) {System.exit(0);}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageMoving extends JWindow implements MouseListener,Runnable{
Image a,b;
JLabel l1,l2;
ImageMoving(String img1,String img2){
this.setSize(400,200);
this.setLocationRelativeTo(null);
this.getContentPane().setLayout(null);
try{
l1=new JLabel(new ImageIcon(ImageIO.read(new URL(img1))));
l2=new JLabel(new ImageIcon(ImageIO.read(new URL(img2))));
}catch(Exception e){}
l2.setLocation(getWidth(),0);
l1.setSize(200,200);
l2.setSize(l1.getSize());
add(l2);
add(l1);
this.addMouseListener(this);
}
public void run(){
while(l1.getX()<getWidth()/2){
l1.setLocation(l1.getX()+1,0);
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
Thread.yield();
}
while(l2.getX()>1){
l2.setLocation(l2.getX()-1,0);
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
Thread.yield();
}
}
public static void main(String[] args) {
ImageMoving im = new ImageMoving(
"https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif",
"http://www.google.cn/intl/zh-CN/images/logo_cn.gif");
im.setVisible(true);
new Thread(im).start();
}
//退出程序
public void mouseClicked(MouseEvent e) {System.exit(0);}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询