Comment modifier l'icône JFrame [dupliquer]

cette question a déjà une réponse ici:

  • comment définir L'icône à JFrame 10 réponses

j'ai un JFrame qui affiche une icône Java sur la barre de titre (coin gauche). Je veux changer cette icône pour mon icône personnalisée. Comment dois-je faire?

91
demandé sur Francesco Menzani 2009-10-23 21:11:12

8 réponses

créer un nouveau ImageIcon objet comme ceci:

ImageIcon img = new ImageIcon(pathToFileOnDisk);

puis réglez-le sur votre JFrame avec setIconImage() :

myFrame.setIconImage(img.getImage());

également caisse setIconImages() qui prend un List à la place.

141
répondu BFree 2015-08-31 16:52:29

Voici une Alternative qui a fonctionné pour moi:

yourFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Filepath)));

c'est très similaire à la réponse acceptée.

15
répondu Mmir 2014-06-18 09:24:32

JFrame.setIconImage(Image image) plutôt standard.

6
répondu Gandalf 2009-10-23 17:15:24

Voici comment je fais:

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;



public class MainFrame implements ActionListener{

/**
 * 
 */


/**
 * @param args
 */
public static void main(String[] args) {
    String appdata = System.getenv("APPDATA");
    String iconPath = appdata + "\JAPP_icon.png";
    File icon = new File(iconPath);

    if(!icon.exists()){
        FileDownloaderNEW fd = new FileDownloaderNEW();
        fd.download("http://icons.iconarchive.com/icons/artua/mac/512/Setting-icon.png", iconPath, false, false);
    }
        JFrame frm = new JFrame("Test");
        ImageIcon imgicon = new ImageIcon(iconPath);
        JButton bttn = new JButton("Kill");
        MainFrame frame = new MainFrame();
        bttn.addActionListener(frame);
        frm.add(bttn);
        frm.setIconImage(imgicon.getImage());
        frm.setSize(100, 100);
        frm.setVisible(true);


}

@Override
public void actionPerformed(ActionEvent e) {
    System.exit(0);

}

}

et voici le téléchargeur:

import java.awt.GridLayout;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;

public class FileDownloaderNEW extends JFrame {
  private static final long serialVersionUID = 1L;

  public static void download(String a1, String a2, boolean showUI, boolean exit)
    throws Exception
  {

    String site = a1;
    String filename = a2;
    JFrame frm = new JFrame("Download Progress");
    JProgressBar current = new JProgressBar(0, 100);
    JProgressBar DownloadProg = new JProgressBar(0, 100);
    JLabel downloadSize = new JLabel();
    current.setSize(50, 50);
    current.setValue(43);
    current.setStringPainted(true);
    frm.add(downloadSize);
    frm.add(current);
    frm.add(DownloadProg);
    frm.setVisible(showUI);
    frm.setLayout(new GridLayout(1, 3, 5, 5));
    frm.pack();
    frm.setDefaultCloseOperation(3);
    try
    {
      URL url = new URL(site);
      HttpURLConnection connection = 
        (HttpURLConnection)url.openConnection();
      int filesize = connection.getContentLength();
      float totalDataRead = 0.0F;
      BufferedInputStream in = new      BufferedInputStream(connection.getInputStream());
      FileOutputStream fos = new FileOutputStream(filename);
      BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
      byte[] data = new byte[1024];
      int i = 0;
      while ((i = in.read(data, 0, 1024)) >= 0)
      {
        totalDataRead += i;
        float prog = 100.0F - totalDataRead * 100.0F / filesize;
        DownloadProg.setValue((int)prog);
        bout.write(data, 0, i);
        float Percent = totalDataRead * 100.0F / filesize;
        current.setValue((int)Percent);
        double kbSize = filesize / 1000;

        String unit = "kb";
        double Size;
        if (kbSize > 999.0D) {
          Size = kbSize / 1000.0D;
          unit = "mb";
        } else {
          Size = kbSize;
        }
        downloadSize.setText("Filesize: " + Double.toString(Size) + unit);
      }
      bout.close();
      in.close();
      System.out.println("Took " + System.nanoTime() / 1000000000L / 10000L + "      seconds");
    }
    catch (Exception e)
    {
      JOptionPane.showConfirmDialog(
        null, e.getMessage(), "Error", 
        -1);
    } finally {
        if(exit = true){
            System.exit(128);   
        }

    }
  }
}
3
répondu TameHog 2013-06-09 16:58:57

malheureusement, la solution ci-dessus n'a pas fonctionné pour Jython Fiji plugin. J'ai dû utiliser getProperty pour construire le chemin relatif de façon dynamique.

voici ce qui a fonctionné pour moi:

import java.lang.System.getProperty;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

frame = JFrame("Test")
icon = ImageIcon(getProperty('fiji.dir') + '/path/relative2Fiji/icon.png')
frame.setIconImage(icon.getImage());
frame.setVisible(True)
2
répondu otterb 2016-08-26 01:06:04

il suffit d'ajouter le code suivant:

setIconImage(new ImageIcon(PathOfFile).getImage());
1
répondu user5984256 2018-03-29 21:13:31

cela a fait l'affaire dans mon cas super ou this se réfère à JFrame dans ma classe

URL url = getClass().getResource("gfx/hi_20px.png");
ImageIcon imgicon = new ImageIcon(url);
super.setIconImage(imgicon.getImage());
0
répondu shareef 2016-05-14 08:10:02

ajouter le code suivant dans le constructeur comme suit:

public Calculator() {
    initComponents();
//the code to be added        this.setIconImage(newImageIcon(getClass().getResource("color.png")).getImage());     }

changer " de couleur.png" au nom de fichier de l'image que vous souhaitez insérer. Faites glisser et déposez cette image sur le paquet (sous paquets Source) de votre projet.

exécutez votre projet.

0
répondu Raymond Wachaga 2016-06-16 03:15:56