Java GridBagLayout automated construction

前端 未结 1 1355
轻奢々
轻奢々 2021-01-13 15:36

I designed a GUI using GridBagLayout and GridBagConstraints. It contains a variable number of rows, each with one of a few possible column layouts. To test the code, I com

相关标签:
1条回答
  • 2021-01-13 16:14

    Rule number 1 for Swing layouts: Whatever you do, do not use GridBagLayout. GridBagLayout was fine in 1998. Its design is problematic, it is buggy, and it has not evolved. Code is extremely verbose, hard to write, hard to understand and hard to maintain.

    I recommend MigLayout, it's the most versatile and intuitive layout manager I have seen. Look at the quick start guide on the MigLayout site, it's a lot less difficult than GridBagLayout and much more powerful. Here is your example in in MigLayout and I've shown you how to refactor your row types:

    import net.miginfocom.swing.MigLayout;
    import java.awt.*;
    import javax.swing.*;
    
    public class GridBagLayoutDemo {
        final static boolean RIGHT_TO_LEFT = false;
    
        public static void addComponentsToPane(Container pane) {
            if (RIGHT_TO_LEFT) {
                pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            }
            pane.setLayout(new MigLayout("insets 0, gap 0, wrap", "[][fill, grow][fill, grow][][]", "[fill]"));
    
            addType1(pane, Color.BLACK, Color.BLUE, Color.GREEN, Color.RED, Color.BLUE);
            addType2(pane, Color.MAGENTA, Color.PINK, Color.BLACK, Color.PINK);
            addType3(pane, Color.GRAY, Color.ORANGE, Color.RED, Color.ORANGE);
            addType3(pane, Color.BLACK, Color.WHITE, Color.GREEN, Color.BLUE);
            addType3(pane, Color.DARK_GRAY, Color.YELLOW, Color.WHITE, Color.ORANGE);
            addType2(pane, Color.GREEN, Color.BLUE, Color.RED, Color.BLACK);
            addType2(pane, Color.DARK_GRAY, Color.WHITE, Color.YELLOW, Color.GREEN);
        }
    
        private static void addType1(Container pane, Color c1, Color c2, Color c3, Color c4, Color c5) {
            pane.add(new TestPanel(c1));
            pane.add(new TestPanel(c2));
            pane.add(new TestPanel(c3));
            pane.add(new TestPanel(c4));
            pane.add(new TestPanel(c5));
        }
    
        private static void addType2(Container pane, Color c1, Color c2, Color c3, Color c4) {
            pane.add(new TestPanel(c1));
            pane.add(new TestPanel(c2), "spanx 2");
            pane.add(new TestPanel(c3));
            pane.add(new TestPanel(c4));
        }
    
        private static void addType3(Container pane, Color c1, Color c2, Color c3, Color c4) {
            pane.add(new TestPanel(c1));
            pane.add(new TestPanel(c2), "spanx 2, pushy, hmin pref+40");
            pane.add(new TestPanel(c3));
            pane.add(new TestPanel(c4));
        }
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("MigLayout Practice");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            addComponentsToPane(frame.getContentPane());
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    class TestPanel extends JPanel {
        public TestPanel(Color myColor) {
            this.setBackground(myColor);
        }
    }
    

    This yields the exact same layout as your example. Probably you want hmin 40 instead of hmin pref+40, the latter produces the same result as setting ipady=40 in GridBagConstraints.

    And please use the upper case constants in the Color class, the lower-case constants should really be deprecated.

    For anyone who wonders what this layout looks like, here it is:

    enter image description here

    0 讨论(0)
提交回复
热议问题