[英]Java - Transfer JFrame to JPanel
I have video player class that extends JFrame, because I need to run this frame in stage (Jafafx by using SwingNode), I need to transfer this class to JPanel. 我有扩展JFrame的视频播放器类,因为我需要在舞台上运行此框架(通过使用SwingNode来使用Jafafx),因此需要将该类转移到JPanel。 This is possible?
这个有可能? I tried to change, but created problems in EventHangler class, and "visualComponent" not show in the panel, only controlsComponent.
我试图进行更改,但是在EventHangler类中创建了问题,并且面板中未显示“ visualComponent”,而仅显示controlsComponent。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.media.*;
public class MediaPlayerDemo extends JFrame {
private Player player;
private File file;
public MediaPlayerDemo()
{
super( "Demonstrating the Java Media Player" );
JButton openFile = new JButton( "Open file to play" );
openFile.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
openFile();
createPlayer();
}
}
);
getContentPane().add( openFile, BorderLayout.NORTH );
setSize( 300, 300 );
show();
}
private void openFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );
// user clicked Cancel button on dialog
if ( result == JFileChooser.CANCEL_OPTION )
file = null;
else
file = fileChooser.getSelectedFile();
}
private void createPlayer()
{
if ( file == null )
return;
removePreviousPlayer();
try {
// create a new player and add listener
player = Manager.createPlayer( file.toURL() );
player.addControllerListener( new EventHandler() );
player.start(); // start player
}
catch ( Exception e ){
JOptionPane.showMessageDialog( this,
"Invalid file or location", "Error loading file",
JOptionPane.ERROR_MESSAGE );
}
}
private void removePreviousPlayer()
{
if ( player == null )
return;
player.close();
Component visual = player.getVisualComponent();
Component control = player.getControlPanelComponent();
Container c = getContentPane();
if ( visual != null )
c.remove( visual );
if ( control != null )
c.remove( control );
}
public static void main(String args[])
{
MediaPlayerDemo app = new MediaPlayerDemo();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
);
}
// inner class to handler events from media player
private class EventHandler implements ControllerListener {
public void controllerUpdate( ControllerEvent e ) {
if ( e instanceof RealizeCompleteEvent ) {
Container c = getContentPane();
// load Visual and Control components if they exist
Component visualComponent =
player.getVisualComponent();
if ( visualComponent != null )
c.add( visualComponent, BorderLayout.CENTER );
Component controlsComponent =
player.getControlPanelComponent();
if ( controlsComponent != null )
c.add( controlsComponent, BorderLayout.SOUTH );
c.doLayout();
}
}
}
}
To turn the JFrame into a JPanel do the following: 要将JFrame转换为JPanel,请执行以下操作:
EDIT: You should also replace super( "Demonstrating the Java Media Player" ) ;
编辑:您还应该替换super(“ Demonstrating the Java Media Player”) ; with super( new BorderLayout() );
与super(new BorderLayout()); – Eric Leibenguth
–埃里克·莱本古斯(Eric Leibenguth)
Also show() in JFrame is deprecated, meaning that you shouldn't use it. JFrame中的show()也已弃用,这意味着您不应使用它。 Use setVisible(true);
使用setVisible(true);
You can also replace your window closing event with frame. 您也可以将框架关闭事件替换为框架。 setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Good luck with your video player! 祝您的视频播放器好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.