如何使JScrollPane与其父级JPanel一起调整大小 [英] How to get a JScrollPane to resize with its parent JPanel

查看:151
本文介绍了如何使JScrollPane与其父级JPanel一起调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与此类似(如何在JScrollPane中获取JScrollPanes以遵循父级的大小调整),但是这个问题尚不清楚,那里的答案也无济于事.

My question is similar to this one (How to get JScrollPanes within a JScrollPane to follow parent's resizing), but that question wasn't clear and the answer there didn't help me..

我有这个SSCCE(使用 MigLayout ):

I have this SSCCE (using 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);
}

每当父级JPanel调整大小时,我都希望JScrollPane调整大小.基本上,我希望绿色边框与红色边框对齐.现在,无论红色边框如何,绿色边框都会保持不变(除非您将尺寸调整得太小).

I want the JScrollPane to resize whenever the parent JPanel is resized. Basically, I want the green border to line up with the red border. Right now, the green border stays the same size no matter the red border (unless you resize too small).

推荐答案

JPanel outerPanel = new JPanel();

默认情况下,JPanel使用FlowLayout,它始终尊重添加到其中的组件的大小.推测一下,也许您可​​以使用:

A JPanel uses a FlowLayout by default which always respects the size of the component added to it. As a guess, maybe you can use:

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

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

A BorderLayout give all the space available to the component added to the panel. By default a JInternalFrame also uses a BorderLayout. So since all the parent components of your scroll pane use a BorderLayout all the space should go to the scroll pane.

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

When you post a SSCCE you should post code using classes from the JDK that simulates your problem so that everybody can test your SSCCE.

这篇关于如何使JScrollPane与其父级JPanel一起调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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