简体   繁体   中英

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. I copied a for loop to draw circles. 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

This is my MouseEvent system I used:

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:

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,

this.addMouseListener(this);

You really should respect the Java naming conventions, too. Methods start with a lowercase charater. And you shouldn't use paint() to draw on Swing components. Read the tutorial .

Here's a tip. Implement your mouseListener in an inner class.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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