我扩展了一个JButton类来创建一个新的类Btn。这样做是为了存储稍后需要的相应x和y坐标。我的问题是当通过mouseEntered事件按下按钮时如何获得X和Y坐标?
public class Btn extends JButton{
private int x,y;
private JButton button;
public Btn(int x, int y,JButton button){
this.x = x;
this.y = y;
this.button = buttton;
}
public int getBtnX{return x}
public int getBtnY{return y}
public JButton getBtnY{return button}
}
public class ButtonBox extends JFrame implements MouseListener{
Btn buttons[][] = new Btn[10][10];
private JButton initBtn(){
// set up button
}
@Override
public void mouseEntered(MouseEvent e) {
((Btn) e.getSource()).getBtnX // the x and y are need to get the coordiante to the button on the grid
}
private setUpBtnBox(){
for(int i=0;i<buttons.length-1;i++ ){
for(int j=0;j<buttons.length-1;j++){
button[][] = new Btn(i,j,initBtn());
}
}
}
}
我尝试了((Btn) e.getSource()).getBtnX
和((JButton) e.getSource()).getBtnX
都无济于事。有没有办法使用这种方法获得x和y?