在Linux上使用Java Swing遇到麻烦,并获得设置外观和获取文件类型图标

发布于 2025-02-02 06:04:47 字数 9868 浏览 4 评论 0原文

我遇到了两个问题,我相信这有点相关。源代码,终端输出和GUI输出的屏幕截图不足。 Linux Java是OpenJDK,Windows/Mac是Oracle,以防有所不同。所有的帮助都非常感谢。

首先,在Kubuntu版本20.04(KDE)上,在设置系统默认外观和感觉时,我会得到金属外观和感觉,但是它在Windows,Mac和Ubuntu 22.04(Gnome)上正常工作。我查看了Java API文档( https://docs.oracle。 com/javase/tutorial/uiswing/lookandfeel/plaf.html ),他们说没有gtk+的系统(我认为kubuntu是这种情况,因为它使用kde而不是gnome)应该默认图像和感觉,并有图案的外观和感觉,不是金属。但是,即使终端输出同时显示GTK+和Motif外观和感觉,Kubuntu仍然默认为金属。为什么Kubuntu这样做?我知道如何在可用外观列表中解析文本,以选择合适的外观和感觉,但希望在可能的情况下避免。

平台默认外观和感觉
Solaris,带有GTK+ 2.2或更高版本的Linux+
其他Solaris,LinuxMotif
WindowsMacintosh
Macintosh第二,在Ubuntu和Kubuntu

上,我无法获得文件的文件类型(例如Triangular squiggly Line for PDF),但是PDF)我可以在Windows和Mac上。通过Jfilechooser浏览和请求文件图标时,它们都不会显示。我在Kubuntu 20.04检查了三个已知用Java编写的程序(NetBeans,Intellij和Eclipse),我发现Eclipse能够正确地显示其文件Chooser中的图标,Intellij和Intellij显示了不同但仍显示的文件图标,这表明那里表明那里有不同必须某种方式获得图标。如何获得文件类型图标?

>

public static void main(String[] args)
    {
        //Display system information
        String os_name = System.getProperty("os.name");
        String os_arch = System.getProperty("os.arch");
        String os_ver = System.getProperty("os.version");
        System.out.println("Current System: " + os_name + " " + os_arch + " " + os_ver);
        String java_name = System.getProperty("java.vm.name");
        String java_vendor = System.getProperty("java.vm.vendor");
        String java_ver = System.getProperty("java.vm.version");
        System.out.println("Current Java VM: " + java_name + " (" + java_vendor + ") " + java_ver);
        System.out.println();

        //initial Look and Feel
        LookAndFeel feel = UIManager.getLookAndFeel();
        System.out.println("Look and Feel initially loaded when Java starts");
        System.out.println("Name: " + feel.getName());
        System.out.println("ID: " + feel.getID());
        System.out.println("Description: " + feel.getDescription());
        System.out.println();
        
        //get a list of all the look and feels installed on this system
        UIManager.LookAndFeelInfo[] info1 = UIManager.getInstalledLookAndFeels();
        System.out.println("List of installed Look and Feels:");
        for (int i = 0; i < info1.length; i++)
        {
            System.out.println(info1[i]);
        }
        System.out.println();
        
        //set Look and feel to what the system says is the default Look and Feel.
        try
        {
            String systemLF = UIManager.getSystemLookAndFeelClassName();
            System.out.println("Default system look and feel: " + systemLF);
            System.out.println("Trying to set Default system look and feel");
            System.out.println();
            UIManager.setLookAndFeel(systemLF);         
        }
        catch(Exception e)
        {
            System.out.println("Error with setting default system Look and Feel");
            System.out.println();
        }

        //new system Look and Feel
        LookAndFeel feel2 = UIManager.getLookAndFeel();
        System.out.println("Look and Feel after trying to set default system Look and Feel");
        System.out.println("Name: " + feel2.getName());
        System.out.println("ID: " + feel2.getID());
        System.out.println("Description: " + feel2.getDescription());
        System.out.println();

        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();
        //System.out.println(file.getAbsolutePath());
        /*Doesn't work on Mac
        Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);*/
        final javax.swing.JFileChooser fc = new javax.swing.JFileChooser();       
        Icon icon = fc.getUI().getFileView(fc).getIcon(file); //get filetype icon for chosen file
        JOptionPane.showMessageDialog(null, "This is the file icon", "", JOptionPane.INFORMATION_MESSAGE, icon);
    }

屏幕截图:

控制台输出:

Current System: Linux amd64 5.4.0-110-generic
Current Java VM: OpenJDK 64-Bit Server VM (Private Build) 11.0.15+10-Ubuntu-0ubuntu0.20.04.1

Look and Feel initially loaded when Java starts
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[GTK+ com.sun.java.swing.plaf.gtk.GTKLookAndFeel]

Default system look and feel: javax.swing.plaf.metal.MetalLookAndFeel
Trying to set Default system look and feel

Look and Feel after trying to set default system Look and Feel
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

22.04:

Current System: Linux amd64 5.15.0-27-generic
Current Java VM: OpenJDK 64-Bit Server VM (Private Build) 11.0.15+10-Ubuntu-0ubuntu0.22.04.1

Look and Feel initially loaded when Java starts
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[GTK+ com.sun.java.swing.plaf.gtk.GTKLookAndFeel]

Default system look and feel: com.sun.java.swing.plaf.gtk.GTKLookAndFeel
Trying to set Default system look and feel

Gtk-Message: 22:12:59.057: Failed to load module "canberra-gtk-module"
Look and Feel after trying to set default system Look and Feel
Name: GTK look and feel
ID: GTK
Description: GTK look and feel

Windows输出:

Current System: Windows 10 amd64 10.0
Current Java VM: Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation) 15.0.1+9-18

Look and Feel initially loaded when Java starts
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Windows com.sun.java.swing.plaf.windows.WindowsLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Windows Classic com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel]

Default system look and feel: com.sun.java.swing.plaf.windows.WindowsLookAndFeel
Trying to set Default system look and feel

Look and Feel after trying to set default system Look and Feel
Name: Windows
ID: Windows
Description: The Microsoft Windows Look and Feel

MAC输出:

Current System: Mac OS X x86_64 10.16
Current Java VM: Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation) 13.0.1+9

Look and Feel initially loaded when Java starts
Name: Mac OS X
ID: Aqua
Description: Aqua Look and Feel for Mac OS X

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Mac OS X com.apple.laf.AquaLookAndFeel]

Default system look and feel: com.apple.laf.AquaLookAndFeel
Trying to set Default system look and feel

Look and Feel after trying to set default system Look and Feel
Name: Mac OS X
ID: Aqua
Description: Aqua Look and Feel for Mac OS X

I am having two problems, both which I believe are somewhat related. Source code, terminal output, and screenshots of GUI output are below. The Linux Java is OpenJDK and the Windows/Mac is Oracle in case that makes a difference. All help is much appreciated.

First, on Kubuntu version 20.04 (KDE), I get the Metal Look and Feel when setting the Look and Feel to the system default, yet it works properly on Windows, Mac, and Ubuntu 22.04 (GNOME). I looked at the Java API docs (https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html) and they say that systems without GTK+ (which I assume is the case for Kubuntu because it uses KDE instead of GNOME) should default to Motif look and feel, not Metal. However Kubuntu still defaults to Metal even though the terminal output shows both GTK+ and Motif Look and Feels are available. Why is Kubuntu doing this? I know how to parse text in the list of available Look and Feels to choose an appropriate Look and Feel but would like to avoid it if possible.

PlatformDefault Look and Feel
Solaris, Linux with GTK+ 2.2 or laterGTK+
Other Solaris, LinuxMotif
WindowsWindows
MacintoshMacintosh

Second, on both Ubuntu and Kubuntu, I cannot obtain the file type icon for files (like the triangular squiggly line for PDF) but I can on Windows and Mac. They don't show up both when browsing through JFileChooser and when requesting the icon for a file. I checked on Kubuntu 20.04 three programs known to be written in Java (Netbeans, IntelliJ, and Eclipse) and I found Eclipse was able to correctly show the icons in its file chooser and IntelliJ showed different but still present versions of file icons, indicating there must be some way to get the icon. How do I obtain the file type icon?

Source code:

public static void main(String[] args)
    {
        //Display system information
        String os_name = System.getProperty("os.name");
        String os_arch = System.getProperty("os.arch");
        String os_ver = System.getProperty("os.version");
        System.out.println("Current System: " + os_name + " " + os_arch + " " + os_ver);
        String java_name = System.getProperty("java.vm.name");
        String java_vendor = System.getProperty("java.vm.vendor");
        String java_ver = System.getProperty("java.vm.version");
        System.out.println("Current Java VM: " + java_name + " (" + java_vendor + ") " + java_ver);
        System.out.println();

        //initial Look and Feel
        LookAndFeel feel = UIManager.getLookAndFeel();
        System.out.println("Look and Feel initially loaded when Java starts");
        System.out.println("Name: " + feel.getName());
        System.out.println("ID: " + feel.getID());
        System.out.println("Description: " + feel.getDescription());
        System.out.println();
        
        //get a list of all the look and feels installed on this system
        UIManager.LookAndFeelInfo[] info1 = UIManager.getInstalledLookAndFeels();
        System.out.println("List of installed Look and Feels:");
        for (int i = 0; i < info1.length; i++)
        {
            System.out.println(info1[i]);
        }
        System.out.println();
        
        //set Look and feel to what the system says is the default Look and Feel.
        try
        {
            String systemLF = UIManager.getSystemLookAndFeelClassName();
            System.out.println("Default system look and feel: " + systemLF);
            System.out.println("Trying to set Default system look and feel");
            System.out.println();
            UIManager.setLookAndFeel(systemLF);         
        }
        catch(Exception e)
        {
            System.out.println("Error with setting default system Look and Feel");
            System.out.println();
        }

        //new system Look and Feel
        LookAndFeel feel2 = UIManager.getLookAndFeel();
        System.out.println("Look and Feel after trying to set default system Look and Feel");
        System.out.println("Name: " + feel2.getName());
        System.out.println("ID: " + feel2.getID());
        System.out.println("Description: " + feel2.getDescription());
        System.out.println();

        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();
        //System.out.println(file.getAbsolutePath());
        /*Doesn't work on Mac
        Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);*/
        final javax.swing.JFileChooser fc = new javax.swing.JFileChooser();       
        Icon icon = fc.getUI().getFileView(fc).getIcon(file); //get filetype icon for chosen file
        JOptionPane.showMessageDialog(null, "This is the file icon", "", JOptionPane.INFORMATION_MESSAGE, icon);
    }

Screenshots:

Console output:

Kubuntu 20.04:

Current System: Linux amd64 5.4.0-110-generic
Current Java VM: OpenJDK 64-Bit Server VM (Private Build) 11.0.15+10-Ubuntu-0ubuntu0.20.04.1

Look and Feel initially loaded when Java starts
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[GTK+ com.sun.java.swing.plaf.gtk.GTKLookAndFeel]

Default system look and feel: javax.swing.plaf.metal.MetalLookAndFeel
Trying to set Default system look and feel

Look and Feel after trying to set default system Look and Feel
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

Ubuntu 22.04:

Current System: Linux amd64 5.15.0-27-generic
Current Java VM: OpenJDK 64-Bit Server VM (Private Build) 11.0.15+10-Ubuntu-0ubuntu0.22.04.1

Look and Feel initially loaded when Java starts
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[GTK+ com.sun.java.swing.plaf.gtk.GTKLookAndFeel]

Default system look and feel: com.sun.java.swing.plaf.gtk.GTKLookAndFeel
Trying to set Default system look and feel

Gtk-Message: 22:12:59.057: Failed to load module "canberra-gtk-module"
Look and Feel after trying to set default system Look and Feel
Name: GTK look and feel
ID: GTK
Description: GTK look and feel

Windows Output:

Current System: Windows 10 amd64 10.0
Current Java VM: Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation) 15.0.1+9-18

Look and Feel initially loaded when Java starts
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Windows com.sun.java.swing.plaf.windows.WindowsLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Windows Classic com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel]

Default system look and feel: com.sun.java.swing.plaf.windows.WindowsLookAndFeel
Trying to set Default system look and feel

Look and Feel after trying to set default system Look and Feel
Name: Windows
ID: Windows
Description: The Microsoft Windows Look and Feel

Mac Output:

Current System: Mac OS X x86_64 10.16
Current Java VM: Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation) 13.0.1+9

Look and Feel initially loaded when Java starts
Name: Mac OS X
ID: Aqua
Description: Aqua Look and Feel for Mac OS X

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Mac OS X com.apple.laf.AquaLookAndFeel]

Default system look and feel: com.apple.laf.AquaLookAndFeel
Trying to set Default system look and feel

Look and Feel after trying to set default system Look and Feel
Name: Mac OS X
ID: Aqua
Description: Aqua Look and Feel for Mac OS X

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文