我正在使用TransferHandler将数据从JPanel作为JLabel传递到JTextArea (单击左侧面板中的某处以创建要拖动的JLabel)
数据的传输工作正常,但是我还想“显示” JLabel,因为
它与鼠标指针一起被拖动。
如果您注释掉
dropLabel.setTransferHandler(new TransferHandler("text"));
dropLabel.getTransferHandler().exportAsDrag(dropLabel, e,
TransferHandler.COPY);
您将看到我想要它的外观。(但当然不会传输数据)。
如何既可以进行传输又可以使JLabel跟随鼠标光标?
这是代码:
import java.awt.*;
import java.awt.datatransfer.Transferable;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
public class DragTest extends JFrame implements MouseMotionListener,
MouseListener {
private JPanel leftPanel = new JPanel(null);
private JPanel rightPanel = new JPanel(null);
private JLabel dragLabel = new JLabel("drop");
private final JWindow window = new JWindow();
JLabel dropLabel;
public DragTest() {
this.setLayout(new GridLayout(1, 2));
leftPanel.setBorder(BorderFactory.createLineBorder(Color.black));
rightPanel.setBorder(BorderFactory.createLineBorder(Color.black));
this.add(leftPanel);
this.add(rightPanel);
leftPanel.addMouseListener(this);
leftPanel.addMouseMotionListener(this);
JTextArea area = new JTextArea();
rightPanel.setLayout(new GridLayout(1, 1));
rightPanel.add(area);
dragLabel.setFont(new Font("Serif", Font.BOLD, 48));
}
@Override
public void mousePressed(MouseEvent e) {
dropLabel = new JLabel("drop");
Dimension labelSize = dropLabel.getPreferredSize();
dropLabel.setSize(labelSize);
int x = e.getX() - labelSize.width / 2;
int y = e.getY() - labelSize.height / 2;
dropLabel.setLocation(x, y);
leftPanel.add(dropLabel);
dropLabel.setTransferHandler(new TransferHandler("text"));
dropLabel.getTransferHandler().exportAsDrag(dropLabel, e,
TransferHandler.COPY);
repaint();
}
@Override
public void mouseDragged(MouseEvent me) {
dragLabel = new JLabel("drop");
dragLabel.setFont(new Font("Serif", Font.BOLD, 48));
int x = me.getPoint().x;
int y = me.getPoint().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 mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
// leftPanel.remove(dropLabel);
window.remove(dragLabel);
window.setVisible(false);
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public static void main(String[] args) {
DragTest frame = new DragTest();
frame.setVisible(true);
frame.setSize(600, 400);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
另一个例子:
编辑:修复光标闪烁
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.swing.*;
import javax.swing.text.*;
public class DragTest3 {
public JComponent makeUI() {
DragPanel p1 = new DragPanel();
p1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p1.add(new JLabel(UIManager.getIcon("OptionPane.warningIcon")));
p1.add(new JLabel(UIManager.getIcon("OptionPane.errorIcon")));
p1.add(new JLabel("Label1"));
p1.add(new JLabel("Label2"));
MouseListener handler = new Handler();
p1.addMouseListener(handler);
LabelTransferHandler th = new LabelTransferHandler();
p1.setTransferHandler(th);
JPanel p = new JPanel(new GridLayout(1,2));
p.add(p1);
DragPanel p2 = new DragPanel();
p2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p2.addMouseListener(handler);
p2.setTransferHandler(th);
p.add(p2);
JPanel panel = new JPanel(new GridLayout(2,1));
panel.add(p);
panel.add(new JScrollPane(new JTextArea()));
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new DragTest3().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class DragPanel extends JPanel {
public DragPanel() {
super();
}
public JLabel draggingLabel;
}
class Handler extends MouseAdapter {
@Override public void mousePressed(MouseEvent e) {
DragPanel p = (DragPanel)e.getSource();
Component c = SwingUtilities.getDeepestComponentAt(p, e.getX(), e.getY());
if(c!=null && c instanceof JLabel) {
p.draggingLabel = (JLabel)c;
p.getTransferHandler().exportAsDrag(p, e, TransferHandler.MOVE);
}
}
}
class LabelTransferHandler extends TransferHandler {
private final DataFlavor localObjectFlavor;
private final JLabel label = new JLabel() {
@Override public boolean contains(int x, int y) {
return false;
}
};
private final JWindow window = new JWindow();
public LabelTransferHandler() {
System.out.println("LabelTransferHandler");
localObjectFlavor = new ActivationDataFlavor(
DragPanel.class, DataFlavor.javaJVMLocalObjectMimeType, "JLabel");
window.add(label);
window.setAlwaysOnTop(true);
window.setBackground(new Color(0,true));
DragSource.getDefaultDragSource().addDragSourceMotionListener(
new DragSourceMotionListener() {
@Override public void dragMouseMoved(DragSourceDragEvent dsde) {
Point pt = dsde.getLocation();
pt.translate(5, 5); // offset
window.setLocation(pt);
}
});
}
@Override protected Transferable createTransferable(JComponent c) {
System.out.println("createTransferable");
DragPanel p = (DragPanel)c;
JLabel l = p.draggingLabel;
String text = l.getText();
//TEST
//if(text==null) {
// text = l.getIcon().toString();
//}
//return new StringSelection(text+"\n");
final DataHandler dh = new DataHandler(c, localObjectFlavor.getMimeType());
if(text==null) return dh;
final StringSelection ss = new StringSelection(text+"\n");
return new Transferable() {
@Override public DataFlavor[] getTransferDataFlavors() {
ArrayList<DataFlavor> list = new ArrayList<>();
for(DataFlavor f:ss.getTransferDataFlavors()) {
list.add(f);
}
for(DataFlavor f:dh.getTransferDataFlavors()) {
list.add(f);
}
return list.toArray(dh.getTransferDataFlavors());
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
for (DataFlavor f: getTransferDataFlavors()) {
if (flavor.equals(f)) {
return true;
}
}
return false;
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if(flavor.equals(localObjectFlavor)) {
return dh.getTransferData(flavor);
} else {
return ss.getTransferData(flavor);
}
}
};
}
@Override public boolean canImport(TransferSupport support) {
if(!support.isDrop()) {
return false;
}
return true;
}
@Override public int getSourceActions(JComponent c) {
System.out.println("getSourceActions");
DragPanel p = (DragPanel)c;
label.setIcon(p.draggingLabel.getIcon());
label.setText(p.draggingLabel.getText());
window.pack();
Point pt = p.draggingLabel.getLocation();
SwingUtilities.convertPointToScreen(pt, p);
window.setLocation(pt);
window.setVisible(true);
return MOVE;
}
@Override public boolean importData(TransferSupport support) {
System.out.println("importData");
if(!canImport(support)) return false;
DragPanel target = (DragPanel)support.getComponent();
try {
DragPanel src = (DragPanel)support.getTransferable().getTransferData(localObjectFlavor);
JLabel l = new JLabel();
l.setIcon(src.draggingLabel.getIcon());
l.setText(src.draggingLabel.getText());
target.add(l);
target.revalidate();
return true;
} catch(UnsupportedFlavorException ufe) {
ufe.printStackTrace();
} catch(java.io.IOException ioe) {
ioe.printStackTrace();
}
return false;
}
@Override protected void exportDone(JComponent c, Transferable data, int action) {
System.out.println("exportDone");
DragPanel src = (DragPanel)c;
if(action == TransferHandler.MOVE) {
src.remove(src.draggingLabel);
src.revalidate();
src.repaint();
}
src.draggingLabel = null;
window.setVisible(false);
}
}
问题内容: 我在JLabel中有一个图像。 我想要以下功能。 -我单击JLabel内部的位置(在图像上)。 -按下鼠标按钮,即可更改JLabel中图像的位置。(我将图片拖到JLabel中的不同位置) 好吧,这意味着在许多情况下,图片将被裁剪并且在视野之外。 请告诉我如何实现此功能? 什么是要添加到我的JLabel的正确事件监听器? 问题答案: 这是一个基本的例子… 通过将标签划分为3x3网格来工作
该行为会自动创建事件监听器来处理元素的拖动,可支持鼠标事件和触摸事件。 d3.behavior.drag() 构造一个新的拖拽行为; drag.on(type[, listener]) 注册指定的监听器listener 来接收拖动行为中指定类型type的事件;如果监听器listener 未指定,则为指定的类型type的事件返回当前已注册的监听器(事件类型可能包含命名空间,查看详细参见: dispa
问题内容: 我想通过覆盖JLabel上的鼠标事件来启用JLabel上的拖放功能,但是当我在mousePressed事件中定义拖放时,mouseReleased对该JLabel无效。难道我做错了什么 ? 缩略图是JLabel的数组 运行该程序时,拖放有效,但不会打印“此处释放鼠标”语句。但是,当我从mousePressed()方法中删除负责DND的代码时,将打印“此处释放了鼠标”。 这段代码有什么问
我正在使用本机拖放html5 api开发拖放界面。我们已经在其他部分使用了jQuery拖动,但是它在这个特定部分的表现很差,所以我们使用原始的JavaScript。 本质上,标记看起来像这样... 我为“dragstart”创建一个addEventListener并运行以下函数 它将我的类完美地应用于原始元素,但我似乎不能移动原始元素。浏览器创建克隆/幽灵图像...我看到你可以创建自己的图像显示,
我有一个扩展JPanel的类ExamplePanel,由2个jPanels-LabelSpanel和SubPanel组成。Labelspanel包含任意JLabels,而子面板包含ExamplePanel的不同实例-嵌套实例。 我希望能够将JLabels拖动到嵌套实例中,但当我这样做时,它们最终会被拖到子面板后面。我相信答案在于setComponentZOrder方法,但我没有用它取得任何进展。它
我工作在一个应用程序实现新的拖放角材料CDK和我试图取消拖动事件的元素按下,我的意思是,我开始拖动元素,但如果我按当我拖动元素时,它应该回到我开始拖动它的位置,到目前为止,我还没有找到这样做的方法,有人知道我怎么做吗?在cdk留档里没有关于这个的任何想法。我试着做这样的事情。 样板 Ts组件 但到目前为止还没有成功。