我创建了一个 Mac Java Swing 应用程序,并在“Info.plist”文件中为其设置了文件扩展名 (*.pkkt),因此当双击该文件时,它会打开我的应用程序。
当我这样做时,程序运行良好。现在我需要在程序中加载 (*.pkkt) 项目,但文件路径没有像 Windows 操作系统中那样作为参数传递给 Mac 中的 main(...) 方法。
经过一番搜索,我发现了一个 Apple 处理 jar“ MRJToolkitStubs ”,它具有MRJOpenDocumentHandler接口来处理此类单击的文件。我曾尝试通过在主程序类中实现该接口来使用它来加载该文件,但它不起作用。程序启动时永远不会调用已实现的方法。
这个接口如何运行?
- - - - - - - - - - - - - - - - - - - - - - - - - 编辑: 添加代码示例
这是我正在使用的代码:
public static void main( final String[] args ) { . . . MacOpenHandler macOpenHandler = new MacOpenHandler(); String projectFilePath = macOpenHandler.getProjectFilePath(); // Always Empty !! }
class MacOpenHandler implements MRJOpenDocumentHandler {
private String projectFilePath = "";
public MacOpenHandler () {
com.apple.mrj.MRJApplicationUtils.registerOpenDocumentHandler(this) ;
}
@Override
public void handleOpenFile( File projectFile ) {
try {
if( projectFile != null ) {
projectFilePath = projectFile.getCanonicalPath();
System.out.println( projectFilePath ); // Prints the path fine.
}
} catch (IOException e) {}
}
public String getProjectFilePath() {
return projectFilePath;
}
}
正如上面的评论中提到的,“getProjectFilePath()”总是空的!