如何使用其父JPanel来调整JScrollPane的大小

时间:2014-04-09 02:30:24

标签: java swing jpanel jscrollpane miglayout

我的问题与此问题类似(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就会调整大小。基本上,我希望绿色边框与红色边框对齐。现在,绿色边框保持相同的大小,无论红色边框(除非你调整太小)。

2 个答案:

答案 0 :(得分:5)

JPanel outerPanel = new JPanel();

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

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

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

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

答案 1 :(得分:0)

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

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

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

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

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

add(jsp, "grow");

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

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);
    }
}