Right Clicking on JButton

后端 未结 2 1089
名媛妹妹
名媛妹妹 2020-11-30 15:42

trying to add a mouseAdapter to a JButton for a right click to flag the cell. Problem is when I instantiate it onto the button, it won\'t let me. Maybe because it already

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 15:59

    MouseAdapter is an abstract class and you cannot create instances of it. That is why you get the error.

    button[r][c].addMouseListener (new MouseAdapter());  //  this will not work
    
    button[r][c].addMouseListener (new MouseAdapter(){});  // this will
                                                     ^
    
    button[r][c].addMouseListener (new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
             // and this will actually do sth. ; )
        }
    });
    

提交回复
热议问题