简体   繁体   English

如何处理JFileChooser

[英]How to dispose of the JFileChooser

Every time the button is pressed, does it create a new JFileChooser object? 每次按下按钮时,是否会创建一个新的JFileChooser对象? Is it possible to dispose it, or does java do that automatically for me? 是否可以处理它,或者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 automatically disposes of unused memory using a Garbage collector, so yes. Java使用垃圾收集器自动处理未使用的内存,所以可以。 It will dispose of your JFileChooser object automatically. 它将自动处理您的JFileChooser对象。

Also yes, each time your button is pressed, if you call buttonPressed, a new JFileChooser will be created. 同样可以,每次按下按钮时,如果调用buttonPressed,将创建一个新的JFileChooser。 This is acceptable. 这是可以接受的。

The Garbage collector should take care of deleting your JFlieChooser object. 垃圾收集器应注意删除JFlieChooser对象。 You can read more about how the Garbage collector works here 您可以在此处详细了解垃圾收集器的工作方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM