这就是我要做的事情,我想在点击它后按照光标制作一个面板,我最初的想法是通过拖放操作来完成它但问题是必须按下clic对于要拖动的面板,我希望通过单击它来拖动面板而不保持按下clic。有任何想法吗? 谢谢你们:))
public class Main {
public static void main(String[] argv) throws Exception {
JComponent panDrag = new DraggableComponent();
Icon newi = new ImageIcon("src/red.png");
JLabel labelImage = new JLabel(newi);
panDrag.add(labelImage);
JPanel panneau = new JPanel();
panneau.add(panDrag);
JFrame f = new JFrame();
f.add(panneau);
f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class DraggableComponent extends JPanel implements DragGestureListener, DragSourceListener, MouseListener {
DragSource dragSource;
public DraggableComponent() {
dragSource = new DragSource();
dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
this.setBackground(new Color(255, 0, 0));
this.addMouseListener(this);
}
public void dragGestureRecognized(DragGestureEvent evt) {
Transferable t = new StringSelection("aString");
dragSource.startDrag(evt, DragSource.DefaultCopyDrop, t, this);
}
public void dragEnter(DragSourceDragEvent evt) {
System.out.println("enters");
}
final JButton clickTwiceButton = new JButton();
final JButton fireEventButton = new JButton();
public void dragOver(DragSourceDragEvent evt) {
System.out.println("over" + evt.getX() + "-" + evt.getY());
this.setLocation(evt.getX() - 50, evt.getY() - 70);
}
public void dragExit(DragSourceEvent evt) {
System.out.println("leaves");
}
public void dropActionChanged(DragSourceDragEvent evt) {
System.out.println("changes the drag action between copy or move");
}
public void dragDropEnd(DragSourceDropEvent evt) {
System.out.println("finishes or cancels the drag operation");
// Invoking later for no reason, just to simulate your code
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.out.println("sdffdf");
clickTwiceButton.dispatchEvent(new MouseEvent(
fireEventButton,
MouseEvent.MOUSE_CLICKED,
1,
MouseEvent.BUTTON1,
0, 0,
1,
true
));
}
});
}
//******************
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouse clic");
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouse enter");
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouse exit");
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("mouse press");
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouse release");
//MouseEvent press = new MouseEvent(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
//this.mousePressed(press );
}
}
有了这个,我只能拖动面板,让它保持可见,我也需要,但我必须按下我不想要的陈词滥调:(
答案 0 :(得分:1)
我可能更容易为拖动设置自己的自定义侦听器。
您可以从Moving Windows博客中找到的DragListener
开始。
您需要修改代码以处理mouseMoved(...)
事件而不是mouseDragged(...)
事件。你可能需要添加一个"拖动"生成mousePressed(...)
事件时要切换的变量。第一次单击将拖动变量设置为" true"然后第二次点击将变量设置为" false"。然后只有当变量为" true"时才会执行mouseMoved(...)
代码。