我是java的新手。我想通过java swing制作拖放应用程序。其中,将有一些JLabel源(label1,label2,label3)和目标(labelD1,labelD2,labelD3)用于拖放。标签只能拖放一次,如果第二次拖动标签已经拖放到任何目标标签,则应该有警告消息。我试图使用addMouseMotionListener但无法解决它。如果我得到任何建议应该是正确的方法,那将是一个很大的帮助。
我的代码如下:
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.TransferHandler;
public class Editor extends JFrame{
String selectedLable = "";
JLabel label1,label2, label3, labelD1, labelD2, labelD3;
public Editor() {
super("Drag and Drop Image");
label1 = new JLabel("label1");
label2 = new JLabel("label2");
label3 = new JLabel("label3");
labelD1 = new JLabel();
labelD2 = new JLabel();
labelD3 = new JLabel();
label1.setBounds(20, 30, 50, 50);
label2.setBounds(100, 30, 50, 50);
label3.setBounds(180, 30, 50, 50);
labelD1.setBounds(20, 200, 50, 20);
labelD2.setBounds(100, 200, 50, 20);
labelD3.setBounds(180, 200, 50, 20);
labelD1.setBorder(BorderFactory.createLineBorder(Color.black));
labelD2.setBorder(BorderFactory.createLineBorder(Color.black));
labelD3.setBorder(BorderFactory.createLineBorder(Color.black));
MouseListener ml = new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
JComponent jc = (JComponent)e.getSource();
TransferHandler th = jc.getTransferHandler();
th.exportAsDrag(jc, e, TransferHandler.COPY_OR_MOVE);
//can i get the source label and check, is label already in
//global variable,show warning if its dragged and dropped
//once??
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
};
/*
label1.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
// can i set the sourse label to a global variable and
// and check it mousePressed??
}
});
*/
label1.addMouseListener(ml);
label2.addMouseListener(ml);
label3.addMouseListener(ml);
label1.setTransferHandler(new TransferHandler("text"));
label2.setTransferHandler(new TransferHandler("text"));
label3.setTransferHandler(new TransferHandler("text"));
labelD1.setTransferHandler(new TransferHandler("text"));
labelD2.setTransferHandler(new TransferHandler("text"));
labelD3.setTransferHandler(new TransferHandler("text"));
add(label1);
add(label2);
add(label3);
add(labelD1);
add(labelD2);
add(labelD3);
setLayout(null);
setSize(500,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Editor();
}
}
答案 0 :(得分:0)
我尝试使用addMouseMotionListener
也许你可以使用ComponentListener
?你可以听一个componentMoved
事件。然后,您可以从标签中删除DnD功能。
我猜你可以这样做:
JLabel label = (JLabel)event.getSource();
label.setTransferHandler( null );
答案 1 :(得分:0)
我想到的是你应该实现自己的JLabel类(通过扩展原始版本)并添加一个布尔成员来表示它是否先前被拖动过。 然后在侦听器方法中,在实际移动标签之前检查标签的布尔成员是真还是假,并相应地显示警告消息。