Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Created April 13, 2012 01:01
Show Gist options
  • Save resarahadian/2372485 to your computer and use it in GitHub Desktop.
Save resarahadian/2372485 to your computer and use it in GitHub Desktop.
Aplikasi MP3 Player sederhana dengan Java (Beta release)
/*
* @ author Resa C.R
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javazoom.jl.player.Player;
import javax.swing.UIManager.*; //Look and Feel
public class Music extends JFrame
{
Container konten = getContentPane();
private JLabel lblBuka = new JLabel("Buka File");
private JButton btnPlay = new JButton("Putar",new ImageIcon("src/MusicPlayer/play.png"));
private JButton btnStop = new JButton("Stop",new ImageIcon("src/MusicPlayer/stop.png"));
private JButton btnBuka = new JButton("Buka",new ImageIcon("src/MusicPlayer/buka.png"));
private JLabel lblAlamat = new JLabel();
private Icon gambar = new ImageIcon("src/MusicPlayer/lagu.png");
private JLabel lblLogo = new JLabel(gambar);
private JLabel lblInfo = new JLabel("Develop by : Resa C.R",SwingConstants.RIGHT);
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JPanel panel4 = new JPanel();
private JFileChooser fc = new JFileChooser();
private String url;
private File file;
private FileInputStream fis;
private Player player;
//Konstruktor
public Music()
{
super("MP3 Suka suka");
setSize(600,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
panel1.setLayout(new GridLayout(1, 2));
panel1.setBorder(BorderFactory.createMatteBorder(2, 4, 2, 4, Color.BLUE));
panel1.add(btnPlay);
panel1.add(btnStop);
panel2.setBorder(BorderFactory.createTitledBorder("Lokasi File"));
panel2.add(lblAlamat);
panel2.setBackground(Color.ORANGE);
panel2.add(lblLogo,BorderLayout.SOUTH);
panel3.setLayout(new GridLayout(1,2));
panel3.add(btnBuka);
panel3.add(lblInfo);
panel4.setLayout(new BorderLayout());
panel4.add(panel1,BorderLayout.NORTH);
panel4.add(panel2,BorderLayout.CENTER);
panel4.add(panel3,BorderLayout.SOUTH);
konten.add(panel4);
btnBuka.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent act)
{
// TODO Auto-generated method stub
//Mencari lokasi File
fc.setCurrentDirectory(new File(""));
int a = fc.showOpenDialog(null);
if(a == JFileChooser.APPROVE_OPTION)
{
try
{
url = fc.getSelectedFile().getPath();
lblAlamat.setText(url);
file = new File(url);
fis = new FileInputStream(file);
player = new Player(fis);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"File tidak dapat diputar","Pesan",JOptionPane.ERROR_MESSAGE);
lblAlamat.setText("");
}
}
}
});
btnPlay.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent act)
{
// TODO Auto-generated method stub
//Memutar File Music
try
{
player.play();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
});
btnStop.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent act)
{
// TODO Auto-generated method stub
try
{
player.close();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
});
}//Akhir Konstruktor
public static void main(String[] ar)
{
//Membuat Look and Feel Java Nimbus
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (UnsupportedLookAndFeelException e)
{
}
catch (ClassNotFoundException e)
{
}
catch (InstantiationException e)
{
}
catch (IllegalAccessException e)
{
}
new Music();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment