0

我正在尝试在我的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.");
}

}
4

1 回答 1

5

您需要将setIconImage()上面的setVisible(). setVisible()它在调用时显示帧。

更新:您还需要将图像更改为

getToolkit().getImage(getClass().getResource("iccnote.jpg"))

于 2012-06-19T20:44:37.210 回答