Skip to content

Instantly share code, notes, and snippets.

@figengungor
Created March 11, 2013 21:29
Show Gist options
  • Select an option

  • Save figengungor/5137945 to your computer and use it in GitHub Desktop.

Select an option

Save figengungor/5137945 to your computer and use it in GitHub Desktop.
Java GUI >> Adding Image as JLabel
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyGUI extends JFrame {
JPanel jp;
JLabel jl;
public MyGUI(){
setTitle("Background Color Example");
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp = new JPanel();
jl = new JLabel();
jl.setIcon(new ImageIcon("C://pn.jpg"));
jp.add(jl);
add(jp);
validate(); //if you do not validate, image will not be displayed unless you resize your window.
}
public static void main(String [] args) {
new MyGUI();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment