gpt4 book ai didi

java - 让 JPanel 只需单击鼠标即可跟随光标

转载 作者:行者123 更新时间:2023-12-01 12:20:34 26 4
gpt4 key购买 nike

这就是我想要做的,我想让一个面板在单击它后跟随光标,我最初的想法是通过拖放操作来完成它,但问题是必须按下 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 );

}
}

有了这个,我只能拖动面板并使其保持可见,这也是我需要的,但我仍然必须按住我不想要的 clic :(

最佳答案

我可能会更容易为您自己的拖动监听器。

您可以从 Moving Windows 中找到的 DragListener 开始博客。

您需要修改代码来处理 mouseMoved(...) 事件而不是 mouseDragged(...) 事件。您可能需要添加一个“拖动”变量,以便在生成 mousePressed(...) 事件时切换该变量。第一次单击会将拖动变量设置为“true”,第二次单击会将变量设置为“false”。那么 mouseMoved(...) 代码只会在变量为“true”时执行。

关于java - 让 JPanel 只需单击鼠标即可跟随光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26691469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com