调整 JScrollPane 大小的最佳实践

Best practice to resize JScrollPane

我在本论坛(this one 例如)中阅读了一些已回答的问题,其中强烈建议避免使用 setXXXSize() 方法在 swing 应用程序中调整组件大小。

所以,对于我的问题,我想知道如何最好地调整 JScrollPane 的大小,以避免其父面板在没有任何控制的情况下增加其大小。

在写一些代码之前,我想描述一下真实情况,因为我会post一个"toy example"。

在我的 JFrame 中,我目前正在为我的内容窗格使用边框布局。在 BorderLayout.CENTER 有一个 JPanel,我可以在其中进行一些自定义绘画。 在 BorderLayout.EAST 处有一个 JPanel(比如 eastPanel),其中包含另一个面板中的一些组件(此面板将添加到 BorderLayout.NORTH 处的 eastPanel),以及一个包含 JTable 的 JScrollPane(添加到 [= 处的 eastPanel) 37=]).这个 table 会有很多行。 由于我希望 eastPanel 的高度与 centerPanel 的高度相同,因此我需要一些方法来避免 JScrollPane 增加其大小,以便尝试显示尽可能多的行。

目前除了在包含滚动窗格的 eastPanel 上调用 setPreferredSize 之外我找不到其他解决方案,但我不得不承认我讨厌这种解决方案。

示例代码

在此代码示例中,我在 eastPanel 的北部和 JScrollPane 内部添加了一些随机标签,因为我的目的是 post 一个简短的代码示例。 但是,情况与我上面描述的情况非常相似。 如果不使用这 "terrible" 行代码,我无法解决我的问题:

eastPanel.setPreferredSize(new Dimension(eastPanel.getPreferredSize().width, centerPanel.getPreferredSize().height));

对于像这样的简单情况,我想避免使用更复杂的布局。我错过了什么吗?此外,设置空白边框是否是一种接受table 设置面板大小的方法,我将在其中进行一些自定义绘画?

代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Test
{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {                       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    new TestFrame().setVisible(true);
                }
                catch(Exception exception) {
                    JOptionPane.showMessageDialog(null, "Fatal error while initialiing application", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }
}
class TestFrame extends JFrame 
{
    public TestFrame() {
        super("Test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel pane = new JPanel(new BorderLayout(20, 0));
        pane.setBorder(new EmptyBorder(20, 20, 20, 20));
        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.WHITE);
        centerPanel.setBorder(new EmptyBorder(400, 400, 0, 0));
        // centerPanel.setPreferredSize(new Dimension(400, 400));
        JPanel eastPanel = new JPanel(new BorderLayout(0, 20));
        JPanel labelsContainer = new JPanel(new GridLayout(0, 1));
        for(int i=0;i<7;i++) labelsContainer.add(new JLabel(String.valueOf(i)));
        eastPanel.add(labelsContainer, BorderLayout.NORTH);
        JPanel moreLabelsContainer = new JPanel(new GridLayout(0, 1));
        for(int i=7;i<70;i++) moreLabelsContainer.add(new JLabel(String.valueOf(i)));
        JScrollPane scroll = new JScrollPane(moreLabelsContainer, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        eastPanel.add(scroll, BorderLayout.CENTER);
        eastPanel.setPreferredSize(new Dimension(eastPanel.getPreferredSize().width, centerPanel.getPreferredSize().height));
        pane.add(centerPanel, BorderLayout.CENTER);
        pane.add(eastPanel, BorderLayout.EAST);
        setContentPane(pane);
        pack();
        setLocationRelativeTo(null);
    }
}

感谢您的帮助!

我不知道布局管理器会根据面板中特定组件的高度来限制面板的高度。

一种方法是自定义包含两个子组件的父面板的行为。

代码可能类似于:

@Override
public Dimension getPreferredSize()
{
    Dimension d = super.getPreferredSize();

    BorderLayout layout = (BorderLayout)getLayout();
    Component center = layout.getLayoutComponent(BorderLayout.CENTER);

    int centerHeight = center.getPreferreidSize().height;

    if (d.height > centerHeight)
        d.height = centerHeight;

    return d;
}

这种方法将允许基于中心的组件动态计算高度。

另一种选择是编写您自己的布局管理器。然后您可以从布局管理器中控制这种类型的逻辑。

Also, is setting that empty border an acceptable way to set the size of the panel where i will do some custom painting?

我将 getPreferredSize() 覆盖为 return 适当的维度。

通过使用 EmptyBorder,您将失去向面板添加真正边框的能力,因此我不推荐它。