运行文件选择器的 Scala 类 [英] Scala class running a file chooser

查看:14
本文介绍了运行文件选择器的 Scala 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Scala 中运行这段代码时,它运行良好:

When I run this code in Scala it works well:

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()

但是此代码不起作用,我想了解原因:

However this code does not work and I'd like to understand why:

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()
                  ^

推荐答案

(new FileChoose).run()

如果您考虑以下示例,就很清楚为什么需要这样:

Why it needs to be like that is clear if you consider the following example:

class Bar {
  def foo {}
}

object Bar {
  class foo
}

new Bar.foo
(new Bar).foo

这篇关于运行文件选择器的 Scala 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆