I tried to put the image but only gives NullPointerException
, I already tried to create a folder called img
inside the folder src
and point there but without success too. I do not know what I might be doing wrong. The image is in the directory.
Follow the code:
public class GuiTeste {
JFrame frame = new JFrame();
JButton bTOrdenar = new JButton();
public void iniciar() {
bTOrdenar.setIcon(new javax.swing.ImageIcon(getClass().getResource("img/iconOrganize.png")));
frame.setSize(520,120);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(BorderLayout.NORTH,bTOrdenar);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
GuiTeste teste= new GuiTeste();
teste.iniciar();
}
}
I also tried the command:
Image img = ImageIO.read(getClass().getResource("img/IconLoad2.bmp")); bTOrdenar.setIcon(new ImageIcon(img));