为 GUI 使用图标图像
Using an icon image for a GUI
我为一个学校项目创建了以下代码,"password protector",只是为了好玩,真的。但是,我遇到的问题是没有出现图标图像,而是默认的 java "coffee cup".
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UserInterfaceGUI extends JFrame
{
private static final long serialVersionUID = 1;
private JLabel userNameInfo; // ... more unimportant vars.
public UserInterfaceGUI()
{
this.setLayout(new FlowLayout());
userNameInfo = new JLabel("Enter Username:"); // ... more unimportant var. declartions
this.add(userNameInfo); // ... more unimportant ".add"s
event e = new event();
submit.addActionListener(e);
}
public static void main(String[] args)
{
//This icon has a problem \/
ImageIcon img = new ImageIcon("[File Location hidden for privacy]/icon.ico");
UserInterfaceGUI gui = new UserInterfaceGUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(400, 140);
gui.setIconImage(img.getImage());
gui.setTitle("Password Protector");
gui.setVisible(true);
}
}
有人能告诉我为什么这只在屏幕底部和 window 顶部的栏上显示 java 咖啡杯吗?
这里可能有两个问题:
- Java 不太可能支持 .ico 文件。唯一可以依赖的类型是 GIF、PNG 和 JPEG。
对于任何特定 JRE 支持的所有类型,请使用 ImageIO.getReaderFileSuffixes()
(但严肃地说,对于应用程序图标,请坚持使用 3 种类型以保证支持)。
- 该代码正在尝试将应用程序资源作为文件加载,当它可能是(或变成)embedded-resource that should be accessed by URL. See the embedded resource info. page 以获取有关如何形成 URL.[=19= 的提示时]
我为一个学校项目创建了以下代码,"password protector",只是为了好玩,真的。但是,我遇到的问题是没有出现图标图像,而是默认的 java "coffee cup".
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UserInterfaceGUI extends JFrame
{
private static final long serialVersionUID = 1;
private JLabel userNameInfo; // ... more unimportant vars.
public UserInterfaceGUI()
{
this.setLayout(new FlowLayout());
userNameInfo = new JLabel("Enter Username:"); // ... more unimportant var. declartions
this.add(userNameInfo); // ... more unimportant ".add"s
event e = new event();
submit.addActionListener(e);
}
public static void main(String[] args)
{
//This icon has a problem \/
ImageIcon img = new ImageIcon("[File Location hidden for privacy]/icon.ico");
UserInterfaceGUI gui = new UserInterfaceGUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(400, 140);
gui.setIconImage(img.getImage());
gui.setTitle("Password Protector");
gui.setVisible(true);
}
}
有人能告诉我为什么这只在屏幕底部和 window 顶部的栏上显示 java 咖啡杯吗?
这里可能有两个问题:
- Java 不太可能支持 .ico 文件。唯一可以依赖的类型是 GIF、PNG 和 JPEG。
对于任何特定 JRE 支持的所有类型,请使用ImageIO.getReaderFileSuffixes()
(但严肃地说,对于应用程序图标,请坚持使用 3 种类型以保证支持)。 - 该代码正在尝试将应用程序资源作为文件加载,当它可能是(或变成)embedded-resource that should be accessed by URL. See the embedded resource info. page 以获取有关如何形成 URL.[=19= 的提示时]