I'm a student currently studying Java Programming and using Netbeans to create the application. The program is already done and loads nicely in the IDE (with images). I have to build it into JAR for a presentation to my lecturer and has done it but the images are not present in the JAR.
First of all, I've checked all the available answers for allowing images to be present in the JAR but I couldn't get it right with the program as it doesn't load even in IDE and shows error. Which most have pointed out that I have to input (getClass().getClassLoader().getResource("image.jpg"))
. I tried inputting it but it shows errors, mostly because my codes for placing the ImageIcon are different.
Below is my full code of the JFrame presenting the program:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class A2{
public void GUI () throws IOException {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setTitle("Minus");
frame.setSize(700,500);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
//Set the layout
JPanel panel;
panel = new JPanel();
panel.setLayout(null);
frame.setContentPane(panel);
// Create all components
JLabel ctgy = new JLabel("Minus");
JLabel minus2 = new JLabel("What is 6 squares minus 3 squares?");
JButton ans_a = new JButton("9");
JButton ans_b = new JButton("3");
JButton ans_c = new JButton("7");
JButton back = new JButton("Back");
JLabel min2 = new JLabel();
//Add objects to layout
panel.add(ctgy);
panel.add(minus2);
panel.add(min2);
panel.add(ans_a);
panel.add(ans_b);
panel.add(ans_c);
panel.add(back);
// Set position of objects in content pane
min2.setLocation(100,100);
minus2.setLocation(20,50);
ctgy.setLocation(10,3);
ans_a.setLocation(500,100);
ans_b.setLocation(500,150);
ans_c.setLocation(500,200);
back.setLocation(500, 400);
//Set size of object
min2.setSize(300,300);
ctgy.setSize(200,50);
minus2.setSize(350,50);
ans_a.setSize(100,30);
ans_b.setSize(100,30);
ans_c.setSize(100,30);
back.setSize(100, 30);
//Set the fonts and colors
Font font1 = new Font("Cooper Black", Font.BOLD, 26);
Font font2 = new Font("Calisto MT", Font.BOLD, 20);
ctgy.setFont(font1);
ctgy.setBackground(Color.white);
minus2.setFont(font2);
minus2.setBackground(Color.red);
panel.setBackground (Color.RED);
ans_a.setBackground(Color.white);
ans_b.setBackground(Color.white);
ans_c.setBackground(Color.white);
min2.setIcon(new ImageIcon("src/images/6-3.png"));
ans_b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Correct!");
try {
A3.main(null);
} catch (IOException ex) {
Logger.getLogger(A1.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
ans_a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Incorrect! Please try again.");
}
});
ans_c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Incorrect! Please try again.");
}
});
frame.repaint();
back.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent f){
Categories.main(null);
}
});
}
public static void main (String [] args) throws IOException {
A2 gd = new A2();
gd.GUI();
}
}
This is the part of my code where I state the JLabel to put my image in which I named as min2:
JLabel ctgy = new JLabel("Minus");
JLabel minus2 = new JLabel("What is 6 squares minus 3 squares?");
JButton ans_a = new JButton("9");
JButton ans_b = new JButton("3");
JButton ans_c = new JButton("7");
JButton back = new JButton("Back");
JLabel min2 = new JLabel();
This is adding the panel for the JLabel:
panel.add(min2);
The size and location:
min2.setLocation(100,100);
min2.setSize(300,300);
Lastly the image itself and location:
min2.setIcon(new ImageIcon("src/images/6-3.png"));
This part will show error because I set this to open a new Class when a user click the JButton so that would happen since you don't have the A3 Class:
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Correct!");
try {
A3.main(null);
} catch (IOException ex) {
Logger.getLogger(A1.class.getName()).log(Level.SEVERE, null, ex);
}
I've checked the JAR file with WinRAR and confirmed that images folder and the images are inside. I wanted to post the screenshots but Imgur isn't working for me.
The pathfile for all images are inside src/images
.
Please suggest what changes I need to make. Thank you and sorry if it has been asked too much.