Swing:创建一个可拖动的组件...? [英] Swing: Creating a draggable component...?

查看:122
本文介绍了Swing:创建一个可拖动的组件...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络上搜索了可拖动的Swing组件的示例,
,但我发现了不完整的或不工作的示例。

I searched the web for examples of draggable Swing components, but I found either incomplete or non-working examples.

我需要的是一个 Swing组件,可以通过其他组件中的鼠标
拖动。在被拖动的时候,它应该已经
改变了
的位置,而不仅仅是'跳转'到它的目的地。

What I need is a Swing component that can be dragged by the mouse inside an other component. While being dragged, it should already change its position, not just 'jump' to its destination.

我将会喜欢没有非标准API的工作。

I would appreciate examples which work without non-standard APIs.

谢谢。

推荐答案

我提出了一个简单但工作良好的解决方案,由我自己发现;)

I propose a simple, but well-working solution, found out by myself ;)

我该怎么办?


  • 按下鼠标后,我在屏幕上记录光标的位置
    组件的位置。

  • 拖动鼠标时,我计算新的和
    旧的游标的位置屏幕,然后通过此差异移动
    组件

  • When mouse is pressed, I record the cursor's position on screen, and the component's position.
  • When mouse is dragged, I calculate the difference between new and old cursor's position on screen, and move the component by this difference.

使用最新的JDK 6 unter Linux(OpenSuse,KDE3),

进行测试,但嘿,它的Java Swing应该在任何地方平等工作。

以下代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MyDraggableComponent
    extends JComponent {

  private volatile int screenX = 0;
  private volatile int screenY = 0;
  private volatile int myX = 0;
  private volatile int myY = 0;

  public MyDraggableComponent() {
    setBorder(new LineBorder(Color.BLUE, 3));
    setBackground(Color.WHITE);
    setBounds(0, 0, 100, 100);
    setOpaque(false);

    addMouseListener(new MouseListener() {

      @Override
      public void mouseClicked(MouseEvent e) { }

      @Override
      public void mousePressed(MouseEvent e) {
        screenX = e.getXOnScreen();
        screenY = e.getYOnScreen();

        myX = getX();
        myY = getY();
      }

      @Override
      public void mouseReleased(MouseEvent e) { }

      @Override
      public void mouseEntered(MouseEvent e) { }

      @Override
      public void mouseExited(MouseEvent e) { }

    });
    addMouseMotionListener(new MouseMotionListener() {

      @Override
      public void mouseDragged(MouseEvent e) {
        int deltaX = e.getXOnScreen() - screenX;
        int deltaY = e.getYOnScreen() - screenY;

        setLocation(myX + deltaX, myY + deltaY);
      }

      @Override
      public void mouseMoved(MouseEvent e) { }

    });
  }

}

public class Main {

  public static void main(String[] args) {
    JFrame f = new JFrame("Swing Hello World");

    // by doing this, we prevent Swing from resizing
    // our nice component
    f.setLayout(null);

    MyDraggableComponent mc = new MyDraggableComponent();
    f.add(mc);

    f.setSize(500, 500);

    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setVisible(true);
  }

}

这篇关于Swing:创建一个可拖动的组件...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆