Jsplitpane 自动调整大小 [英] Jsplitpane automatically resizes

查看:45
本文介绍了Jsplitpane 自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JSPlitPane,它们之间有 50% 的分隔线.这工作正常.

I have a JSPlitPane with a 50% divider between them. That works correctly.

但是,当我向右侧添加一些 JLabel 时,jsplitpane 会忽略我的 50% 分隔符,而左侧窗格会增加其大小并仅挤压右侧.

However, when I add some JLabels to the right side, then the jsplitpane ignores my 50% divider thing and the left hand pane increases its size and just squashes the right hand side.

为什么会发生这种情况以及如何解决?

Why does this happen and how to fix it?

我的代码是:

import javax.swing.*;
import java.awt.*;

public class Example {

    private static void setup() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500, 500));
        frame.setFocusable(true);

        JSplitPane pane= new JSplitPane();
        pane.setResizeWeight(0.5);
        pane.setRightComponent(new JPanel());

        JPanel myPanel = new JPanel();
        myPanel.setLayout(new FlowLayout());

        for(int i=0; i<20; i++) myPanel.add(new JLabel("hello"));

        pane.setLeftComponent(myPanel);

        frame.add(pane);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setup();
            }
        });
    }
}

推荐答案

但是,当我向右侧添加一些 JLabel 时,jsplitpane 会忽略我的 50% 分隔符,而左侧窗格会增加其大小并仅挤压右侧.

However, when I add some JLabels to the right side, then the jsplitpane ignores my 50% divider thing and the left hand pane increases its size and just squashes the right hand side.

拆分窗格将首先计算每个组件的首选大小.

The split pane will initially calculate the preferred size of each component.

调整大小权重用于在调整拆分窗格大小时分配空间.在这种情况下,当框架可见时就会发生这种情况.因此,如果添加到拆分窗格的组件的首选大小小于使框架可见时拆分窗格的大小,则每个组件将获得 50% 的额外空间.

The resize weight is for allocating space when the split pane is resized. In this case this will happen when the frame is made visible. So if the preferred size of the components added to the split pane is less than the size of the split pane when the frame is made visible each component will get 50% of the extra space.

在第一种情况下,没有组件,所以每个面板的首选大小是相同的,所以当拆分面板调整大小时,它们每个都会获得相同的空间.

In the first case there are no components so the preferred size of each panel is the same, so when the split pane is resized they each get the same space.

在第二种情况下,第一个面板的首选大小将是添加到面板的所有标签的宽度,第二个面板的宽度为 0.由于第一个面板的首选宽度大于那里的框架的大小没有额外的空间分配给第二个面板.

In the second case the preferred size of the first panel will be the width of all the labels added to the panel and the second panel is 0. Since the preferred width of the first panel is greater than the size of the frame there is no extra space to allocate to the second panel.

尝试添加 1 或 2 个标签而不是 20 个标签,看看会发生什么.

Try adding 1 or 2 labels instead of 20 to see what happens.

一些问题:

1) 调整大小的权重不像您预期​​的那样起作用.当分隔线以慢速移动时,每个像素都会生成一个事件.一个像素不能被两个组件共享,所以像素被分配给正确的组件.

1) The resize weight doesn't work as you might expect. When the divider is moved as a slow speed an event is generated for every pixel. Well a pixel can't be shared by both components so the pixel is given to the right component.

我建议您的解决方案是使用1.0"的调整大小权重,以便标签组件获得所有空间.

I would suggest the solution in you case would be to use a resize weight of "1.0" so that the label component get all the space.

2) JSplitPane 尊重组件的最小尺寸,这意味着您不能将栏拖动到组件的最小尺寸之外.对于您的面板,最小尺寸与首选尺寸相同,因此您无法将分隔线拖得更小.

2) The JSplitPane respects the minimum size of a component which means you can't drag the bar beyond the components minimum size. For your panel the minimum size is the same as the preferred size so you won't be able to drag the divider smaller.

解决方案是覆盖面板的getMinimumSize()方法返回一些较小的尺寸

The solution is to override the getMinimumSize() method of the panel to return some smaller dimension

  1. 手动将分隔位置设置为初始显示拆分窗格时拆分窗格大小的 50%.

这可以通过将 setDividerLocation(...) 方法包装在 SwingUtilities.invokeLater() 中来实现.

This can be done by wrapping the setDividerLocation(...) method in a SwingUtilities.invokeLater().

以上 3 条建议已添加到您的 MCVE 中:

The above 3 suggestions have been added to your MCVE:

import javax.swing.*;
import java.awt.*;

public class Example {

    private static void setup() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500, 500));
        frame.setFocusable(true);

        JSplitPane pane= new JSplitPane();
        pane.setResizeWeight(1.0);
        pane.setRightComponent(new JPanel());

        JPanel myPanel = new JPanel()
        {
            @Override
            public Dimension getMinimumSize()
            {
                return new Dimension(50, 50);
            }
        };
        myPanel.setLayout(new FlowLayout());

        for(int i=0; i<20; i++) myPanel.add(new JLabel("hello"));

        pane.setLeftComponent(myPanel);

        frame.add(pane);

        frame.pack();
        frame.setVisible(true);

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                pane.setDividerLocation(0.5f);
            }
        });
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setup();
            }
        });
    }
}

这篇关于Jsplitpane 自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆