gpt4 book ai didi

java - 单击时画一个点

转载 作者:行者123 更新时间:2023-12-01 17:27:37 25 4
gpt4 key购买 nike

您好,我想知道如何在 MouseClicked 方法之外获取鼠标的位置我需要在有人点击的地方画一个点。这是我现在使用的代码。有人可以帮我解决这个问题吗?

    Dot punt = new Dot();

public void paintComponent(Graphics g) {
super.paintComponent(g);

punt.setDot(g, getX(), getY(), 5);

repaint();
}

@Override
public void mouseClicked(MouseEvent m) {
// TODO Auto-generated method stub
gebeurtenisverslag.setText(
m.getClickCount() + " keer geklikt op " + m.getX() + "," + m.getY());
}

最佳答案

为 mouseClicked 修改的类设置一个变量。

private int X;
private int Y;

@Override
public void mouseClicked(MouseEvent m) {
gebeurtenisverslag.setText(
m.getClickCount() + " keer geklikt op " + m.getX() + "," + m.getY());
X = m.getX();
Y = m.getY();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
//Probably want to add logic here to verify X & Y have been set
punt.setDot(g, X, Y, 5);
repaint();
}

关于java - 单击时画一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13708141/

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