【问题标题】:How to get a JScrollPane to resize with its parent JPanel如何让 JScrollPane 使用其父 JPanel 调整大小
【发布时间】:2014-05-22 00:22:55
【问题描述】:

我的问题与这个 (How to get JScrollPanes within a JScrollPane to follow parent's resizing) 类似,但那个问题并不清楚,那里的答案对我没有帮助..

我有这个 SSCCE(使用 MigLayout):

public static final int pref_height = 500;
public static void main(String[] args) {

    JPanel innerPanel = new JPanel(new MigLayout());
    innerPanel.setBorder(new LineBorder(Color.YELLOW, 5));

    for(int i = 0; i < 15; i++) {
        JTextArea textArea = new JTextArea();
        textArea.setColumns(20);
        textArea.setRows(5);
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);
        JScrollPane jsp = new JScrollPane(textArea);

        innerPanel.add(new JLabel("Notes" + i));
        innerPanel.add(jsp, "span, grow");
    }
    JScrollPane jsp = new JScrollPane(innerPanel) {
        @Override
        public Dimension getPreferredSize() {
            setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            Dimension dim = new Dimension(super.getPreferredSize().width + getVerticalScrollBar().getSize().width, pref_height);
            setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            return dim;
        }
    };
    jsp.setBorder(new LineBorder(Color.green, 5));

    JPanel outerPanel = new JPanel();
    outerPanel.setBorder(new LineBorder(Color.RED, 5));
    outerPanel.add(jsp);

    JFrame frame = new JFrame();

    JDesktopPane jdp = new JDesktopPane();
    frame.add(jdp);
    jdp.setPreferredSize(new Dimension(800, 600));
    frame.pack();

    JInternalFrame jif = new JInternalFrame("Title", true, true, true, true);
    jif.pack();
    jif.add(outerPanel);

    jdp.add(jif);
    jif.pack();
    jif.setVisible(true);


    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

我希望 JScrollPane 在父 JPanel 调整大小时调整大小。基本上,我希望绿色边框与红色边框对齐。现在,无论红色边框如何,绿色边框都保持相同的大小(除非您将大小调整得太小)。

【问题讨论】:

  • 搜索@kleopatra 的 miglayout 标记的答案(JScrollPane + 里面的任何内容)

标签: java swing jpanel jscrollpane miglayout


【解决方案1】:
JPanel outerPanel = new JPanel();

JPanel 默认使用 FlowLayout,它始终尊重添加到其中的组件的大小。作为猜测,也许你可以使用:

JPanel outerPanel = new JPanel( new BorderLayout() );

BorderLayout 为添加到面板的组件提供所有可用空间。默认情况下,JInternalFrame 也使用 BorderLayout。因此,由于滚动窗格的所有父组件都使用 BorderLayout,因此所有空间都应该转到滚动窗格。

当您发布 SSCCE 时,您应该使用 JDK 中模拟您的问题的类发布代码,以便每个人都可以测试您的 SSCCE。

【讨论】:

  • 是的,我在最后一刻意识到我的 SSCCE 有 MigLayout,但我很难让这些手工编码的 GUI API 屈服于我的心血来潮。所以我没有费心去尝试没有它......
【解决方案2】:

我注意到这没有使用原始布局的答案,所以这里是一个。

为了在调整父级JPanel 的大小时调整JScrollPane 的大小,您需要做两件事。

1) 将面板的布局设置为增长。这可以使用以下代码。

new MigLayout("",  //Layout Constraints
    "grow",        //Column Constraints
    "grow");       //Row Constraints

2) 设置组件增长。这就像在 add() 函数中添加一个额外的参数一样简单。

add(jsp, "grow");

额外
为了在调整JScrollPane 大小时使JTextArea 列增长,您可以更改布局以仅更改第二列。例如

new MigLayout("",                        //Layout Constraints
    "[/*Column 1*/][grow /*Column 2*/]", //Column Constraints
    "");                                 //Row Constraints

另外,我建议您使用wrap 而不是span 来使用下一行,因为span 指的是使用这么多列。例如span 2 //Means use 2 columns for this component。这意味着当您将 jsp 添加到 innerPanel 时,它将变为

innerPanel.add(jsp, "wrap, grow");

已编辑 SSSCE

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import net.miginfocom.swing.MigLayout;

public class JSPR extends JFrame {
    public static final int pref_height = 500;
    public static void main(String[] args) {

        JPanel innerPanel = new JPanel(new MigLayout("", "[][grow]", ""));
        innerPanel.setBorder(new LineBorder(Color.YELLOW, 5));

        for(int i = 0; i < 15; i++) {
            JTextArea textArea = new JTextArea();
            textArea.setColumns(20);
            textArea.setRows(5);
            textArea.setWrapStyleWord(true);
            textArea.setLineWrap(true);
            JScrollPane jsp = new JScrollPane(textArea);

            innerPanel.add(new JLabel("Notes" + i));
            innerPanel.add(jsp, "wrap, grow");
        }
        JScrollPane jsp = new JScrollPane(innerPanel) {
            @Override
            public Dimension getPreferredSize() {
                setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                Dimension dim = new Dimension(super.getPreferredSize().width + getVerticalScrollBar().getSize().width, pref_height);
                setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
                return dim;
            }
        };
        jsp.setBorder(new LineBorder(Color.green, 5));

        JPanel outerPanel = new JPanel(new MigLayout("", "grow", "grow"));
        outerPanel.setBorder(new LineBorder(Color.RED, 5));
        outerPanel.add(jsp, "grow");

        JFrame frame = new JFrame();

        JDesktopPane jdp = new JDesktopPane();
        frame.add(jdp);
        jdp.setPreferredSize(new Dimension(800, 600));
        frame.pack();

        JInternalFrame jif = new JInternalFrame("Title", true, true, true, true);
        jif.pack();
        jif.add(outerPanel);

        jdp.add(jif);
        jif.pack();
        jif.setVisible(true);


        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 2020-08-16
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多