如何将图像从一个标签拖放到另一个标签?
How to drag and drop an image from label to label?
我在 java.I 中尝试使用 DnD 尝试将图像从一个标签拖放到另一个标签。第一个标签是源,第二个是目的地。我的麻烦是我需要从源中拖动图像并识别我正在拖放到正确的目的地;如果目标是正确的,来自源的图像必须消失,否则必须返回到源并使用 window 消息或只是 System.out.println()
通知用户。我试过使用 TransferHandler
、DragSource
,但我没有得到一个好的结果。
如何将图像从一个标签拖放到另一个标签?
Drag Listener
public class DragMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
}
包含图像的来源标签
public ShipsGUI() {
// setBorder(new EmptyBorder(10,10,10,10));
setLayout(new GridLayout(2, 5));
MouseListener listener = new DragMouseAdapter();
for (int i = 0; i < 10; i++) {
JPanel p = new JPanel(new BorderLayout(5, 0));
JLabel a = new JLabel(ship,JLabel.CENTER);
a.setName("ship");
JLabel n = new JLabel("[" + Integer.toString(i + 1) + "]");
n.setForeground(Color.BLUE);
// a.setBorderPainted(false);
// a.setBackground(Color.white);
// a.setOpaque(true);
//a.setIcon(ship,JLabel.CENTER);
a.setTransferHandler(new TransferHandler("icon"));
a.addMouseListener(listener);
p.add(a);
p.add(n, BorderLayout.LINE_START);
add(p);
}
}
目的地(它是标签的网格)
public NewAreaGioco(int r,int c, boolean enable){
this.setLayout(new GridLayout(r,c,1,1));
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
JLabel l= new JLabel(" ");
l.setSize(30, 30);
l.setBorder(BorderFactory.createLineBorder(Color.BLUE));
if(enable)l.setTransferHandler(new TransferHandler("icon"));
add(l);
}//fine for
}//fine for
}
嗯,您不能使用默认的 TransferHandler class。你需要自己做。
我将从查看 Drag and Drop 教程示例页面上的 DropDemo
和 ListTransferHandler
class 开始。
我认为您需要进行一些更改:
导出图像而不是文本。我认为上面 Sergiy 提供的 link 可能会有所帮助。
重点在exportDone(...)
方法。您的清理代码会将源组件的图标设置为空。
您可能需要阅读教程才能理解这两个 classes 的概念。
我在 java.I 中尝试使用 DnD 尝试将图像从一个标签拖放到另一个标签。第一个标签是源,第二个是目的地。我的麻烦是我需要从源中拖动图像并识别我正在拖放到正确的目的地;如果目标是正确的,来自源的图像必须消失,否则必须返回到源并使用 window 消息或只是 System.out.println()
通知用户。我试过使用 TransferHandler
、DragSource
,但我没有得到一个好的结果。
如何将图像从一个标签拖放到另一个标签?
Drag Listener
public class DragMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
}
包含图像的来源标签
public ShipsGUI() {
// setBorder(new EmptyBorder(10,10,10,10));
setLayout(new GridLayout(2, 5));
MouseListener listener = new DragMouseAdapter();
for (int i = 0; i < 10; i++) {
JPanel p = new JPanel(new BorderLayout(5, 0));
JLabel a = new JLabel(ship,JLabel.CENTER);
a.setName("ship");
JLabel n = new JLabel("[" + Integer.toString(i + 1) + "]");
n.setForeground(Color.BLUE);
// a.setBorderPainted(false);
// a.setBackground(Color.white);
// a.setOpaque(true);
//a.setIcon(ship,JLabel.CENTER);
a.setTransferHandler(new TransferHandler("icon"));
a.addMouseListener(listener);
p.add(a);
p.add(n, BorderLayout.LINE_START);
add(p);
}
}
目的地(它是标签的网格)
public NewAreaGioco(int r,int c, boolean enable){
this.setLayout(new GridLayout(r,c,1,1));
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
JLabel l= new JLabel(" ");
l.setSize(30, 30);
l.setBorder(BorderFactory.createLineBorder(Color.BLUE));
if(enable)l.setTransferHandler(new TransferHandler("icon"));
add(l);
}//fine for
}//fine for
}
嗯,您不能使用默认的 TransferHandler class。你需要自己做。
我将从查看 Drag and Drop 教程示例页面上的 DropDemo
和 ListTransferHandler
class 开始。
我认为您需要进行一些更改:
导出图像而不是文本。我认为上面 Sergiy 提供的 link 可能会有所帮助。
重点在
exportDone(...)
方法。您的清理代码会将源组件的图标设置为空。
您可能需要阅读教程才能理解这两个 classes 的概念。