Guest User

Untitled

a guest
Sep 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. /*
  2. * @ author Resa C.R
  3. */
  4. import javax.swing.*;
  5.  
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import javax.swing.UIManager.*;
  9.  
  10. public class OpenFile extends JFrame
  11. {
  12. Container konten = getContentPane();
  13. private JButton btnBuka = new JButton("Buka Gambar",new ImageIcon("src/Component/gambar/package.png"));
  14. private JPanel panel = new JPanel();
  15. private JFileChooser fc = new JFileChooser();
  16. private JLabel lbl = new JLabel();
  17. private ImageHandler handler = new ImageHandler();
  18.  
  19. //Konstruktor
  20. public OpenFile()
  21. {
  22. super("JFileChooser Java");
  23. setSize(1000,900);
  24. setVisible(true);
  25. setLocationRelativeTo(null);
  26. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.  
  28. panel.add(lbl);
  29. setLayout(new FlowLayout(FlowLayout.CENTER));
  30. konten.add(btnBuka,BorderLayout.NORTH);
  31. konten.add(panel,BorderLayout.CENTER);
  32.  
  33. btnBuka.addActionListener(handler);
  34.  
  35. }//Akhir Konstruktor
  36.  
  37. //Inner class
  38. private class ImageHandler implements ActionListener
  39. {
  40. public void actionPerformed(ActionEvent act)
  41. {
  42. int open = fc.showOpenDialog(panel);
  43. if(open == JFileChooser.APPROVE_OPTION)
  44. {
  45. String sumberGambar = fc.getSelectedFile().getPath();
  46. lbl.setIcon(new ImageIcon(sumberGambar));
  47. }
  48. }
  49. }//Akhir Inner class
  50.  
  51.  
  52. public static void main(String[] ar)
  53. {
  54. //Membuat Look and Feel dengan Java Nimbus
  55. try{
  56. UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  57. }
  58. catch (UnsupportedLookAndFeelException e)
  59. {
  60.  
  61. }
  62. catch (ClassNotFoundException e)
  63. {
  64.  
  65. }
  66. catch (InstantiationException e)
  67. {
  68.  
  69. }
  70. catch (IllegalAccessException e)
  71. {
  72.  
  73. }
  74. new OpenFile();
  75. }
  76. }
Add Comment
Please, Sign In to add comment