【发布时间】:2023-03-15 20:15:01
【问题描述】:
所以默认情况下我没有在窗口框架上放置图像,我希望在我的应用程序运行时添加一个放置在框架角落的图像。我浏览了论坛,看到了一堆对我没有帮助的解决方案。我的程序运行没有错误,但图像没有显示。但是当程序在 linux 中运行时,当我专注于我的应用程序时,主导航栏(顶部)确实设置了图像,但这在 MacOS 中不会发生.
我的代码:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Image image = null;
URL url = null;
try {
url = new URL("https://www.multivutech.com/images/logo1.png");
image = ImageIO.read(url);
} catch (MalformedURLException ex) {
System.out.println("Malformed URL");
} catch (IOException iox) {
System.out.println("Can not load file");
}
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new FlowLayout());
frame.getContentPane().add(panel, BorderLayout.CENTER);
JLabel label = new JLabel("Hello World!", SwingConstants.CENTER);
panel.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Hello World Frame!");
frame.setIconImage(image);
frame.pack();
frame.setVisible(true);
frame.setSize(354, 279);
【问题讨论】:
-
Windows 工作正常,但 .png 作为徽标不好。