JPanels随着JFrame的调整而缩小。

huangapple go评论83阅读模式
英文:

JPanels shrink when JFrame is resized

问题

public class Test extends JFrame {

    static Test frame;
    static Canvas canvas;
    static int video = 1;
    JButton button;

    public Test() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        JPanel followTo = new JPanel();
        gbc.gridy=0;
        gbc.weightx=1;
        gbc.weighty= 0.11;
        add(followTo, gbc);

        canvas = new Canvas();
        canvas.setBackground(Color.BLACK);

        // button = new JButton();

        gbc.gridy=1;
        gbc.weightx=1;
        gbc.weighty= 1.02;
        gbc.fill= GridBagConstraints.BOTH;
        add(canvas, gbc);
        // add(button, gbc);

        JPanel controls = new JPanel();
        gbc.gridy=2;
        gbc.weightx=1;
        gbc.weighty=0.16;
        add(controls, gbc);
    }

    public static void main(String[] args) {
        frame = new Test();
        frame.setSize(1200-54,864);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // /* Comment when using JButton
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"C:\\Program Files\\VideoLAN\\VLC");
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
        MediaPlayerFactory mpf = new MediaPlayerFactory();
        EmbeddedMediaPlayer emp = mpf.newEmbeddedMediaPlayer(new Win32FullScreenStrategy(frame));
        emp.setVideoSurface(mpf.newVideoSurface(canvas));
        emp.prepareMedia(url("Toulouse.mp4"));
        emp.play();
        // */
    }

    static String url (String video) {
        String mrl = new Object().getClass().getResource("/media/guide.txt").getFile();
        String url = mrl.replace("/", "\\").split("\\\\",2)[1].split("media")[0].concat("media\\"+video);
        return url;
    }
}
英文:

I'm making a video player with VLCJ; above and below I have two JPanels to display some information, and in the middle I have the canvas where the video is played. The problem is that when I resize the JFrame, the JPanels shrink and the Canvas expands, but when I place a button instead of the Canvas nothing changes. Do you know how to mantain the JPanels size when the JFrame is resized using GridBagLayout?

Snippet:

public class Test extends JFrame {
static Test frame;
static Canvas canvas;
static int video = 1;
JButton button;
public Test() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JPanel followTo = new JPanel();
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty= 0.11;
add(followTo, gbc);
canvas = new Canvas();
canvas.setBackground(Color.BLACK);  
//        button = new JButton();
gbc.gridy=1;
gbc.weightx=1;
gbc.weighty= 1.02;
gbc.fill= GridBagConstraints.BOTH;
add(canvas, gbc);
//        add(button, gbc);
JPanel controls = new JPanel();
gbc.gridy=2;
gbc.weightx=1;
gbc.weighty=0.16;
add(controls, gbc);
}
public static void main(String[] args) {
frame = new Test();
frame.setSize(1200-54,864);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// /* Comment when using JButton
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"C:\\Program Files\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
MediaPlayerFactory mpf = new MediaPlayerFactory();
EmbeddedMediaPlayer emp = mpf.newEmbeddedMediaPlayer(new Win32FullScreenStrategy(frame));
emp.setVideoSurface(mpf.newVideoSurface(canvas));
emp.prepareMedia(url("Toulouse.mp4"));
emp.play();
// */
}
static String url (String video) {
String mrl = new Object().getClass().getResource("/media/guide.txt").getFile();
String url = mrl.replace("/", "\\").split("\\\\",2)[1].split("media")[0].concat("media\\"+video);
return url;
}
}

答案1

得分: 1

问题在于当我调整JFrame的大小时,JPanels会收缩而Canvas会扩展,

好吧,你告诉了我们发生了什么,但你还没有告诉我们你期望发生什么。

我猜问题可能出在你使用的权重约束方式上。

基于提供的代码,我建议你不要使用GridBagLayout,而是使用BorderLayout

然后,将面板添加到框架的代码将简单地是:

add(followTo, BorderLayout.PAGE_START);
add(canvas, BorderLayout.CENTER);
add(controls, Borderlayout.PAGE_END):

现在,随着框架大小的改变,所有的空间都将被分配给CENTER位置的组件。顶部/底部的组件高度将保持不变。

请注意,在你的代码中也不应该使用静态变量。

英文:

> The problem is that when I resize the JFrame, the JPanels shrink and the Canvas expands,

Well you are telling us what is happening, but you haven't told us what you EXPECT should happen.

I would guess the problem is the way you are using the weighty constraints.

Based on the code provided I would suggest you should NOT use a GridBagLayout, but instead use a BorderLayout.

Then the code for adding the panels to the frame would simply be:

add(followTo, BorderLayout.PAGE_START);
add(canvas, BorderLayout.CENTER);
add(controls, Borderlayout.PAGE_END):

Now as the frame size changes, all the space will be given to the component in the CENTER. The components at the top/bottom would remain fixed in height.

Note you should also NOT be using static variables in your code.

huangapple
  • 本文由 发表于 2020年4月6日 00:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61045688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定