在Java怎么让图片水平移动? 15
importjavax.swing.ImageIcon;importjavax.swing.JButton;importjavax.swing.JFrame;import...
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ball extends JFrame{
JLabel ball;
JLabel cup1,cup2,cup3;
Timer timer;
public ball(){
this.setBounds(200,30,965,700);
this.getContentPane().setBackground(Color.black);
this.setTitle("猜我在哪");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new demo());
this.getContentPane().setLayout(null);
timer =new Timer(100,new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cup2.getLocation().y<250){
cup2.setLocation(400, cup2.getLocation().y+10);
}
else{
timer.stop();
}
}
});
cup1=new JLabel(new ImageIcon("images/cup.jpg"));
cup1.setBounds(150,250,180,250);
cup2=new JLabel(new ImageIcon("images/cup.jpg"));
cup2.setBounds(400,100,180,250);
cup3=new JLabel(new ImageIcon("images/cup.jpg"));
cup3.setBounds(650,250,180,250);
this.getContentPane().add(cup1);
this.getContentPane().add(cup2);
this.getContentPane().add(cup3);
ball=new JLabel(new ImageIcon("images/ball.jpg"));
ball.setBounds(450,380,100,110);
this.getContentPane().add(ball);
this.setVisible(true);
}
class demo extends MouseAdapter{
public void mouseClicked(MouseEvent e){
timer.start();
}
}
public static void main(String[] args)
{
new ball();
}
}
我需要的是要让图片动起来的 老师说的是什么水平移动 我不知道写哪里 怎么写 有些写的都是老师指点的 还有怎么加那个让球随机放杯子下面那个 还有在主页怎样写提示 点击空白处开始的 展开
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ball extends JFrame{
JLabel ball;
JLabel cup1,cup2,cup3;
Timer timer;
public ball(){
this.setBounds(200,30,965,700);
this.getContentPane().setBackground(Color.black);
this.setTitle("猜我在哪");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new demo());
this.getContentPane().setLayout(null);
timer =new Timer(100,new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cup2.getLocation().y<250){
cup2.setLocation(400, cup2.getLocation().y+10);
}
else{
timer.stop();
}
}
});
cup1=new JLabel(new ImageIcon("images/cup.jpg"));
cup1.setBounds(150,250,180,250);
cup2=new JLabel(new ImageIcon("images/cup.jpg"));
cup2.setBounds(400,100,180,250);
cup3=new JLabel(new ImageIcon("images/cup.jpg"));
cup3.setBounds(650,250,180,250);
this.getContentPane().add(cup1);
this.getContentPane().add(cup2);
this.getContentPane().add(cup3);
ball=new JLabel(new ImageIcon("images/ball.jpg"));
ball.setBounds(450,380,100,110);
this.getContentPane().add(ball);
this.setVisible(true);
}
class demo extends MouseAdapter{
public void mouseClicked(MouseEvent e){
timer.start();
}
}
public static void main(String[] args)
{
new ball();
}
}
我需要的是要让图片动起来的 老师说的是什么水平移动 我不知道写哪里 怎么写 有些写的都是老师指点的 还有怎么加那个让球随机放杯子下面那个 还有在主页怎样写提示 点击空白处开始的 展开
2个回答
展开全部
java让图片水平移动,就是不断监听图片的位置,示例如下:
//给个实例
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageMove {
static int x,y;
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);//这个要设置成 null
//图标
Icon icon = new ImageIcon(new URL("https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif"));
// Icon icon = new ImageIcon("c:/logo-zhidao.gif");//本地图片文件
JLabel l = new JLabel(icon);
l.setSize(icon.getIconWidth(),icon.getIconHeight()); l.setBorder(BorderFactory.createLineBorder(Color.red));
f.getContentPane().add(l); f.setVisible(true);
l.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e){ x=e.getX();y=e.getY(); } });
l.addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e) { JLabel l = (JLabel)e.getSource();
l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);
}
public void mouseMoved(MouseEvent e) {} }); } }
展开全部
ball t=new ball();
for (int i = 150; i > 0; i =i-10) {
t.cup1.setBounds(i,250,180,250);
Thread.sleep(1000);
if(i<=10){
i=650;
}
}
一直移动 其他的你可以看看代码里面为什么要这么写,里面的参数有什么意义。
for (int i = 150; i > 0; i =i-10) {
t.cup1.setBounds(i,250,180,250);
Thread.sleep(1000);
if(i<=10){
i=650;
}
}
一直移动 其他的你可以看看代码里面为什么要这么写,里面的参数有什么意义。
追问
我要的是让图片的个杯子也动 在一定时间内停下 然后鼠标一点 球就在下面
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询