Java JLabel create image label

Description

Java JLabel create image label

// Demonstrate JLabel and ImageIcon. 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class Demo extends JPanel {

   public Demo() {

      // Create an icon.
      ImageIcon ii = new ImageIcon("1.png");

      // Create a label.
      JLabel jl = new JLabel("OK", ii, JLabel.CENTER);

      // Add the label to the content pane.
      add(jl);//from   ww  w .ja  v a2s  .com
   }

}

public class Main {
   public static void main(String[] args) {
      Demo panel = new Demo();

      JFrame application = new JFrame();

      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      application.add(panel);
      application.setSize(250, 250);
      application.setVisible(true);
   }
}



PreviousNext

Related