US>To display mouse-pressed coordinates on the screen

Asked 2 years ago, Updated 2 years ago, 34 views

After a screen is generated by using swing, coordinates pushed by the mouse are displayed by using mouse event processing.For that reason, I understand that it can be realized using the MouseListener interface and addMouseListener(), but I can't proceed even if I refer to tutorials.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
/*
a program that generates a screen and displays its coordinates when the mouse is pressed in the screen
*/
public class Event extensions JFrame implements MouseListener {

    intx = 0;
    inty=0;// variable for mouse coordinate recording
    public static void main(String[]args) {
        SwingUtilities.invokeLater(()->{
            createAndShowGUI();
            Event m = new Event();
        });
    }

    private static void createAndShowGUI(){
        JFrame frame = new JFrame("window");
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public Event() {
        addMouseListener(this);
    }

    @ Override
    public void mouseClicked (MouseEvente) {
        Point point = e.getPoint();
        x = point.x;
        y = point.y;
        System.out.println("x:"+x+"y:"+y);
    }

    @ Override
    public void mouseExited (MouseEvente){}
    @ Override
    public void mouseEntered (MouseEvente){}
    @ Override
    public void mousePressed (MouseEvente) {}
    @ Override
    public void mouseReleased (MouseEvente){}
}

java

2022-09-30 20:21

2 Answers

When I woke up and thought about it for a few hours, I solved myself.Here's the source code

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
/*
a program that generates a screen and displays its coordinates when the mouse is pressed in the screen
*/
public class Event extensions JFrame implements MouseListener {

    intx = 0;
    inty=0;// variable for mouse coordinate recording

    public static void main(String[]args) {
        SwingUtilities.invokeLater(()->{
            Event m = new Event();
            JFrame frame = new JFrame("window");
            frame.setSize(300,300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.addMouseListener(m);
        });
    }

    @ Override
    public void mouseClicked (MouseEvente) {
        Point point = e.getPoint();
        x = point.x;
        y = point.y;
        System.out.println("x:"+x+"y:"+y);
    }

    @ Override
    public void mouseExited (MouseEvente){}
    @ Override
    public void mouseEntered (MouseEvente){}
    @ Override
    public void mousePressed (MouseEvente) {}
    @ Override
    public void mouseReleased (MouseEvente){}
}


2022-09-30 20:21

There is no particular problem because it is working, but if you correct it a little bit, it will look like the following.
Since the original code inherits JFrame, there is no need to create a separate instance of JFrame.The event itself has all the features of JFrame, so you can do the following to display the window:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
/*
* a program that generates a screen and displays its coordinates when the mouse is pressed in the screen
* */
public class Event extensions JFrame implements MouseListener {

  intx = 0;
  inty=0;// variable for mouse coordinate recording

  public static void main(String[]args) {
    SwingUtilities.invokeLater(()->{
      Event frame = new Event();
      frame.setVisible(true);
    });
  }

  public Event() {
    super("window");
    setSize (300,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addMouseListener(this);
  }

  @ Override
  public void mouseClicked (MouseEvente) {
    Point point = e.getPoint();
    x = point.x;
    y = point.y;
    System.out.println("x:"+x+", y:"+y);
  }

  @ Override
  public void mouseExited (MouseEvente){}
  @ Override
  public void mouseEntered (MouseEvente){}
  @ Override
  public void mousePressed (MouseEvente) {}
  @ Override
  public void mouseReleased (MouseEvente){}
}

Alternatively, if you want JFrame functionality and mouse event functionality to be independent, Mevent does not need to inherit JFrame.


2022-09-30 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.