我怎么做摆动复杂的布局Java

时间:2014-10-16 18:51:52

标签: java swing gridbaglayout miglayout

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

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

我尝试了一个网格包布局,但我无法插入内容为10:00的浮动JPanel(在图像上)。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

<强>更新

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

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

CoreSections

enter image description here

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;

    }

}