我尝试过添加DragDetectListener,但在拖拽过程中好像只能检测到鼠标按下的第一个坐标,之后没有一个Event可以一直追踪鼠标的move事件,DropTarget只能在接收区域内响应。我希望可以跟踪鼠标位置以实现自画图形的拖拽。还有在拖拽过程怎样能实现半透明层。如各位有知道相关的任何解释,请一定赐教。谢谢各位了

解决方案 »

  1.   

    大家帮帮忙,在canvas上添加的监听器竟然没有实现鼠标跟踪的功能,我很怀疑,肯定是我哪里搞不对,我还在努力尝试中,如果各位路过看到可借只手指点迷津
      

  2.   

    使用JLayeredPane不行吗? 晚上回去给你看看
      

  3.   

    我用的eclipse,你说的那个是swing中的么?!谢谢谢谢你!终于有个人回话了!!!!!!!
    我等着你的教诲
      

  4.   

    对,Swing中的分层组件。别说的那么严重,教诲就谈不上了
      

  5.   

    public class Drag extends JFrame{

    JButton b = null;

    public Drag(){
    getContentPane().setLayout(null);
    b = new JButton("Test");
    b.setBounds(2, 2, 50, 20);
    getRootPane().getLayeredPane().add(b, JLayeredPane.DEFAULT_LAYER); JPanel panel = new MyPanel();
    panel.setBounds(0, 0, 500, 500);
    panel.setOpaque(false);
    getRootPane().getLayeredPane().add(panel, JLayeredPane.DRAG_LAYER);
    setSize(500, 500);
    }

    public static void main(String args[]) {
    JFrame frm = new Drag();
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.setVisible(true);
    } private class MyPanel extends JPanel implements MouseListener, MouseMotionListener{
    private boolean isPressOrDrag = false;

    private int x;

    private int y;

    private int width;

    private int height;

    public MyPanel() {
    super();
    addMouseListener(this);
    addMouseMotionListener(this);
    } public void mousePressed(MouseEvent e) {
    isPressOrDrag = b.contains(new Point(e.getX() - b.getX(), e.getY() - b.getY()));
    x = b.getX() + b.getWidth() / 2;
    y = b.getY() + b.getHeight() / 2;

    width = b.getWidth();
    height = b.getHeight();
    } public void mouseReleased(MouseEvent e) {
    if(isPressOrDrag){
    isPressOrDrag = false;
    b.setLocation(x, y);
    repaint();
    }
    } public void mouseDragged(MouseEvent e) {
    x = e.getX() - b.getWidth() / 2;
    y = e.getY() - b.getHeight() / 2; printComponent(getGraphics());
    } public void printComponent(Graphics g) {
    if(isPressOrDrag){
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.red);
    g2.drawRect(x, y, width, height);
    }
    super.printComponent(g);
    }
    }
    }
      

  6.   

    一个简单的实现,可能会有错误,自己改改吧。上面的方法不太好。
    还有中就是在拖拽的时候,获取指定区域的Image,设置Image的透明度,再与鼠标图片合并,松开后再换回鼠标的形状。也就是说可以通过自定义鼠标形状实现,自己看看吧