Merto
Merto

Reputation: 433

Adding an Image to Applet Java

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);

 }
}

Upvotes: 0

Views: 117

Answers (1)

bigGuy
bigGuy

Reputation: 1744

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

ImageIcon image = new ImageIcon(Welcome.class.getResource ("car.jpg"));

Upvotes: 3

Related Questions