Pages

Thursday, September 8, 2016

Add image to jlabel , jpanel or button (java)

Source code is below


Explanation

In java you can add an image to a button or Jlabel with just simple code in this page down is the code for how to add an image to a Jlabel just copy the code and paste to the eclipse and play around it
 
package my_game;
/*dailymotion.com/abdul-wahab65*/
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Display{
public static void main(String[]args){
   
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    /*create an object for Jlabel*/
    JLabel label = new JLabel();
   
    frame.setSize(599,500);
    frame.setVisible(true);
    frame.add(panel);
   
     panel.add(label);
    label.setPreferredSize (new Dimension(599,500));
    /*add an image now
     * copy the path of
     * your file where your
     * file is stored
     */
   
    /*and make sure to
     * type two back
     * slashes
     */
    ImageIconimage = new ImageIcon ("C:\\Users\\abdul\\Desktop\\Dekstop\\New folder\\aaa.jpg");
       /*add your image to
        * the label
        */
     label.setIcon(image);
}
}

No comments:

Post a Comment