1

I am learning java and while doing a MouseListener problem I am facing an error in the class declaration, tried everything known to me please help me out. According to me I have done all the coding correctly, Pasted the code in IDE too but getting the same error.

Thanks

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 public class Sub extends JFrame 
 {
private JPanel mousepanel;
private JLabel statusbar;
public Sub()
{
 super("Mouse Events");
 // we didnt have a FlowLayout as we had in previous programs
 mousepanel = new JPanel();
 mousepanel.setBackground(Color.WHITE);
 add(mousepanel, BorderLayout.CENTER); //BorderLayout used instead of FlowLayout and it will place it in the center of the window.
 statusbar = new JLabel("Default");
 add(statusbar, BorderLayout.SOUTH); // same as above
 thehandler handler = new thehandler();
 mousepanel.addMouseListener(handler);
 mousepanel.addMouseMotionListener(handler);
 private class thehandler implements MouseListener, MouseMotionListener
 {
 public void mouseClicked(MouseEvent event)
 {
 statusbar.setText(String.format("Clicked at %d, %d", event.getX(), event.getY()));
 }
 public void mousePressed(MouseEvent event)
 {
 statusbar.setText("You press down the mouse.");
 }
 public void mouseReleased(MouseEvent event)
 {
 statusbar.setText("You released the mouse.");
 }
 public void mouseEntered(MouseEvent event)
 {
 statusbar.setText("You enetered the mouse panel area.");
 mousepanel.setBackground(Color.PINK);
 }
 public void mouseExited(MouseEvent event)
 {
 statusbar.setText("The mouse has left the window.");
 mousepanel.setBackground(Color.WHITE);
 }
 //these aremouse motion events
 public void mouseDragged(MouseEvent event)
 {
 statusbar.setText("Your are dragging the mouse.");
 }
 public void mouseMoved(MouseEvent event)
 {
 statusbar.setText("You moded the mouse.");
 }
 }
}
 }
mKorbel
110k20 gold badges138 silver badges331 bronze badges
asked Dec 12, 2012 at 21:11
2
  • 1
    Care to elaborate on the error that you're running into? Commented Dec 12, 2012 at 21:16
  • problem has been solved...by the solutions provided below...still thanks for your reply. Commented Dec 14, 2012 at 19:34

3 Answers 3

1

You need to move the thehandler class definition out of the scope of the Sub constructor.

Sidenote: Class names begin with an uppercase letter.

answered Dec 12, 2012 at 21:16
Sign up to request clarification or add additional context in comments.

1 Comment

I have been disabled from posting more questions on this site so please up rate my question...thanks
0

Check your curly brackets and use some IDE it will help you to rectify errors more easily.

answered Dec 18, 2012 at 17:50

Comments

0

Move your class definition outside of the constructor like this:

public Sub() {
 // ... rest of code
}
// ... rest of code
private class thehandler implements MouseListener, MouseMotionListener {
 // ... rest of code
}
answered Dec 12, 2012 at 21:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.