gpt4 book ai didi

调用frame.setLocation() 时会触发Java mouseExited 事件

转载 作者:行者123 更新时间:2023-11-30 04:04:35 26 4
gpt4 key购买 nike

我有一个没有装饰(没有标题栏、关闭按钮等)的 JFrame,我可以使用 setLocation() 和鼠标位置在屏幕上拖动它。

不幸的是,mouseExited 事件在第一次移动窗口时被调用...

  1. 将鼠标移入窗口并触发 mouseEntered 事件
  2. 单击鼠标并触发 mousePressed 事件。
  3. 拖动鼠标并触发 mouseDragged 事件,并调用 setLocation。
  4. 即使鼠标仍在窗口中,也会触发 mouseExited 事件!
  5. 此时将鼠标移出窗口不会触发 mouseExited。
  6. 将鼠标移出并移回将重置回第 1 步。

除了手动测试屏幕上的鼠标位置之外,如何解决此问题?

编辑:这是代码的精简版本

import java.awt.*;
import javax.swing.*;
import java.awt.Event;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;

class DragNDropper implements MouseMotionListener, MouseListener
{
private int x, y;
private int dx, dy;

private boolean clicked;

private JFrame frame;

public DragNDropper(JFrame frame)
{
dx = dy = 0;
this.frame = frame;
}

public void mouseDragged(MouseEvent e)
{
x = e.getXOnScreen();
y = e.getYOnScreen();

frame.setLocation(x-dx, y-dy);
}

public void mouseMoved(MouseEvent e)
{
x = e.getXOnScreen();
y = e.getYOnScreen();
}

public void mouseClicked(MouseEvent e)
{

}

public void mousePressed(MouseEvent e)
{
clicked = true;
dx = e.getX();
dy = e.getY();
}

public void mouseReleased(MouseEvent e)
{
clicked = false;
}

public void mouseEntered(MouseEvent e)
{
System.out.println("Mouse entered");
}


public void mouseExited(MouseEvent e)
{
System.out.println("Mouse exited");
}
}


public class Program
{
public static void main(String[] argv)
{
JFrame jf = new JFrame();
DragNDropper dnd = new DragNDropper(jf);

jf.setSize(new Dimension(512, 512));
jf.addMouseListener(dnd);
jf.addMouseMotionListener(dnd);

jf.show();
}
}

最佳答案

使用此代码一切正常。请停止使用frame.show(),从那里您知道它被用来显示JFrame,请使用frame.setVisible(true) 代替。请多解释一下这个问题。请看一下代码的修改:

import java.awt.*;
import java.awt.Event;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;

public class DragNDropper implements MouseMotionListener, MouseListener {

private int x, y;
private int dx, dy;
private boolean clicked;
private JFrame jf;

public static void main(String[] argv) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new DragNDropper().displayGUI();
}
});
}

private void displayGUI() {
dx = dy = 0;
jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setUndecorated(true);

jf.setSize(new Dimension(512, 512));
jf.addMouseListener(this);
jf.addMouseMotionListener(this);

jf.setVisible(true);
}

public void mouseDragged(MouseEvent e) {
x = e.getXOnScreen();
y = e.getYOnScreen();
jf.setLocation(x-dx, y-dy);
}

public void mouseMoved(MouseEvent e) {
x = e.getXOnScreen();
y = e.getYOnScreen();
}

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
clicked = true;
dx = e.getX();
dy = e.getY();
}

public void mouseReleased(MouseEvent e) {
clicked = false;
}

public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered");
}

public void mouseExited(MouseEvent e) {
System.out.println("Mouse exited");
}
}

关于调用frame.setLocation() 时会触发Java mouseExited 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21071412/

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