How I can do swing complex layout Java [closed]

白昼怎懂夜的黑 提交于 2019-12-09 03:59:07

问题


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

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?


回答1:


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

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.

Updated

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;

    }

}


来源:https://stackoverflow.com/questions/26411775/how-i-can-do-swing-complex-layout-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!