-1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;




public class Concentration extends JFrame implements ActionListener {

    private JButton buttons[][]=new JButton[4][4];
    int i,j,n;

    public Concentration() {

        super ("Concentration");    
        JFrame frame=new JFrame();
        setSize(10000,10000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel=new JPanel(new GridLayout(4,4));
        panel.setSize(4000, 4000);

        for( i=0; i<buttons.length; i++){
            for (j=0; j<buttons[i].length;j++){ 
                n=i*buttons.length+buttons[i].length;
                buttons[i][j]=new JButton(new ImageIcon("1.jpg"));

                panel.add(buttons[i][j]);

            }
        }
        add(panel);
        pack();
        setVisible(true);

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

}

I put the images in images folder in package folder but it doesnot show. What am i doing wrong?

I will make a memory game but still cant do anything. And should i use label instead of icon to show picture? Or jtogglebutton or jbutton?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
john brown
  • 55
  • 1
  • 1
  • 5
  • By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Sep 22 '13 at 20:33
  • `setSize(10000,10000);` What screen is this intended for, the JumboTron? – Andrew Thompson Sep 22 '13 at 20:35
  • made http://stackoverflow.com/questions/8960381/runnable-jars-missing-images-files-resources/9278270#9278270 here but now an exclamation mark near my project. I restarted refreshed. There seems no errors but it wont execute and show my window? I made a imagefolder in source, in it an images file and uttons[i][j]=new JButton(new ImageIcon(getClass().getResource("/images/1.jpg"))); but it does not work. – – john brown Sep 22 '13 at 21:18
  • The project cannot be built until build path errors are resolved. the error is that.but my project has imagefolder and in it images folder. How can this be – john brown Sep 22 '13 at 21:24
  • if I don't misunderstand your file system like that : {imagefolder/images/*.jpg} , you should use new ImageIcon(getClass().getResource("/imagefolder/images/1.jpg")); – Melih Altıntaş Sep 22 '13 at 21:39
  • Now i have bigger problem. I deleted images folder, i deleted the part of image from code and added "something" and it still executes the deleted images? i restarted a lot of times but still same. why? – john brown Sep 22 '13 at 22:24

1 Answers1

2

If the image is internal (you want a location relative to your project, or perhaps packaged into your jar) :

new ImageIcon(getClass().getResource("/images/1.jpg"));

The path is relative, so path/ will be a folder in the same folder as your project (or packaged into your jar).

If you want an external image, simply hand ImageIcon constructor the path (ex. "C:/.../file.png"). This isn't recommended though, as it's better to use it as a resource.

For more info on the ImageIcon constructor, see here. for more info on loading class resources, see here (Javadoc links)

Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35