0
我正在嘗試將自定義圖標放到我的JFrame
上。我有我的項目文件夾中的圖標圖像,但我似乎無法讓它工作。Java:自定義圖標問題
我也試過setIconImage(new ImageIcon(imgURL).getImage());
但它似乎並不適用於我。
此外,什麼是自定義圖標
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/**
* @author Curtis
*/
public class Favorites extends JFrame implements ActionListener
{
String[] styles = {"Big Band", "Country", "Pop", "Rock", "Rap"};
Font boxFont = new Font("Times New Roman", Font.BOLD, 14);
JLabel instruct = new JLabel("What is your favorite type of music?");
JComboBox music = new JComboBox(styles);
JTextField result = new JTextField(20);
final int WIDTH = 270;
final int HEIGHT = 125;
public Favorites()
{
super("Favorite Music");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
instruct.setFont(boxFont);
JFrame.setDefaultLookAndFeelDecorated(true);
add(instruct);
add(music);
add(result);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
music.addActionListener(this);
Image icon = Toolkit.getDefaultToolkit().getImage("icnnote.jpg");
setIconImage(icon);
}
@Override
public void actionPerformed(ActionEvent e)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
是你的「icnnote.jpg」文件位於同一目錄中的類文件? –
我改變了代碼的順序,但它似乎仍然沒有工作。 Image icon = Toolkit.getDefaultToolkit()。getImage(「icnnote.jpg」); setIconImage(icon); setVisible(true); –
查看我的上述評論 –