Advertisement
TermSpar

Java Add Image

Feb 26th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. package imageTest;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.*;
  6. public class newImage  {
  7.         JFrame frame = new JFrame();
  8.     public newImage(){
  9.        
  10.    
  11.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12.         frame.setSize(800, 800);
  13.         frame.setVisible(true);
  14.         frame.setLayout(null);
  15.    
  16.    
  17.         JButton btn = new JButton();
  18.         btn.setText("Show Image");
  19.         btn.setBounds(100, 100, 70, 70);
  20.         btn.addActionListener(new ActionListener(){
  21.             public void actionPerformed(ActionEvent e){
  22.                     JLabel lbl = new JLabel();
  23.                     lbl.setIcon(new ImageIcon("C:\\Users\\bbol1765\\Downloads\\jpg_file.png"));
  24.                     lbl.setBounds(190, 150, 150, 150);
  25.                     frame.add(lbl);
  26.                     SwingUtilities.updateComponentTreeUI(frame);
  27.             }
  28.            
  29.         });
  30.         frame.add(btn);
  31.     }
  32.    
  33.     public static void main(String args[]){
  34.         new newImage();
  35.     }
  36.    
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement