简体   繁体   中英

Mouse event e (Jpanel) Java help

I want a MouseListener for a JPanel inside a JTabbedPane in a JFrame...
I want it to print something, whenever the mouse is clicked/pressed.
I've tried looking around but when I try and implement it I got an error on this line:

public class GUI implements ActionListener, MouseListener

And this is the error:

GUI is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener

What does that mean?

PS (the JPanel/tab bit I want it in is called: displayMainPanel)
Thanks a lot.

Well, you have to override mouseExit :

In your GUI class:

@Override
public void mouseExit(MouseEvent e) { /* Do nothing */ }

When all errors are gone. Don't forget to yourPanel.addMouseListener(theMouseListener) :

yourPanel.addMouseListener(theObjectOfTheClassWhichImplementsTheListener);

And then set the focus an the panel, to be sure events will be caught.

yourPanel.requestFocus();
JPanel jPanel = new JPanel();
jPanel.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    System.out.println("mouseClicked");
                }
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    System.out.println("mouseEntered");
                }
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    System.out.println("mouseExited");
                }
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    System.out.println("mousePressed");
                }
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    System.out.println("mouseReleased");
                }
            });

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