不一定,
如果
class OuterClass{
JPassField jpf1, jpf2;
.....
jpf1.addActionListener(new InnerClass1());
jpf2.addActionListener(new InnerClass2());
class InnerClass1 implements ActionListener{
public void actionPerformed(ActionEvent e){
//What is the e.getSource()? jpf1, jpf2 or something else
//that is not listed here?
}
}
...........
}如果你能确定触发这个内部类ActionListener的只可能是唯一一个控件时,可以完全抛开e.getSource()
如果
class OuterClass{
JPassField jpf1, jpf2;
.....
jpf1.addActionListener(new InnerClass1());
jpf2.addActionListener(new InnerClass2());
class InnerClass1 implements ActionListener{
public void actionPerformed(ActionEvent e){
//What is the e.getSource()? jpf1, jpf2 or something else
//that is not listed here?
}
}
...........
}如果你能确定触发这个内部类ActionListener的只可能是唯一一个控件时,可以完全抛开e.getSource()
//////////////////////////////////////////////////////////////////////////////
else if ( e.getSource() == password ) {
s = "password: " +
new String( password.getPassword() );
}
//////////////////////////////////////////////////////////////////////////////
但我认为这样会比较好:
...
if ( e.getSource() instanceof JPasswordField ) {
JPasswordField pwd =
(JPasswordField) e.getSource();
s = "password: " +
new String( pwd.getPassword() );
}