-1
我爲學校項目創建了一個「密碼保護程序」的代碼,只是爲了好玩,真的。但是,我遇到的問題是圖標圖像不出現,而是默認的java「咖啡杯」。使用GUI的圖標圖像
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);
}
}
有人可以告訴我爲什麼這只是顯示在屏幕的底部和窗口頂部欄上的java咖啡杯?
不確定Java是否可以讀取「.ico」文件。嘗試使用.jpg或.gif圖片。 – camickr
你確定你的ImageIcon已完全初始化(錯誤的路徑?)@camickr你是對的,請參閱http://stackoverflow.com/questions/12185768/does-swing-support-ico-files –
這不會解決你的問題,但考慮使用['setIconImages'](http://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#setIconImages-java.util.List-)來代替,它允許定義不同分辨率的圖像 – Robin