OK, first thing I want to say is: yes, I know there are a number of similar questions on setting a background image in java. However, my situation is a bit different since I got handed a Java project that was started by someone else. I believe that the previous programmer was fairly new to Java GUI programming and I myself am very new to Java GUI programming. Here is what the program will have to do:
The only thing that the previous programmer did was set up 2 classes, one being the main and one setting up the background image which he had as a monkey from a url
Now, I decided to use MigLayout to organize a grid where I will place the bandwidth images in the correct arrangement. I was having trouble with that so I decided to start a little smaller and change the monkey background to the network map. I added the map jpg to the src library and thought that if I switched URL to File it would work. It didn't and I have been stuck trying to switch things around to get it to work but no cigar.
Below is the code for the setting up a background. This is my first time trying to post code I hope it works:... well I tried several times several ways and googled it several time but posting my code isnt working so I took a screen shot. Nothing is working for me today.
The errors I get are:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at bwMonBackground.<init>(bwMonBackground.java:21)
at bwMonBackgroundMain.main(bwMonBackgroundMain.java:7)
EDIT: I uncommented out line 18. Here is the main:
public class bwMonBackgroundMain extends bwMonBackground{
public static void main( String[] args )
{
bwMonBackground frame = new bwMonBackground();
migLayout testing = new migLayout();
testing.createLayout();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
}
EDIT 2: once i uncommented out line 18 in bwMonBackground.java I get the following errors:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at bwMonBackground.<init>(bwMonBackground.java:15)
at bwMonBackgroundMain.main(bwMonBackgroundMain.java:7)
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at bwMonBackground.<init>(bwMonBackground.java:20)
at bwMonBackgroundMain.main(bwMonBackgroundMain.java:7)
Basically, the error is saying that the File can not be found (you are specifying a null
reference to the ImageIcon
constructor). Make sure it exists in the root location of the application's execution context.
Now, to your actual problem.
You will want to setup a custom Component
(such as a JPanel
) and override the paintComponent
method
Take a look at
You may also want to look at
I bet you have IOException and you just do nothing in catch, thats why image is null. And JLabel with icon is not really a background image, its icon and you cant place elements in it. So you need to read this: java swing background image
It contains pretty good answer with code sample, just change URL to a valid image and launch.
Can you show the source of bwMonBackgroundMain.java? Right now it just looks like it can't find your image file you are trying to add to the label
An IOException is being thrown and you cant see if because the stack trace is commented out. I assume that the IO exception is being thrown when attempting to read the file. Try printing the stack trace and then solving it from there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.