我怎么做摆动复杂的布局Java [英] How I can do swing complex layout Java

查看:116
本文介绍了我怎么做摆动复杂的布局Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关java swing布局的帮助。我想做任何类似的事情(请看图片):

I need a help about a java swing layout. I want do any thing like that (please see the image):

http://i1374.photobucket.com/albums/ag420/Bruno_Freitas/PROGRAMADEPONTUACcedilAtildeO_zpsebaf314e.jpg

我试过放一个网格包布局,但我无法插入内容为10:00的浮动JPanel(在图像上)。任何人都可以帮助我?

I tried put a grid bag layout, but I could not insert a floatting JPanel with the content 10:00 (on image). Anyone can help me?

推荐答案

在大多数情况下,您通常需要考虑使用多种布局,但在您的情况下, GridBagLayout 应该能够实现基本布局所需的内容

In most cases, you would normally need to consider using multiple layouts, but in your case, a GridBagLayout should be capable of achieving what you want for the basic layout

对于需要扩展多个列的组件,您可以使用 GridBagConstraints#gridwidth 同样适用于需要扩展多行的组件 GridBagConstraints #gridheight

For components that need to expand multiple columns, you can use GridBagConstraints#gridwidth and equally, for components that need to expand multiple rows GridBagConstraints#gridheight

作为一个整体,将布局要求分解为各个责任区域,例如,记分卡本身有自己的布局要求,应该是自包含的组件,这将减少布局复杂性。

As a whole, break down you layout requirements into individual areas of responsibility, for example, the score cards them selves have there own layout requirements and should be self contained components, this will reduce the layout complexity.

已更新

您可以执行(核心)布局单个容器,但这很容易破解,相反,你应该尝试将UI分解成各个部分......

You could do the (core) layout in single container, but that is WAY to easy to break, instead, you should try and break the UI down into sections...

这些是,两个,我看到的核心部分,其中有子部分,但这是我开始的地方...

These are the, two, core sections that I see, there are subsections within them, but this is where I started...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class LayoutTest {

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

    public LayoutTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridheight = GridBagConstraints.REMAINDER;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(new ScoreCardsPane(), gbc);

            gbc.gridx = 1;
            add(new TimePane(), gbc);
        }

    }

    public class TimePane extends JPanel {

        public TimePane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);

            add(createScoreCard(), gbc);
            gbc.gridx++;
            add(createScoreCard(), gbc);

            gbc.gridx = 0;
            gbc.gridy = 2;
            add(createScoreCard(), gbc);
            gbc.gridx++;
            add(createScoreCard(), gbc);

            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.gridwidth = 3;
            add(new TimeCard(), gbc);
        }

    }

    public class TimeCard extends JPanel {

        public TimeCard() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(new JLabel("TEMPO"), gbc);
            JLabel time = new JLabel("10:00");
            time.setBorder(new LineBorder(Color.BLACK));
            add(time, gbc);
        }

    }

    public class ScoreCardsPane extends JPanel {

        public ScoreCardsPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.anchor = GridBagConstraints.WEST;

            JLabel topLabel = new JLabel("PONTOS");
            add(topLabel, gbc);
            JLabel bottomLabel = new JLabel("PONTOS");
            gbc.gridy = 3;
            add(bottomLabel, gbc);

            gbc.anchor = GridBagConstraints.CENTER;
            gbc.gridy = 1;
            gbc.gridwidth = 1;

            add(createScoreCard(), gbc);
            gbc.gridx++;
            add(createScoreCard(), gbc);
            gbc.gridx++;
            add(createScoreCard(), gbc);

            gbc.gridy++;
            gbc.gridx = 0;

            add(createScoreCard(), gbc);
            gbc.gridx++;
            add(createScoreCard(), gbc);
            gbc.gridx++;
            add(createScoreCard(), gbc);

        }

    }

    protected static JPanel createScoreCard() {

        JPanel card = new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 100);
            }

        };
        card.setBackground(Color.RED);

        return card;

    }

}

这篇关于我怎么做摆动复杂的布局Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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