我需要在添加新组件时动态调整JPanel的大小 [英] I need to make my JPanel resize dynamically as new components are being added to it

查看:147
本文介绍了我需要在添加新组件时动态调整JPanel的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要让用户向我的JFrame添加更多文本字段,这样一旦框架的大小超过其原始值,滚动窗格就会介入。由于我无法将JScrollPane添加到JFrame以便启用滚动我决定放入JFrame上的JPanel并将JPanel对象传递给JScrollPane构造函数。滚动现在可以正常工作,但只能到达JPanel的边框。事情是JPanel的大小保持原样,并没有动态扩展。发生的事情是我的代码中的按钮占用了JPanel的大小为300x300的所有空间,但我想要做的是在这些控件耗尽其原始空间后让JPanel拉伸。请指教。

I need to let users add more text fields to my JFrame so once the size of the frame has exceeded its original value a scroll pane would step in. Since I cannot add JScrollPane to JFrame in order to enable scrolling I decided to put the JPanel on the JFrame and pass the JPanel object into the JScrollPane constructor. Scrolling now works fine but only until it has reached the borders of the JPanel. The thing is the size of JPanel stays as is and is not stretching dynamically. What happens is the buttons in my code are using up all the space of the JPanel being the size of 300x300 but what I want to do is have JPanel stretch once these controls have used up its original space. Please advise.

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;


public class Skrol {
    public static void main(String[] args) {


        JFrame f = new JFrame();
        f.setLayout(new FlowLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();


        p.setPreferredSize(new Dimension(400,400));



        JScrollPane jsp = new JScrollPane(p);

        jsp.setPreferredSize(new Dimension(300,300));
        jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

            for(int i=0;i<100;i++)
                {
                    JButton b = new JButton("Button "+i);
                    p.add(b);
                }
        f.add(jsp);
        f.setSize(new Dimension(600,600));
        f.setLocation(300, 300);
        f.setVisible(true);

    }
}


推荐答案

我将 JPanel 中的布局更改为 GridLayout ,因此LayoutManager的大小只是面板上的组件所取代的。

I changed the Layout in your JPanel to GridLayout, so the Size of it is just handeld by the Layoutmanager depending on the components on the panel.

    JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel(new GridLayout(0, 5));
    JScrollPane jsp = new JScrollPane(p);

    jsp.setPreferredSize(new Dimension(300,300));
    jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

    for (int i = 0; i < 100; i++) {
        JButton b = new JButton("Button " + i);
        p.add(b);
    }

    f.add(jsp, BorderLayout.CENTER);
    f.setLocation(300, 300);
    f.setVisible(true);
    f.pack();

这篇关于我需要在添加新组件时动态调整JPanel的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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