我正在尝试在JFrame
上添加自定义图标。我的项目文件夹中有图标图像,但我似乎无法使其工作。
我也试过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.");
}
}
答案 0 :(得分:5)
您需要将setIconImage()
移到setVisible()
之上。它会在调用setVisible()
时显示框架。
更新:您还需要将图片更改为
getToolkit().getImage(getClass().getResource("iccnote.jpg"))