在JFrame中更改图标(摆脱杯赛)

时间:2018-09-18 07:21:06

标签: java jframe icons

作为练习,我必须将JFrame的图标更改为公司徽标。 但是无论如何,我不会改变。

 public class window extends JFrame
{
private JTextField dateiname;

JLabel textdatei;
String[] choice = { "", "Berechtigung anlegen", "Tabelle erstellen",    "Stammdaten anlegen" };

String intext="";
JTextArea ausgabe = new JTextArea(intext,19,10);

public      ImageIcon icon = new ImageIcon("tqg.JPG");
public window()
{

    super("SQL-Code-Vorlage");
    setLayout(new BorderLayout());

    setSize(400, 400);
    setIconImage(icon.getImage());

我将“ tqg.jpg”放在项目的主文件夹src文件夹中,我也尝试将其放在/ images文件夹中。它只是不会更改徽标。

//已修复

  try
    {
        FileInputStream inputStream = new FileInputStream(new     File("tqg.png"));

        Image image = ImageIO.read(inputStream);

        this.setIconImage(image);
    } catch (IOException e1)
    {

        e1.printStackTrace();
    }

使用jpg时,ImageIcon会要求输入一条注释,如果没有,则将出现nullpointer异常。

1 个答案:

答案 0 :(得分:0)

您必须在setIconImage()

中指定框架
URL iconURL = getClass().getResource("/package name/tqg.JPG"); // path of the image
ImageIcon icon = new ImageIcon(iconURL);
frame.setIconImage(icon.getImage());