每次按下按钮时,它都会创建一个新的 JFileChooser 对象吗?是否可以处理它,或者 java 会自动为我处理它吗?
public void buttonPressed(){
JFileChooser chooser = null;
LookAndFeel previousLF = UIManager.getLookAndFeel();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
chooser = new JFileChooser();
UIManager.setLookAndFeel(previousLF);
} catch (IllegalAccessException | UnsupportedLookAndFeelException | InstantiationException | ClassNotFoundException e) {}
File location = new File("C:\\");
chooser.setCurrentDirectory(location);
chooser.setDialogTitle("Select Your Directory");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
chooser.showOpenDialog(frame);
}
最佳答案
Java 使用垃圾收集器自动处理未使用的内存,所以是的。它将自动处理您的 JFileChooser 对象。
也是的,每次按下按钮时,如果调用buttonPressed,将创建一个新的JFileChooser。这是可以接受的。
关于java - 如何处置 JFileChooser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17455641/