Embed a YouTube video to JFrame?

前端 未结 3 1653
礼貌的吻别
礼貌的吻别 2020-12-03 09:09

I\'ve been doing a lot of research and trying to find a guide that can teach me how to correctly embed a YouTube video directly to my JFrame. I\'ve read all of

3条回答
  •  有刺的猬
    2020-12-03 09:44

    You can try this code: This code will redirect the click to the browser.

    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 */ }
      }
      }
    

提交回复
热议问题