以下是我试图实现的,但是在上面提到的线上却给出了错误;这是因为该函数无法获取哪个Component.getName()属于...
字符串名称=新的String();
mntmOneToOne.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent arg0)
{
String name=new String();
int count = arg0.getClickCount();
if (count == 1)
{
Component panel = (Component) arg0.getSource();
System.out.println(panel.getName());
}
}
});
最佳答案
通过MouseEvent的getSource()
方法获取单击的对象。然后调用其getName()
方法。
mntmOneToOne.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
int count = mouseEvent.getClickCount();
if (count == 1) {
Component panel = (Component)mouseEvent.getSource();
name = panel.getName();
}
}
});