开发者

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 开发者_Go百科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?

P.S (the JPanel/tab bit I want it in is called: displayMainPanel)

Thanks a lot.


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");
                }
            });


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜