当我在Scala中运行此代码时,它运行良好:
scala> import javax.swing.JFileChooser
scala> import java.io.File
scala> def run() {
var chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("choosertitle");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
} else {
System.out.println("No Selection ");
}
}
scala> run()
但是这段代码不起作用,我想了解原因:
scala> class FileChoose {
def run() {
var chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("choosertitle");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
} else {
System.out.println("No Selection ");
}
}
}
defined class FileChoose
问题在于:
scala> new FileChoose.run()
<console>:13: error: not found: value FileChoose
new FileChoose.run()
^
答案 0 :(得分:2)
(new FileChoose).run()
如果您考虑以下示例,为什么需要这样清楚:
class Bar {
def foo {}
}
object Bar {
class foo
}
new Bar.foo
(new Bar).foo