| Home | Links |

 Pictures in Dialogue Box

·         the (“dog.gif”) part of the code is where the imaged is attached

·         the picture needs to be saved in the same folder as the code

·         the image can either be saved gif or jpeg/jpg – just remember to attach it with the same name and format

·         the “Doggy” part of the code is what appears as the title of the box

·         the “JOptionPane.INFORMATION_MESSAGE” inputs an information sign

·         the method at the end is what actually makes the dialogue box appear

 

import java.applet.*;

import java.awt.*;

import javax.swing.*;

public class dog extends Applet

{

public void init ()

{

JOptionPane.showMessageDialog (null, createImageIcon ("dog.gif"), "Doggy", JOptionPane.INFORMATION_MESSAGE);

}

protected static ImageIcon createImageIcon (String path)

{

java.net.URL imgURL = cat.class.getResource (path);

if (imgURL != null)

{

return new ImageIcon (imgURL);

}

else

{

System.err.println ("Couldn't find file: " + path);

return null;

}

}

}

Here's the output for the code above: