java - 在 Ubuntu 12.04 (GTK) 下改进 JFileChooser

标签 java swing ubuntu jfilechooser filedialog

我在 Ubuntu 12.04 下遇到了 JFileChooser 的问题。我使用此代码来设置外观: javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());

它看起来像这样。用起来很不舒服,看起来也很丑:

enter image description here
(来源:picfront.org)

我希望它看起来像这样:

enter image description here
(来源:picfront.org)

使用来自 post 的提示,我尝试使用 FileDialog 而不是 FileChooser。但是,当我以 LOAD 模式启动它并单击“打开”按钮时,FileDialog 会抛出异常。 我创建对话框的方式:

FileDialog fd = new FileDialog(frame, "Test", FileDialog.LOAD);
fd.setVisible(true);

异常:

Exception in thread "Thread-0" java.lang.NullPointerException
at sun.awt.X11.GtkFileDialogPeer.setFileInternal(GtkFileDialogPeer.java:79)
at sun.awt.X11.GtkFileDialogPeer.run(Native Method)
at sun.awt.X11.GtkFileDialogPeer.showNativeDialog(GtkFileDialogPeer.java:172)
at sun.awt.X11.GtkFileDialogPeer.access$000(GtkFileDialogPeer.java:39)
at sun.awt.X11.GtkFileDialogPeer$1.run(GtkFileDialogPeer.java:114)

我在带有 Gnome 3 的 Ubuntu Linux 12.04 下使用 Oracle JDK7(如果这有帮助的话)。

有没有人知道我如何改进 JFileChooser 的外观或让 FileDialog 工作?

最佳答案

我在我正在处理的 Java GUI 项目中遇到了同样的问题。我将它设置为使用“zenity”终端程序来调用 Linux/unix 系统上的 native 文件选择器。请注意,此解决方案不需要导入任何额外的 Java 库(尽管您必须在 Linux 上安装 Zenity),并且在 Windows 上也能正常工作:

private File fileSelection(boolean savemode) {
        String os = System.getProperty("os.name");
        File input = null;
        String zenity = "zenity --file-selection --title=Open";
        String filestring;
        if ((os.indexOf("nix")!=-1 || os.indexOf("nux")!=-1)) {
            //Use native Linux file selection.
            try {
                if (savemode) {
                    zenity ="zenity --file-selection --title=Save --save";
                }
                Process p = Runtime.getRuntime().exec(zenity);  
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));  
                StringBuffer sb = new StringBuffer();  
                String line;
                /*while ((line = br.readLine()) != null) {  
                  sb.append(line).append("\n");  
                } */
                sb.append(br.readLine());
                filestring = sb.toString();  
                if (filestring.equals("null")) {
                    return null;
                }
                System.out.println(filestring);
                input = new File(filestring);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else {
            final JFileChooser fc = new JFileChooser();
            int returnVal;
            if (savemode) {
                returnVal = fc.showSaveDialog(fc);
            } else {
                returnVal = fc.showOpenDialog(fc);  
            }
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                input = fc.getSelectedFile();
            }
        }
        return input;
    }

关于java - 在 Ubuntu 12.04 (GTK) 下改进 JFileChooser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10597831/

相关文章:

java - JSP获取参数

java - 添加 jSTL 作为 Maven 依赖

java - 在 JTextArea 中设置插入符位置

java - 如何设置应用程序目录的对象路径

php - 将 Apache 连接到远程 MySQL

linux - apt-get 代理 : Permission denied

java - 如何用Java制作循环列表

java - 被攻击的 WordPress 网站

java - 我应该使用什么类来扩展 SwingWorker?

c++ - 如何使用 OpenCL 构建 ImageMagick?