如何使用GridLayout和多个面板? [英] How do I use GridLayout and multiple Panels?

查看:66
本文介绍了如何使用GridLayout和多个面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用多个 JPanel 容器使此代码看起来像这样?

How do I use multiple JPanel containers to make this code look like this?

这是我的代码应该是什么样的图像,但我无法弄清楚.我只能使用 GridLayout BorderLayout FlowLayout .作为初学者,我们仅涉及基本概念,但我需要更多帮助.

This is the image of what my code is supposed to be like but I cant figure it out. I can only use GridLayout, BorderLayout and FlowLayout. As a beginner, We've only been over basic concepts but I need more help.

我也不允许使用 GridBagLayout .我感谢所有的帮助.

I am also not permitted to use GridBagLayout. I appreciate all the help.

推荐答案

解决复杂计算任务的常用策略是将它们分解为细小,定义明确的可管理任务.分而治之.
这也适用于gui:将设计分为较小的,易于布局的容器.
在这种情况下,请考虑将设计分为3个区域( JPanel s)嵌套在主 JPanel 中:

A common strategy to solve complex computing tasks, is to break them into small, well defined manageable tasks. Divide and conquer.
This also applies to gui: break the design into small, easy to layout containers.
In this case, consider dividing the design into 3 areas (JPanels) nested in a main JPanel:

如果不能使用 GridBagLayout ,则可以使用 BoxLayout 来实现底部面板.
BoxLayout 对于主面板也是有效的选项,以允许不同的子面板(顶部,中央,底部)高度.

If you can't use GridBagLayout you can implement bottom panel using BoxLayout.
BoxLayout is a valid option also for main panel, to allow for different child panels (top, center, bottom) height.

演示:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Lab1 extends JFrame
{
    public Lab1() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel main = new JPanel(new GridLayout(3,1));
        //to allow different child-panels height use BoxLayout
        //BoxLayout boxLayout = new BoxLayout(main, BoxLayout.Y_AXIS);

        add(main);
        JPanel top = new JPanel(new GridLayout(1,3));
        main.add(top);
        top.add(getPanel(Color.RED));
        top.add(getPanel(Color.GREEN));
        top.add(getPanel(Color.BLUE));

        JPanel center = new JPanel(new GridLayout(1,4));
        main.add(center);
        center.add(getPanel(Color.YELLOW));
        center.add(getPanel(Color.CYAN));
        center.add(getPanel(Color.BLACK));
        center.add(getPanel(Color.LIGHT_GRAY));

        JPanel bottom = new JPanel();
        bottom.setLayout(new BoxLayout(bottom, BoxLayout.LINE_AXIS));
        main.add(bottom);

        bottom.add(getPanel(Color.PINK));
        JPanel rightPane =  getPanel(Color.MAGENTA);
        rightPane.setPreferredSize(new Dimension(900, 200));
        bottom.add(rightPane);

        pack();
        setVisible(true);
    }

    private JPanel getPanel(Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        panel.setPreferredSize(new Dimension(300, 200));
        return panel;
    }

    public static void main(String args[])
    {
        new Lab1();
    }
}

这篇关于如何使用GridLayout和多个面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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