简体   繁体   中英

; expected when using a Mouse Listener

I am trying to make a border appear when a user hovers their mouse over something, but when I use the paint Method. It says

Syntax Error on Token "(" ; expected and 
Syntax Error on Token ")" ; expected

My code is:

JLabel lblAllOrNothing = new JLabel("All Or Nothing (4 BP)");
lblAllOrNothing.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent arg0) {
        public void paint(Graphics g) { //Error is this line
            g.drawRect(0, 72, 256, 72);
        }
    }
});              

I just don't where else I can put a semi-colen. I am new to GUI programming, so I hope I did not make too bad of a mistake. Thanks!

You cannot nest two method in java:

public void mouseEntered(MouseEvent arg0) {
            public void paint(Graphics g) { //Method inside a method is not allowed

You have a syntax problem.

JLabel lblAllOrNothing = new JLabel("All Or Nothing (4 BP)");
lblAllOrNothing.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // You can't define a method inside a method!!
    }
});

By the way to make custom painting in Swing you should override paintComponent instead of paint . Read more Painting in AWT and Swing

  • you can't to call paint() from AWT/Swing Listener

  • this methods is automatically called by override this method for Container

  • override paintComponent for JPanel instead of paint()

You can't have a method inside of a method in Java.

Instead, do this

JLabel lblAllOrNothing = new JLabel("All Or Nothing (4 BP)");
    lblAllOrNothing.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent arg0) {
            //call the paint method here
        }

        //Move this method here
        public void paint(Graphics g) { //Error is this line
            g.drawRect(0, 72, 256, 72);
        }
    });

Note that this will not actually work functionally, but it illustrates why you're getting a syntax error

You can't nest a paint method in your mouseEntered method; perhaps you just want

JLabel lblAllOrNothing = new JLabel("All Or Nothing (4 BP)");
lblAllOrNothing.addMouseListener(new java.awt.event.MouseAdapter() {
  @Override
  public void mouseEntered(MouseEvent e) {
    // public void paint(Graphics g) { 
    // g.drawRect(0, 72, 256, 72); }
    java.awt.Component c = e.getComponent();
    c.getGraphics().drawRect(0, 72, 256, 72);
  }
});

您不能在Java中将一个方法放到另一个方法中,使draw方法成为侦听器,您应该做的只是在listener方法中调用draw方法:

this

new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // You can't define a method inside a method!!
    }
};

is an anonymous class. now when you use it as an statement it will be used as };, and return an object, while when you use it as pass the object as a parameter the will use it as

fun(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // You can't define a method inside a method!!
    }
});

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