Created
March 11, 2013 21:39
-
-
Save figengungor/5138016 to your computer and use it in GitHub Desktop.
Java GUI >> Adding Image on Button
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.ImageIcon; | |
| import javax.swing.JFrame; | |
| import javax.swing.JButton; | |
| import javax.swing.JPanel; | |
| public class MyGUI extends JFrame { | |
| JPanel jp; | |
| JButton jb; | |
| public MyGUI(){ | |
| setTitle("Background Color Example"); | |
| setSize(300,300); | |
| setVisible(true); | |
| setDefaultCloseOperation(EXIT_ON_CLOSE); | |
| jp = new JPanel(); | |
| jb = new JButton(); | |
| jb.setIcon(new ImageIcon("C://pn.jpg")); | |
| jp.add(jb); | |
| 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