|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
Display image icon in java Hi I am trying to write a code in java, I should be able to create a new image icon using the object. When I try to run the program the image simple does not appear on the button. All the image files are located in the image folder inside the working directory. I am post my code please check it. Code: public class Jbt2s extends JFrame { public static void main(String[] args) { new Jbt2s(); } public Jbt2s() { super("Creating bt2s"); addWindowListener(new ExitListener()); Container cn = getcnPane(); cn.setBackground(Color.white); cn.setLayout(new FlowLayout()); // This is the statement of interest // the image can be anything in my ImageIcon sp = new ImageIcon("images/grapes.jpg"); Jbt2 bt1 = new Jbt2("bt2 1"); cn.add(bt1); //Jbt2 bt22 = new Jbt2("bt2 2 ",sp); Jbt2 bt22 = new Jbt2("bt2 2"); cn.add(bt22); //Jbt2 bt23 = new Jbt2("bt2 3 ", sp); Jbt2 bt23 = new Jbt2("bt2 3"); bt23.setHorizontalTextPosition(SwingConstants.L EFT); cn.add(bt23); //Jbt2 bt24 = new Jbt2(sp); Jbt2 bt24 = new Jbt2(sp); cn.add(bt24); pack(); setVisible(true); } } |
#2
| |||
| |||
re: Display image icon in java Hi, I went through your code thoroughly, I think you should try this. this may be helpful to your program Code: ImageIcon sp = new ImageIcon(this.getClass().getResource("images/grapes.jpg")); |
#3
| |||
| |||
re: Display image icon in java Thanks for the reply I tried you code, and what I got was the following popup. Code: Cannot launch Java application Uncaught exception in main method: java.lang.NullPointerException |
#4
| |||
| |||
re: Display image icon in java Hi Since you are getting a NullPointerException Code: NullPointerException Code: RuntimeException
__________________ Grand Theft Auto 4 PC Video Game |
![]() |
|
Tags: awt, image icon, java, programming language |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Want to get icon which can displays the image of data which it contains with Windows 7 | Nakul2 | Windows Software | 4 | 04-01-2011 08:30 PM |
How to change JFrame image icon? | Juany | Software Development | 4 | 08-02-2010 04:39 PM |
Image Display In Java | Amaresh | Software Development | 5 | 30-01-2010 09:20 AM |
How to display an image icon file in subform of access 2003? | Superstar9 | Windows Software | 3 | 03-09-2009 06:27 PM |
Convert any image into icon | ChrisUlrich | Customize Desktop | 2 | 30-01-2009 11:31 AM |