简体   繁体   English

如何让我的 MouseListener 方法在 Java 中工作?

[英]How do I get my MouseListener method to work in Java?

I'm very new to java and I'm making a simple pixel art maker.我是 java 的新手,我正在制作一个简单的像素艺术制作工具。 I copied a for loop to draw circles.我复制了一个 for 循环来绘制圆圈。 I was doing the mouse clicking method.我在做鼠标点击的方法。 I did it so that when I click, it prints "Left" or "Right".我这样做是为了在单击时打印“左”或“右”。

This is the grid system: http://www.java2s.com/Tutorials/Java/Graphics/Shape/Draw_a_grid_by_drawing_lines_in_Java.htm这是网格系统: http ://www.java2s.com/Tutorials/Java/Graphics/Shape/Draw_a_grid_by_drawing_lines_in_Java.htm

This is my MouseEvent system I used:这是我使用的 MouseEvent 系统:

https://github.com/DevonCrawford/A-Pathfinding-Visualization/blob/master/src/Frame.java https://github.com/DevonCrawford/A-Pathfinding-Visualization/blob/master/src/Frame.java

The only problem is that it doesn't print the "Left" and "Right".唯一的问题是它不打印“左”和“右”。 Here is my code:这是我的代码:

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

class Main extends JComponent implements MouseListener {
  public static void main(String[] args) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 600, 584);
    window.getContentPane().add(new Main());
//    window.setResizable(false);
    window.setVisible(true);

  }

  public void paint(Graphics g) {
    int rows = 20;

    int cols = 20;
    int width = getSize().width;
    int height = getSize().height;

    // draw the rows
    int rowHt = height / (rows);
    for (int i = 0; i < rows; i++)
      g.drawLine(0, i * rowHt, width, i * rowHt);

    // draw the columns
    int rowWid = width / (cols);
    for (int i = 0; i < cols; i++)
      g.drawLine(i * rowWid, 0, i * rowWid, height);

  }

  public void MapCalculations(MouseEvent e) { 
    // If left mouse button is clicked
    if (SwingUtilities.isLeftMouseButton(e)) {
        System.out.println("Left");

    } 
    // If right mouse button is clicked
    else if (SwingUtilities.isRightMouseButton(e)) {
        System.out.println("Right");
    }
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    MapCalculations(e);
  }

  @Override
  public void mousePressed(MouseEvent e) {}

  @Override
  public void mouseReleased(MouseEvent e) {}

  @Override
  public void mouseEntered(MouseEvent e) {}

  @Override
  public void mouseExited(MouseEvent e) {}

  public void mouseDragged(MouseEvent e) {
    MapCalculations(e);
  }
}

The mouse listener methods are never being called because the framework doesn't know when to call them.永远不会调用鼠标侦听器方法,因为框架不知道何时调用它们。

Try adding the mouse listener to your window object in the main method as such:尝试在 main 方法中将鼠标侦听器添加到window对象,如下所示:

public static void main(String[] args) {
    JFrame window = new JFrame();
    Main myMain = new Main();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 600, 584);
    window.getContentPane().add(myMain);
//    window.setResizable(false);
    window.setVisible(true);    
    window.addMouseListener(myMain);
  }

The method is never called because you never add a mouse listener to anything.该方法永远不会被调用,因为您永远不会向任何东西添加鼠标侦听器。

You probably want, in the Main constructor,您可能希望在 Main 构造函数中,

this.addMouseListener(this);

You really should respect the Java naming conventions, too.您确实也应该尊重 Java 命名约定。 Methods start with a lowercase charater.方法以小写字符开头。 And you shouldn't use paint() to draw on Swing components.并且您不应该使用paint()来绘制 Swing 组件。 Read the tutorial .阅读教程

Here's a tip.这是一个提示。 Implement your mouseListener in an inner class.在内部类中实现您的 mouseListener。

    class MyMouseListener extends MouseAdapter {
       public void mouseClicked(MouseEvent e) {
         MapCalculations(e);
       }
       public void mouseDragged(MouseEvent e) {
         MapCalculations(e);
       }
    }

Then use an instance of that to process mouse events.然后使用它的一个实例来处理鼠标事件。 The inner class still has access to your enclosing class's values.内部类仍然可以访问您的封闭类的值。

window.addMouseListener(new MyMouseListener());

By doing it this way you don't need to have all those empty methods.通过这种方式,您不需要所有那些空方法。 Also, you can use the same mouse listener class for mouseMotionListener requirements since the MouseAdapter class has empty methods for both interfaces of MouseListener and MouseMotionListener此外,您可以使用相同的鼠标侦听器类来满足mouseMotionListener要求,因为MouseAdapter类对于MouseListenerMouseMotionListener的两个接口都有空方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM