框架上的鼠标侦听器

发布于 2024-11-13 08:17:14 字数 1769 浏览 2 评论 0原文

你好 我正在尝试向我的框架添加一个鼠标侦听器以获取鼠标单击的位置并检查它是否在圆圈内,问题是它没有触发

public class CircleDraw extends Frame implements MouseListener {

static int circles = 0;
private double color;
double mousex = 0;
double mousey = 0;
int score;

public void mouseClicked(MouseEvent evt)
{
         mousex = evt.getX();
         mousey = evt.getY();
}
public void mouseEntered (MouseEvent me) {}
public void mousePressed (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}

public void paint(Graphics g) {
    try {
                this.addMouseListener(this);
      while (circles < 20) {
        color = 10*Math.random();
        Shape circle = new Ellipse2D.Double(900*Math.random(),900*Math.random(), 50.0f,      50.0f);
        Graphics2D ga = (Graphics2D)g;
        ga.draw(circle);
        if(color >2)
            ga.setPaint(Color.green);
        else
            ga.setPaint(Color.BLACK);

        ga.fill(circle);

        if(circle.contains(mousex, mousey) && color > 2)
                score ++;
        else
            if(circle.contains(mousex, mousey) && color < 2)
                score--;
        Thread.sleep(1000);

        System.out.println(circles);
        System.out.println(mousex);
        System.out.println(mousey);

        circles ++;
        ga.setPaint(Color.white);
        ga.fill(circle);
    }
                System.exit(0);
 } catch (InterruptedException e) {
    e.printStackTrace();
 } 
  }

  public static void main(String args[]) {

  Frame frame = new CircleDraw();

 frame.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
 System.exit(0);
 }
 });
 frame.setSize(1000, 1000);
 frame.setVisible(true);

 }}

Hi
i am trying to add a mouse listener to my frame to get the position of the mouse clicked and check if it is inside the circle, the problem is that it is not triggering

public class CircleDraw extends Frame implements MouseListener {

static int circles = 0;
private double color;
double mousex = 0;
double mousey = 0;
int score;

public void mouseClicked(MouseEvent evt)
{
         mousex = evt.getX();
         mousey = evt.getY();
}
public void mouseEntered (MouseEvent me) {}
public void mousePressed (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}

public void paint(Graphics g) {
    try {
                this.addMouseListener(this);
      while (circles < 20) {
        color = 10*Math.random();
        Shape circle = new Ellipse2D.Double(900*Math.random(),900*Math.random(), 50.0f,      50.0f);
        Graphics2D ga = (Graphics2D)g;
        ga.draw(circle);
        if(color >2)
            ga.setPaint(Color.green);
        else
            ga.setPaint(Color.BLACK);

        ga.fill(circle);

        if(circle.contains(mousex, mousey) && color > 2)
                score ++;
        else
            if(circle.contains(mousex, mousey) && color < 2)
                score--;
        Thread.sleep(1000);

        System.out.println(circles);
        System.out.println(mousex);
        System.out.println(mousey);

        circles ++;
        ga.setPaint(Color.white);
        ga.fill(circle);
    }
                System.exit(0);
 } catch (InterruptedException e) {
    e.printStackTrace();
 } 
  }

  public static void main(String args[]) {

  Frame frame = new CircleDraw();

 frame.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
 System.exit(0);
 }
 });
 frame.setSize(1000, 1000);
 frame.setVisible(true);

 }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洒一地阳光 2024-11-20 08:17:14

在 Paint() 方法中添加鼠标侦听器是致命的,因为该方法被非常频繁地调用(每次重绘),并且添加了如此多的侦听器(每次重绘)。

您应该将侦听器添加到内容面板而不是 JFrame 本身。这样就可以了。您可以在类的构造函数中执行此操作:

public CircleDraw() {
    this.getContentPane().addMouseListener(this);
}

我认为这不会完全解决您的问题,因为当您的绘制方法处于活动状态时您不会单击鼠标。您的代码设计(尤其是 while 循环)没有给其他事件任何时间来触发。因此 mouseclick 事件将在 20 次循环后处理。您可以通过添加

public void mouseClicked(MouseEvent evt) {
    mousex = evt.getX();
    mousey = evt.getY();
    System.out.println("X: "+mousex+"/ Y: "+mousey);
}

到代码中来检查这一点。您必须在不同的线程中运行 GUI(例如,使用 SwingUtilities 和 Runnable())。我推荐你看一本关于JAVA开发的好书。或者您可以从这个等在线教程开始。

恕我直言,您不应该尝试使用 awt,而是使用 SWING 或 SWT 进行 GUI 设计,因为这更舒适。

It is deadly to add your mouselistener in the paint() method, since this method is called very very often (with each repaint), and so many listeners are added (with each repaint).

You should add the listener to your content-panel and not to the JFrame itself. This will do it. You can do this in the constructor of your class:

public CircleDraw() {
    this.getContentPane().addMouseListener(this);
}

This won't solve your problem completely I think, since you won't get your mouseclick while your paint-method is active. Your code-design (especially your while-loop) does not give any time to other events to fire. So the mouseclick-event will be handled after your 20 loops. You can check this by adding

public void mouseClicked(MouseEvent evt) {
    mousex = evt.getX();
    mousey = evt.getY();
    System.out.println("X: "+mousex+"/ Y: "+mousey);
}

to your code. You have to run your GUI in a different thread (e.g. use SwingUtilities and a Runnable() therefor). I recommend you to get a good book on JAVA development. Or you can start with online-tutorials like this one.

IMHO you should not try to deal with awt, instead use SWING or SWT for GUI-design, since this is much more compfortable.

听闻余生 2024-11-20 08:17:14

在构造函数中添加监听器,paint被重复调用

add the listener in the constructor, the paint is called repeatedly

伊面 2024-11-20 08:17:14

以下是我在该源中看到的一些问题:

  1. paint() 中添加侦听器
  2. paint() 方法中调用 wait()
  3. paint() 方法中调用 System.exit()(严格来说不是问题,但非常不寻常)。
  4. 格式很差并且难以理解
  5. 调用已弃用的方法。
  6. AWT 中的代码错千年了。

Here are some of the problems I see with that source:

  1. Adds the listener in paint()
  2. Calls wait() within the paint() method.
  3. Calls System.exit() within the paint() method (not strictly a problem, but very unusual).
  4. Is poorly formatted and hard to understand
  5. Calls deprecated methods.
  6. Codes in AWT in the wrong millennium.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文