1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Frame extends JFrame implements ActionListener {
private JMenuBar m;
private JMenu op;
private JMenuItem ouv;
private JMenuItem test;
private MPlayer pla;
private Player p;
public Frame() throws MalformedURLException {
pla = new MPlayer();
p = null;
this.setTitle("Lecteur");
this.setPreferredSize(new Dimension(350, 150));
this.setLocationRelativeTo(null);
m = new JMenuBar();
this.setJMenuBar(m);
op = new JMenu("Option");
m.add(op);
ouv = new JMenuItem("Ouvrir");
test = new JMenuItem("Test");
op.add(ouv);
op.add(test);
this.add(pla);
op.addActionListener(this);
ouv.addActionListener(this);
test.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ouv) {
if (p != null) {
p.close();
pla.setplayer(null);
}
FileDialog dialog = new FileDialog(this);
dialog.setVisible(true);
if (dialog.getDirectory() != null && dialog.getFile() != null) {
String s = (dialog.getDirectory() + dialog.getFile());
System.out.println(s);
URL url = null;
try {
url = new URL("file://" + s);
} catch (MalformedURLException ex) {
Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
}
p = null;
try {
p = Manager.createPlayer(url);
} catch (IOException | NoPlayerException ex) {
Logger.getLogger(MPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
p.start();
}
}
}
} |
Partager