нашёл jframe-player, решил сделать japplet :
| Код | /* * JApplet_player с кнопками select, play и stop * <applet code="mplayer" width="640" height="360"></applet> * javac mplayer.java */ import javax.media.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class mplayer extends JApplet { Font f; JPanel pk, pm, ps; JButton play, stop, select; Player mplayer; URL mediaURL = null; Component vc; private static final String SELECT = "SELECT"; private static final String PLAY = "PLAY"; private static final String STOP = "STOP"; public void init() { try{SwingUtilities.invokeAndWait(new Runnable(){public void run(){makeGUI();}});} catch(Exception exc){System.out.println("Can't create, because "+exc);} } public void makeGUI() { f = new Font("Verdana", Font.BOLD, 22); setLayout(new BorderLayout()); setBackground(Color.black); ps = new JPanel(); ps.setBackground(Color.black); ps.setLayout(new FlowLayout(FlowLayout.RIGHT)); select = new JButton("SELECT"); select.setActionCommand(SELECT); select.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if(select.getActionCommand().equals(SELECT)) { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(null); if(result == JFileChooser.APPROVE_OPTION) { URL mediaURL = null; try{mediaURL = fileChooser.getSelectedFile().toURL();} catch ( MalformedURLException malformedURLException ){System.err.println("Can't create URL");} if (mediaURL != null) { JApplet applet = new JApplet(); applet.add(pm); applet.setVisible(true); } } } } }); ps.add(select); add(BorderLayout.NORTH, ps); pm = new JPanel(); pm.setBackground(Color.white); pk = new JPanel(); pk.setBackground(Color.black); pk.setLayout(new FlowLayout(FlowLayout.LEFT)); play = new JButton("PLAY"); play.setActionCommand(PLAY); play.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if(play.getActionCommand().equals(PLAY)) { try { final Player mplayer = Manager.createRealizedPlayer(mediaURL); mplayer.addControllerListener(new ControllerAdapter() { public void realizeComplete(RealizeCompleteEvent event) { Component vc = mplayer.getVisualComponent(); if(vc != null){pm.add(vc); add(BorderLayout.CENTER, pm);} mplayer.start(); } }); } catch (NoPlayerException noPlayerException){System.err.println("Player not found");} catch (CannotRealizeException cannotRealizeException){System.err.println("Can't realize player");} catch (IOException iOException){System.err.println("Error in source");} } } }); stop = new JButton("STOP"); stop.setActionCommand(STOP); stop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if(stop.getActionCommand().equals(STOP)) { if (vc != null){mplayer.stop();} } } }); pk.add(play); pk.add(stop); add(BorderLayout.SOUTH, pk); } public void controllerUpdate(ControllerEvent ce){if(mplayer == null) return;} public Insets getInsets(){return new Insets(5, 5, 5, 5);} }
|
вроде-бы всё на месте, только - диалоговое окно появляется пустым, не могу понять - в чём дело ? |