java - 将 JLabel 拖到 JFrame 之外

标签 java swing drag-and-drop jframe jlabel

所以我一直在看这个:dragging a jlabel around the screen

有没有办法将 JLabel(或 JLabel 的表示形式)拖到 JFrame 之外?

例如,在 JFrame 之间拖动 JLabel,同时实际“显示”正在拖动的 JLabel(使用 http://docs.oracle.com/javase/tutorial/uiswing/dnd/intro.html )

最佳答案

使用JWindow测试:

//http://stackoverflow.com/questions/4893265/dragging-a-jlabel-around-the-screen
private final JWindow window = new JWindow();
@Override
public void mouseDragged(MouseEvent me) {
  if (dragLabel == null) {
      return;
  }
  int x = me.getPoint().x - dragLabelWidthDiv2;
  int y = me.getPoint().y - dragLabelHeightDiv2;
  //dragLabel.setLocation(x, y);
  window.add(dragLabel);
  window.pack();
  Point pt = new Point(x, y);
  Component c = (Component)me.getSource();
  SwingUtilities.convertPointToScreen(pt, c);
  window.setLocation(pt);
  window.setVisible(true);
  repaint();
}
@Override public void mouseReleased(MouseEvent me) {
  window.setVisible(false);
  //...
}

下面是未经测试的代码: (可能需要一些按摩才能起作用,但这就是要点)

@Override public int getSourceActions(JComponent src) {
  JLabel label = (JLabel)src;
  window.add(label);
  window.pack();
  window.setVisible(true);
  return MOVE;
}
//...
DragSource.getDefaultDragSource().addDragSourceMotionListener(new DragSourceMotionListener() {
  @Override public void dragMouseMoved(DragSourceDragEvent dsde) {
    Point pt = dsde.getLocation();
    pt.translate(5, 5); // offset
    window.setLocation(pt);
  }
});

关于java - 将 JLabel 拖到 JFrame 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11456343/

相关文章:

java - 方法中预期错误]

java - JFrame 调整大小

javascript - 自定义光标,拖放不带库的 HTML 元素

javascript - 如何判断某个元素是否正在使用 jQuery 进行拖动?

jQuery UI 拖放 - 如何在拖动开始时隐藏原始容器?

java - 我怎样才能使这段代码更好

java - 接受/拒绝聊天客户端中的文件传输

java - 如何使用索引将大型 PDF(文档包)拆分为页面 block

java - UI-Update 后 SpringLayout 中组件的宽度等于 0

java - 使用 miglayout 设置宽度