0

how can I add the following (car.jpg) image to my code's center. I can not see my picture in the output.

public class Welcome extends JApplet implements ActionListener {

JLabel title = new JLabel("hello");
JButton reserve = new JButton("btn 2");
JButton webpage = new JButton("btn 3");

ImageIcon image = new ImageIcon("car.jpg");

JLabel label = new JLabel("PHOTO", image, SwingConstants.CENTER);

public void init() {


    setLayout(null);


    add(title);
    add(reserve);
    add(webpage);
    add(label);


   label.setLocation (1000,1000);
   label.setSize (2500, 2300);
   reserve.addActionListener(this);
   webpage.addActionListener(this);

   title.setLocation(10, 10);
   title.setSize(250, 30);



   reserve.setLocation(50, 70);
   reserve.setSize(250, 50);
   webpage.setLocation(50, 130);
   webpage.setSize(150, 30);

 }
}
mKorbel
  • 109,107
  • 18
  • 130
  • 305
Merto
  • 363
  • 1
  • 3
  • 6
  • 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Apr 30 '14 at 06:19

1 Answers1

3

Put image file into jar. Then you can load it like this:

ImageIcon image = new ImageIcon(Welcome.class.getResource ("car.jpg"));
bigGuy
  • 1,692
  • 1
  • 22
  • 36
  • how can i put my image to jar? – Merto Apr 29 '14 at 18:59
  • how you build jar? ant? export with eclipse? – bigGuy Apr 29 '14 at 19:00
  • try to copy image to src folder and export jar – bigGuy Apr 29 '14 at 19:01
  • i had putted it into my src folder but i can not exported it to jar can you please help me about this please? – Merto Apr 29 '14 at 19:03
  • Check this: http://stackoverflow.com/questions/423938/java-export-to-an-jar-file-in-eclipse – bigGuy Apr 29 '14 at 19:05
  • *"i had putted it into my src folder but i can not exported it to jar can you please help me about this please?"* That is an entirely new question that should be asked as a new question with the [tag:eclipse] tag added. Please [accept](http://meta.stackoverflow.com/a/65088/155831) the answer if it helped solve the (add the image to code's center) problem. – Andrew Thompson Apr 30 '14 at 06:21