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?
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)