将YouTubevideo嵌入JFrame?

我一直在做很多研究,并试图找到一个指南,可以教我如何正确地将YouTubevideo直接嵌入我的JFrame 。 我已经阅读了YouTube API上的所有Google Developers指南,但找不到我想要做的事情。

我正在尝试使用我的main方法中的init将YouTubevideo直接嵌入到JFrame中。 例如:

 /** * Main * @param args * @throws IOException */ public static void main(String[] args) throws IOException { try { UIManager.setLookAndFeel(new NimbusLookAndFeel()); } catch (UnsupportedLookAndFeelException ulafe) { Loader loader = new Loader(); loader.doFrame(); } Start Loader = new Start(); Loader.setVisible(true); } /** * Start * @throws IOException */ public Start() throws IOException { super("Ryan's Client Launcher version: '0.0.1'"); try { getContentPane().setBackground(Color.BLACK); setBackground(Color.BLACK); BufferedImage image39 = ImageIO.read(getClass().getResourceAsStream("\\jaggl\\igs\\39.png")); this.setIconImage(image39); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(new Dimension(launcherWidth, launcherHeight)); this.setLocationRelativeTo(null); this.setResizable(false); this.setUndecorated(true); getContentPane().setLayout(null); initWorldSelectInterface(); } catch (IOException e) { System.out.println(e); } } 

在我发起我的世界选择界面的地方,我希望有“ initYouTubeVideo

我创建了一个initYouTubeVideoinitYouTubeVideo ”,并不完全理解我如何只嵌入video和video。 说我为我的游戏做了更新,我制作了一个关于它的YouTubevideo。 我想在我的JFrame上播放该video(没有评论部分或任何其他只是简单的YouTubevideo)。

有人可以给我一个指南,我可以直接学习如何嵌入YouTubevideo。

对于这篇文章,我做了一个示例JFrame,以帮助您在视觉上理解我正在努力实现的目标。 这是图片:

图片

在图像中,红色是我放置YouTube播放器的地方,绿色是我的界面。 现在再次说我在我的游戏中做了一个更新并制作了一个关于它的YouTubevideo。 我希望能够将链接添加到video中,当有人运行客户端时,他们可以点击播放并观看video。

注意:我使用的是Google的YouTube API

我查看了以下指南:有关YouTube API的所有信息,请访问: https : //developers.google.com/youtube/v3/getting-started

我想要的只是将video添加到JFrame,以便我的玩家可以获得有关最新游戏内容的video更新。

谢谢你和代表那些可以帮助我的人。

这是我通常用于将YouTubevideo嵌入Swing应用程序的方式。

使用DJ Native Swing将原生浏览器组件嵌入到JPanel中,而不是使用YouTube API:

 public class YouTubeViewer { public static void main(String[] args) { NativeInterface.open(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("YouTube Viewer"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(getBrowserPanel(), BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationByPlatform(true); frame.setVisible(true); } }); NativeInterface.runEventPump(); // don't forget to properly close native components Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { NativeInterface.close(); } })); } public static JPanel getBrowserPanel() { JPanel webBrowserPanel = new JPanel(new BorderLayout()); JWebBrowser webBrowser = new JWebBrowser(); webBrowserPanel.add(webBrowser, BorderLayout.CENTER); webBrowser.setBarsVisible(false); webBrowser.navigate("https://www.youtube.com/v/b-Cr0EWwaTk?fs=1"); return webBrowserPanel; } } 

运行时看起来像

在此处输入图像描述

启动上面的示例需要以下库:

  • DJNativeSwing.jar
  • DJNativeSwing-SWT.JAR
  • swt-4.3-win32-win32-x86.jar(这个是平台相关的)

你可以从DJ Native Swing下载包中获取所有这些内容。

您可以尝试以下代码:此代码会将点击重定向到浏览器。

 public class YoutubePlay { public static void main(String[] args) throws URISyntaxException { final URI uri = new URI("http://www.youtube.com/watch?v=qzW6mgfY5X4"); class OpenUrlAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { open(uri); } } JFrame frame = new JFrame("Links"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(100, 400); Container container = frame.getContentPane(); container.setLayout(new GridBagLayout()); JButton button = new JButton(); button.setText("Click the link"); button.setHorizontalAlignment(SwingConstants.LEFT); button.setBorderPainted(false); button.setOpaque(false); button.setBackground(Color.WHITE); button.setToolTipText(uri.toString()); button.addActionListener(new OpenUrlAction()); container.add(button); frame.setVisible(true); } private static void open(URI uri) { if (Desktop.isDesktopSupported()) { try { Desktop.getDesktop().browse(uri); } catch (IOException e) { /* TODO: error handling */ } } else { /* TODO: error handling */ } } } 

我没有声誉来评论和解释Mayukh的情况,但我想帮助你。 这里的诀窍(在Jk1的答案中)就在

  webBrowser.navigate("https://www.youtube.com/v/b-Cr0EWwaTk?fs=1"); 

原始链接将是:

  https://www.youtube.com/watch?v=b-Cr0EWwaTk 

但你把它切换到:

  https://www.youtube.com/v/b-Cr0EWwaTk?fs=1 

在“全屏幕浏览器”视图中获取video。