如何让 vlcj 播放器不占用所有房间?

How can I keep the vlcj player from taking up all the room?

我正在编写一个小的 Swing 程序,它涉及在界面中嵌入一个视频播放器。为了实现这一点,我正在使用 vlcj。

虽然 GUI 本身有更多的组件,这里是一个模拟代码结构:

import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import javax.swing.*;
import java.awt.*;

public class MyTestClass extends JFrame {
    public MyTestClass(){
        EmbeddedMediaPlayerComponent playerCmpt = 
            new EmbeddedMediaPlayerComponent();
        playerCmpt.setPreferredSize(new Dimension(200, 100));

        JPanel leftPane = new JPanel();
        leftPane.setPreferredSize(new Dimension(100, 100));

        JSplitPane mainSplit = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT,
            leftPane, playerCmpt);

        this.add(mainSplit);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args){
        new MyTestClass();
    }
}

(我单独尝试了这段代码,我遇到了与我的 GUI 中相同的问题)

基本上,window 有两部分:左侧面板用于显示一些数据,右侧面板用于嵌入视频。这些面板以 JSplitPane 的顺序放在一起,以允许用户为播放器(以及视频本身)分配他想要的空间量。首先,组件的宽度分别为 100 和 200 像素。

问题是:EmbeddedMediaPlayerComponent 有点太舒服了。虽然我已将其设置为 200x100 的首选大小,但一旦移动垂直分割,它就拒绝缩小。也就是说,我无法将播放器的宽度缩小到 200 像素以下,并且一旦我将其放大,就无法将其恢复到 200 像素...设置最大尺寸不会改变任何内容。这个小问题很烦人,因为它迫使我的左面板一次又一次地收缩,直到它变得几乎看不见。

有什么方法可以让媒体播放器在用户尝试调整组件大小时遵循 JSplitPane 设置的限制?如果有用的话,左窗格在我的应用程序中包含一个 JTree,它也被播放器压碎了。

这个对我有用。只需改进代码以满足您的目的。

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.*;
import javax.swing.*;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;


public class MyTestClass extends JFrame {

    public MyTestClass() {
        String vlcPath = "C:\Program Files (x86)\VideoLAN\VLC";

        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcPath);
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);

        EmbeddedMediaPlayerComponent playerCmpt = new EmbeddedMediaPlayerComponent();
        playerCmpt.setPreferredSize(new Dimension(200, 100));

        JPanel leftPane = new JPanel();
        leftPane.setPreferredSize(new Dimension(100, 100));

        JPanel playerPanel = new JPanel(new BorderLayout());
        playerPanel.add(playerCmpt);

        JSplitPane mainSplit = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT,
            leftPane, playerPanel);

        playerPanel.setMinimumSize(new Dimension(10, 10));

        this.add(mainSplit);
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new MyTestClass();
    }
}