简体   繁体   中英

How to set Icon to JFrame

I tried this way, but it didnt changed?

ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Desktop\\favicon(1).ico");
frame.setIconImage(icon.getImage());

Better use a.png file;.ico is Windows specific. And better to not use a file, but a class resource (can be packed in the jar of the application).

URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
frame.setIconImage(icon.getImage());

Though you might even think of using setIconImages for the icon in several sizes.

Try putting your images in a separate folder outside of your src folder. Then, use ImageIO to load your images. It should look like this:

frame.setIconImage(ImageIO.read(new File("res/icon.png")));

Finally I found the main issue in setting the jframe icon. Here is my code. It is similar to other codes but here are few things to mind the game.

    this.setIconImage(new ImageIcon(getClass().getResource("Icon.png")).getImage());

1) Put this code in jframe WindowOpened event

2) Put Image in main folder where all of your form and java files are created eg

src\ myproject\ myFrame.form
src\ myproject\ myFrame.java
src\ myproject\ OtherFrame.form
src\ myproject\ OtherFrame.java
src\ myproject\ Icon.png

3) And most important that name of file is case sensitive that is icon.png won't work but Icon.png.

this way your icon will be there even after finally building your project.

This works for me.

    frame.setIconImage(Toolkit.getDefaultToolkit().getImage(".\\res\\icon.png"));

For the export jar file, you need to configure the build path to include the res folder and use the following codes.

    URL url = Main.class.getResource("/icon.png");
    frame.setIconImage(Toolkit.getDefaultToolkit().getImage(url));

Yon can try following way,

myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));

Here is the code I use to set the Icon of a JFrame

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

  try{ 
    setIconImage(ImageIO.read(new File("res/images/icons/appIcon_Black.png")));
  } 
  catch (IOException e){
    e.printStackTrace();
  }

Just copy these few lines of code in your code and replace "imgURL" with Image(you want to set as jframe icon) location.

JFrame.setDefaultLookAndFeelDecorated(true);

//Create the frame.
JFrame frame = new JFrame("A window");

//Set the frame icon to an image loaded from a file.
frame.setIconImage(new ImageIcon(imgURL).getImage());

I'm using the following utility class to set the icon for JFrame and JDialog instances:

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Scanner;

public class WindowUtilities
{
    public static void setIconImage(Window window)
    {
        window.setIconImage(Toolkit.getDefaultToolkit().getImage(WindowUtilities.class.getResource("/Icon.jpg")));
    }

    public static String resourceToString(String filePath) throws IOException, URISyntaxException
    {
        InputStream inputStream = WindowUtilities.class.getClassLoader().getResourceAsStream(filePath);
        return toString(inputStream);
    }

    // http://stackoverflow.com/a/5445161/3764804
    private static String toString(InputStream inputStream)
    {
        try (Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A"))
        {
            return scanner.hasNext() ? scanner.next() : "";
        }
    }
}

So using this becomes as simple as calling

WindowUtilities.setIconImage(this);

somewhere inside your class extending a JFrame .

The Icon.jpg has to be located in myproject\src\main\resources when using Maven for instance.

I use Maven and have the structure of the project, which was created by entering the command:

mvn archetype:generate

The required file icon.png must be put in the src/main/resources folder of your maven project.

Then you can use the next lines inside your project:

ImageIcon img = new ImageIcon(getClass().getClassLoader().getResource("./icon.png"));
setIconImage(img.getImage());

My project code is as below:

private void setIcon() {
       setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/slip/images/cage_settings.png")));

    }
frame.setIconImage(new ImageIcon(URL).getImage());

/* frame is JFrame setIcon method, set a new icon at your frame new ImageIcon make a new instance of class (so you can get a new icon from the url that you give) at last getImage returns the icon you need it is a "fast" way to make an icon, for me it is helpful because it is one line of code */

public FaceDetection() {
    initComponents();
    //Adding Frame Icon
    try {
        this.setIconImage(ImageIO.read(new File("WASP.png")));
    } catch (IOException ex) {
        Logger.getLogger(FaceDetection.class.getName()).log(Level.SEVERE, null, ex);
    }
}'

this works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM