Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的图标图像不会在mac上的JFrame中显示_Java - Fatal编程技术网

Java 我的图标图像不会在mac上的JFrame中显示

Java 我的图标图像不会在mac上的JFrame中显示,java,Java,我的徽标图像没有显示在mac上。Jframe中没有java徽标吗? 我错过了什么 package test; import javax.swing.*; public class Demo { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("this is the frame title");

我的徽标图像没有显示在mac上。Jframe中没有java徽标吗? 我错过了什么

package test;

import javax.swing.*;

public class Demo {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("this is the frame title");
        frame.setSize(420,420);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        
        ImageIcon image = new ImageIcon("logo.png");
        frame.setIconImage(image.getImage());
    }       
}

mac OS不会显示您的图标,您的代码没有问题,您可以做的是启用跨平台的外观。在文档中,您可以看到如何实现这一点并对其进行配置。基本上,在启动jframe之前,请执行以下操作:

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName();
// you can even set a theme on it
MetalLookAndFeel.setCurrentTheme(new OceanTheme()); // since Metal is the L&F for cross platforms.
JFrame.setDefaultLookAndFeelDecorated(true);

mac OS不会显示您的图标,您的代码没有问题,您可以做的是启用跨平台的外观。在文档中,您可以看到如何实现这一点并对其进行配置。基本上,在启动jframe之前,请执行以下操作:

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName();
// you can even set a theme on it
MetalLookAndFeel.setCurrentTheme(new OceanTheme()); // since Metal is the L&F for cross platforms.
JFrame.setDefaultLookAndFeelDecorated(true);

不要试图通过文件系统路径加载图像(即使它现在可以工作,在JAR中封装代码后也会遇到问题)。相反,尝试将它们作为项目资源加载。您可能想要读取,但不要尝试通过文件系统路径加载图像(即使它现在可以工作,在JAR中封装代码后也会遇到问题)。相反,尝试将它们作为项目资源加载。你可能想看看,