Results 1 to 5 of 5
- 08-22-2009, 12:23 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
Can't display image icons on simple demo
I'm new to Java and trying to run some basic demos from a class on my iMac. I should be able to create a new image icon using the ImageIcon object. No problems building. But when I run the program, the image(s) simply don't appear on the button (for this example). The image files are located in an image folder inside the working directory. This is too simple to be an issue, but there it is. Any suggestions?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.ImageIcon.*;
public class JButtons extends JFrame {
public static void main(String[] args) {
new JButtons();
}
public JButtons() {
super("Creating Buttons");
addWindowListener(new ExitListener());
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
// This is the statement of interest
// the image can be anything in my
ImageIcon splat = new ImageIcon("images/grapes.jpg");
JButton button1 = new JButton("Button 1");
content.add(button1);
//JButton button2 = new JButton("Button 2 ",splat);
JButton button2 = new JButton("Button 2");
content.add(button2);
//JButton button3 = new JButton("Button 3 ", splat);
JButton button3 = new JButton("Button 3");
button3.setHorizontalTextPosition(SwingConstants.L EFT);
content.add(button3);
//JButton button4 = new JButton(splat);
JButton button4 = new JButton(splat);
content.add(button4);
pack();
setVisible(true);
}
}
- 08-22-2009, 12:44 AM #2
You might try this:
Java Code:ImageIcon splat = new ImageIcon(this.getClass().getResource("images/grapes.jpg"));
- 08-22-2009, 03:34 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
It worked the first time:)
After that I got the following popup:
Cannot launch Java application
Uncaught exception in main method:
java.lang.NullPointerException
- 08-22-2009, 05:48 AM #4
"After that I got the following popup:"
I'm not exactly sure what you mean you got a popup...
"Cannot launch Java application
Uncaught exception in main method:
java.lang.NullPointerException "
Hopefully it displays the stacktrace of exactly where and what line in your code the NullPointerException happened. Null translates to "Nothing." It means you have an object somewhere that hasn't been created or doesn't exist yet but you are trying to use it.
- 11-05-2009, 03:27 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Simple Calculator Display Problem :(
By jimbob in forum Java AppletsReplies: 4Last Post: 07-18-2009, 04:13 AM -
Putting clickable icons on an image
By szakee in forum AWT / SwingReplies: 3Last Post: 04-06-2009, 10:25 AM -
is there anyone how can help me with image icons please?
By xbox_nutter in forum New To JavaReplies: 4Last Post: 03-27-2009, 10:26 AM -
How do I display a JList with icons and text?
By aneesahamedaa in forum AWT / SwingReplies: 2Last Post: 09-04-2008, 08:49 AM -
Simple demo of CSV matching using Regular Expressions
By Java Tip in forum java.utilReplies: 0Last Post: 04-16-2008, 10:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks