当前位置: 代码迷 >> Java相关 >> java中怎样实现图片拖动
  详细解决方案

java中怎样实现图片拖动

热度:200   发布时间:2013-03-20 19:35:21.0
java中怎样实现图片拖动
java中怎样实现图片的拖动,我的代码如下,我的问题是怎样让图片整个显示,我的代码的问题是图片拖动后只能显示图片的一部分,求高手指点:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class DragTest_02 extends JFrame  {
    Point pStart = new Point();
    Point pEnd = new Point();
    Icon img = new ImageIcon(this.getClass().getResource("/img/jinan.jpg"));
    JLabel JL = new JLabel(img);   
    JPanel p = new JPanel();

    public DragTest_02() {
        setBounds(300, 200, 500, 500);
        setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(null);
        add(JL);
        add(JL);
        JL.setBounds(10, 10, 80, 100);
        JL.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                pStart.x = JL.getX();
                pStart.y = JL.getY();
            }
        });
        JL.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                pEnd.x = e.getX();
                pEnd.y = e.getY();
                JL.setLocation(pEnd);
            }
        });
        JL.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                pEnd.x = e.getX();
                pEnd.y = e.getY();
                JL.setLocation(pEnd);
            }
        });

        setVisible(true);
    }

    public static void main(String[] args) {
        new DragTest_02();
    }
}
搜索更多相关的解决方案: public  图片  import  

----------------解决方案--------------------------------------------------------
程序代码:
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class DragListener extends JFrame implements MouseMotionListener,MouseListener{

    /**
     *
@param args
     
*/
    Point p1=null;
    Point p=null;
    Point p2=null;
    JLabel jl=new JLabel(new ImageIcon("9.jpg"));
    JLabel jl2=new JLabel(new ImageIcon("9.jpg"));
    boolean flagjl1=false;
    boolean flagjl2=false;
    DragListener(){
        this.setTitle("DragListener");
        this.setBounds(200, 200, 400, 400);
        this.setLayout(null);
        jl.setBounds(20, 20, 50, 50);
        jl.addMouseListener(this);
        jl.addMouseMotionListener(this);
        jl2.setBounds(70, 70, 70,70);
        jl2.addMouseListener(this);
        jl2.addMouseMotionListener(this);
        this.add(jl2);
        this.add(jl);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new DragListener();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
      

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getComponent().equals(jl)){
            flagjl1=true;
        }else if(e.getComponent().equals(jl2)){
            flagjl2=true;
        }
        p=this.jl.getMousePosition();
        p2=this.jl2.getMousePosition();
        p1=this.getLocationOnScreen();
//        System.out.println(p);
      

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        flagjl1=false;
        flagjl2=false;
      

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
      

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
      

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if(flagjl1){
            this.jl.setLocation(e.getXOnScreen()-p.x-p1.x, e.getYOnScreen()-p.y-p1.y);
        }else if(flagjl2){
            this.jl2.setLocation(e.getXOnScreen()-p2.x-p1.x, e.getYOnScreen()-p2.y-p1.y);
        }
      

      

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
      

    }

}

    这个是可以拖动两张图片的  以前写的 ...

----------------解决方案--------------------------------------------------------
我运行了一下,根本就不显示图片啊…………
----------------解决方案--------------------------------------------------------
回复 3楼 lovefan
你放了图片在你的工程里面吗?
----------------解决方案--------------------------------------------------------
我发现问题所在了,虽然没有解决我的问题,但还是感谢您,我的解决方法也分享一下,解决方案如下:
将原先的程序段:
JLabel jl=new JLabel(new ImageIcon("/img/image1.jpg"));
JLabel jl2=new JLabel(new ImageIcon("/img/jinan.jpg"));
改为如下程序:
Icon image1 = new ImageIcon(this.getClass().getResource("/img/img.jpg"));
Icon image2 = new ImageIcon(this.getClass().getResource("/img/jinan.jpg"));
JLabel jl=new JLabel(image1);
JLabel jl2=new JLabel(image2);
就可以看到图片了,可以拖动,不过只能看到图片的一部分……
----------------解决方案--------------------------------------------------------
  相关解决方案