我刚刚添加了一个标准“打开文件“与我正在编写的一个小型桌面应用程序的对话,基于JFileChooserSwing 教程的入口 http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html。它生成一个如下所示的窗口:
但我更喜欢有一个如下所示的窗口:
换句话说,我希望我的文件选择器具有 Windows Vista/Windows 7 的风格,而不是 Windows XP 的风格。这在 Swing 中可能吗?如果是这样,是如何完成的? (出于本问题的目的,假设代码将专门在 Windows 7 计算机上运行。)
Java 6 中的 Swing 似乎不支持此功能。
目前,我能找到打开此对话框的最简单方法是通过 SWT,而不是 Swing。 SWT 的文件对话框 (javadoc http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/widgets/FileDialog.html) 弹出此对话框。以下是SWT的修改文件对话框片段 http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet72.java使用打开而不是保存对话框。我知道这并不完全是您正在寻找的,但您可以将其隔离到实用程序类,并将 swt.jar 添加到您的类路径中以实现此功能。
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class SWTFileOpenSnippet {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
// Don't show the shell.
//shell.open ();
FileDialog dialog = new FileDialog (shell, SWT.OPEN | SWT.MULTI);
String [] filterNames = new String [] {"All Files (*)"};
String [] filterExtensions = new String [] {"*"};
String filterPath = "c:\\";
dialog.setFilterNames (filterNames);
dialog.setFilterExtensions (filterExtensions);
dialog.setFilterPath (filterPath);
dialog.open();
System.out.println ("Selected files: ");
String[] selectedFileNames = dialog.getFileNames();
for(String fileName : selectedFileNames) {
System.out.println(" " + fileName);
}
shell.close();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)