简体   繁体   English

; 使用鼠标侦听器时预期

[英]; 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. 当用户将鼠标悬停在某物上时,但在使用paint方法时,我试图使边框出现。 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. 我是GUI编程的新手,所以希望我不要犯一个错误。 Thanks! 谢谢!

You cannot nest two method in java: 您不能在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 . 通过在Swing中进行自定义绘画的方式,您应该重写paintComponent而不是paint Read more Painting in AWT and Swing 阅读更多AWT和Swing中的绘画

  • you can't to call paint() from AWT/Swing Listener 您不能从AWT / Swing Listener调用paint()

  • this methods is automatically called by override this method for Container 覆盖此方法会自动调用此方法

  • override paintComponent for JPanel instead of paint() 为JPanel覆盖paintComponent而不是paint()

You can't have a method inside of a method in Java. 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; 您不能将paint方法嵌套在mouseEntered方法中。 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!!
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM