如何在Borderlayout的.CENTER中创建组件占用所有中心空间并使用应用程序调整大小? [英] How does one make a component in the .CENTER of a Borderlayout occupy all center space and resize with the app?

查看:440
本文介绍了如何在Borderlayout的.CENTER中创建组件占用所有中心空间并使用应用程序调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的app / JFrame,使用Borderlayout,顶部或北部有一个工具栏,底部或南部有一个状态栏,中间有一个JPanel.JTabbedPane.JScrollPane.JTable。 JPanel总是一个固定的大小,可以使用各种组合应用的各种set * Size()方法对各种组件进行粗略调整。但它始终是一个固定的大小,并始终有东西差距。北部和南部组件保持固定高度,并按照预期水平调整大小。

My app/JFrame, using Borderlayout, has a toolbar at the top or north, a statusbar at the bottom or south and a JPanel.JTabbedPane.JScrollPane.JTable in the center. The JPanel is always a fixed size which is roughly adjustable using the various set*Size() methods applied in various combinations to the various components. But it's always a fixed size and always has east and west gaps. The north and south components stay fixed height and resize horizontally as one would expect.

当然这不是一个新的或独特的设计。

Surely this is not a new or unique design.

这是正常行为吗?
我错过了一些技巧吗?

Is this normal behaviour? Is there some trick I've missed?

推荐答案

这是保留默认 FlowLayout JPanel 并将面板添加到 BorderLayout 的中心。下面的示例比较具有 FlowLayout 的面板,默认值或 GridLayout 。相比之下,这两个被添加到 GridLayout ,这允许以类似于 BorderLayout 中心的方式进行扩展。

This is characteristic of retaining the default FlowLayout of JPanel and adding the panel to the center of a BorderLayout. The example below compares panels having FlowLayout, the default, or GridLayout. For contrast, the two are added to a GridLayout, which allows expansion in a manner similar to that of BorderLayout center.

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;

/** @see http://stackoverflow.com/questions/5822810 */
public class LayoutPanel extends JPanel {

    public LayoutPanel(boolean useGrid) {
        if (useGrid) {
            this.setLayout(new GridLayout());
        } // else default FlowLayout
        this.add(new JTree());
    }

    private static void display() {
        JFrame f = new JFrame("LayoutPanels");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(1, 0));
        f.add(new LayoutPanel(false));
        f.add(new LayoutPanel(true));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }
}

这篇关于如何在Borderlayout的.CENTER中创建组件占用所有中心空间并使用应用程序调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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