Swing:创建一个可拖动的组件......?

时间:2009-05-17 10:56:59

标签: java swing drag-and-drop draggable

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

我需要的是 Swing组件,可以通过鼠标拖动 在另一个组件内。被拖动时,它应该已经了 改变它的位置,而不只是“跳”到目的地。

我希望能够使用没有非标准API的示例。

谢谢。

3 个答案:

答案 0 :(得分:32)

我提出了一个简单但运作良好的解决方案,由我自己找到;)

我该怎么办?

  • 按下鼠标时,我会在屏幕上记录光标的位置,并且 组件的位置。
  • 拖动鼠标时,我会计算new和之间的差异光标位置在屏幕上,然后移动 组件由此差异

使用最新的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);
  }

}

答案 1 :(得分:1)

另外,我发现可以在JFrame中创建 JInternalFrame , 但问题是:你总是得到一个恼人的窗口标题栏

要禁用标题栏,遗憾的是,必须使用脏的解决方法

public class MyDraggableComponent extends JInternalFrame {

  public MyDraggableComponent() {
    InternalFrameUI thisUI = getUI();
    if (thisUI instanceof BasicInternalFrameUI) {
      ((BasicInternalFrameUI) thisUI).setNorthPane(null);
    }

}

我真的很想念像“someInternalFrame.setWindowTitleBar(false)”这样的方法...... :'(

答案 2 :(得分:1)

这是您可能想要尝试的另一种方法。我觉得它很干净。只需复制以下类并使用它:

用法:

DragListener dl = new DragListener(componentOrWindowToBeMoved); dl.addHandle(componentToPickWithTheMouse);

类别:

import java.awt.Component;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DragListener extends MouseAdapter {

    private final Component COMPONENT_TO_DRAG;
    private final int MOUSE_BUTTON;
    private Point mousePosition;
    private Point sourceLocation;
    private Point locationOnScreen;
    private int buttonPressed;

    public DragListener(final Component componentToDrag) {
         this(componentToDrag, MouseEvent.BUTTON1);
    }

    public DragListener(final Component componentToDrag, final int mouseButton)                         {
        this.COMPONENT_TO_DRAG = componentToDrag;
        this.MOUSE_BUTTON = mouseButton;
    }

    @Override
    public void mousePressed(final MouseEvent e) {
        this.buttonPressed = e.getButton();
        this.mousePosition = MouseInfo.getPointerInfo().getLocation();
        this.sourceLocation = new Point();
    }

    @Override
    public void mouseDragged(final MouseEvent e) {
        if (this.buttonPressed == MOUSE_BUTTON) {
            this.locationOnScreen = e.getLocationOnScreen();
            this.sourceLocation = this.COMPONENT_TO_DRAG.getLocation(this.sourceLocation);
            this.sourceLocation.translate((int) (this.locationOnScreen.getX() - this.mousePosition.getX()), (int) (this.locationOnScreen.getY() - this.mousePosition.getY()));
            this.COMPONENT_TO_DRAG.setLocation(this.sourceLocation);
            this.mousePosition = MouseInfo.getPointerInfo().getLocation();
        }
    }

    public void addHandle(Component handle) {
        handle.addMouseListener(this);
        handle.addMouseMotionListener(this);
    }
}