Duster The First Duster The First - 7 months ago 56
Java Question

JFrame Icon Not Displaying

This Is My Code And It Doesn't Show the Icons
No Errors Show Up And The Window Shows up fine
(All Icons Are The Write Size But Are All Completely White)

Any Help Woud Be Greatly Appreciated

Edit:

Is This Better

package dev.dusterthefrst.gamemain.display;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Image;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Demo {

public static void main(String[] args) {
DemoRun();
}

private static JFrame frame;
private static Canvas canvas;
private static ArrayList<Image> icons;


private static void DemoRun(){

icons = new ArrayList<Image>();
icons.add(new ImageIcon("16.png").getImage());
icons.add(new ImageIcon("32.png").getImage());
icons.add(new ImageIcon("64.png").getImage());
icons.add(new ImageIcon("128.png").getImage());

frame = new JFrame("Demo");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setIconImages(icons);

canvas= new Canvas();
canvas.setPreferredSize(new Dimension(500, 500));
canvas.setMaximumSize(new Dimension(500, 500));
canvas.setMinimumSize(new Dimension(500, 500));

frame.add(canvas);
frame.pack();


}


}

Answer

It should show the icon on JFrame ie.,enter image description here

below is the code I guess you have executed

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class JFrameTest {
    private JFrame frame;
    private Canvas canvas;
    private ArrayList<Image> icons;

    private String title;
    private int width, height;

    public JFrameTest(String title, int width, int height){
        this.title = title;
        this.width = width;
        this.height = height;
        initFrame();
        }

    private void initFrame() {
        icons = new ArrayList<Image>();
        icons.add(new ImageIcon("16.png").getImage());
        icons.add(new ImageIcon("32.png").getImage());
        icons.add(new ImageIcon("64.png").getImage());
        icons.add(new ImageIcon("128.png").getImage());

        frame = new JFrame(title);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setIconImages(icons);

        canvas= new Canvas();
        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setMinimumSize(new Dimension(width, height));

        frame.add(canvas);
        frame.pack();

    }  


}