我有同样的
java文件中的以下代码.
@H_404_2@import javax.swing.SwingUtilities;
import java.io.File;
public class MainClass2{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
javax.swing.JFileChooser jfc = new MyFileChooser();
File file = jfc.getSelectedFile();
}
});
}
}
class MyFileChooser extends javax.swing.JFileChooser{
public MyFileChooser(){
System.out.println("constructor call");
}
@Override
public java.io.File getSelectedFile(){
System.out.println("call to getSelectedFile");
return null;
}
}
当我运行它,输出给我
调用getSelectedFile
调用getSelectedFile
不应该输出
调用getSelectedFile
我使用的是Java 5.
解决方法
MyFileChooser的构造函数相当于:
@H_404_2@public MyFileChooser() {
super(); // ***
System.out.println("constructor call");
}
getSelectedFile()的第一个调用是由MyFileChooser的基类构造函数创建的,该构造函数在System.out.println(“构造函数调用”)之前被标记为***的位置隐式调用.
这是堆栈跟踪:
@H_404_2@MyFileChooser.getSelectedFile() line: 16 AquaFileChooserUI.installComponents(JFileChooser) line: 1436 AquaFileChooserUI.installUI(JComponent) line: 122 MyFileChooser(JComponent).setUI(ComponentUI) line: 670 MyFileChooser(JFileChooser).updateUI() line: 1798 MyFileChooser(JFileChooser).setup(FileSystemView) line: 360 MyFileChooser(JFileChooser).<init>(File,FileSystemView) line: 333 MyFileChooser(JFileChooser).<init>() line: 286 MyFileChooser.<init>() line: 11