I have problems with adding images to JFrame java application. Now I am using Eclipse IDE.
Below is the code that no images is shown.
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JFrame;
public class LabelDemo {
public static void main(String[] args) {
JLabel northLabel = new JLabel("North");
ImageIcon labelIcon = new ImageIcon(\\resources\\CS2103_Logo.png");
JLabel centerLabel = new JLabel(labelIcon);
JLabel southLabel = new JLabel(labelIcon);
southLabel.setText("South");
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add( northLabel, BorderLayout.NORTH);
application.add(centerLabel, BorderLayout.CENTER);
application.add(southLabel, BorderLayout.SOUTH);
application.setSize(300,300);
application.setVisible(true);
}
}
After typing code, I right click on src folder to add subfolder called "resources" and add new file and link to my CS2103_LOGO.png
However, if I change the code from
ImageIcon labelIcon = new ImageIcon(\\resources\\CS2103_Logo.png");
to
ImageIcon labelIcon = new ImageIcon("D:\\JAVAworkspace\\CH6HowtoProgram\\src\\resources\\CS2103_Logo.png");
It will work well but I do not want. Because, I would like this application to be able to be used in different locations as well.
I also have one addition problem about GUI design.
If my design have one main body but after click a button it will expand another panel showing different information. Should I use big JFrame and then add panel later? Or should I seperate into many JFrame.
Thank you very much for your help :)