Which Layout Manager do you use?

2019-03-18 02:12发布

What java GUI layout manager does everyone use? Lately, I have been using MigLayout, which has some powerful component controls. Just wanted to see what other developers are using other than the standard JDK ones.

22条回答
beautiful°
2楼-- · 2019-03-18 02:35

I use to go for GridBagLayout for the control, but since java1.6 I'm going to use GroupLayout Is awsome.

Here an screenshot and sample code to use it!.

alt text http://img145.imageshack.us/img145/7844/screenshot1dz8.png

    private void layoutComponents(){
        JPanel panel = new JPanel();

        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        layout.setAutoCreateGaps(true);

        layout.setAutoCreateContainerGaps(true);
        SequentialGroup hGroup = layout.createSequentialGroup();

        JLabel nameLbl  = new JLabel("Name");
        JLabel countLbl = new JLabel("Amount");
        JLabel dateLbl  = new JLabel("Date(dd/MM/yy)");
        hGroup.addGroup(layout.createParallelGroup().
                addComponent(nameLbl).
                addComponent(countLbl).
                addComponent(dateLbl).
                addComponent(go));

        hGroup.addGroup(layout.createParallelGroup().
                addComponent(name).
                addComponent(count).
                addComponent(date));

        layout.setHorizontalGroup(hGroup);

        SequentialGroup vGroup = layout.createSequentialGroup();

        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(nameLbl).addComponent(name));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(countLbl).addComponent(count));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(dateLbl).addComponent(date));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
                addComponent(go));
        layout.setVerticalGroup(vGroup);

        frame.add( panel , BorderLayout.NORTH );
        frame.add( new JScrollPane( textArea ) );
    }
查看更多
劫难
3楼-- · 2019-03-18 02:37

I was sick of all those layoutmanagers that needed alot of setup, werent very readable or exhausting to do manually, so I wrote my own very simple laoutmanager which uses the abstraction of two photocorners keeping each component in place. You can add your component like this: parent.add(child,"topleft(0, 0.5)bottomright(0.5,1.0)");

Have a look here https://github.com/hageldave/UsefulStuff/blob/master/src/PhotoCornersLayout.java ;) you're responisble for a correct layout yourself though, cause it's not checking overlappings or other shortcommings of your layout.

查看更多
Juvenile、少年°
4楼-- · 2019-03-18 02:38

GridBagLayout is powerful but quite primitively: the code that wires up the layout is very verbose. This utility library (actual just 1 jar file containing about 10 classes) simplifies a lot of works: http://code.google.com/p/painless-gridbag/ The following snippet is quoted from the home page of that site:

    PainlessGridBag gbl = new PainlessGridBag(getContentPane(), false);

    gbl.row().cell(lblFirstName).cell(txtFirstName).fillX()
             .cell(lblFamilyName).cell(txtFamilyName).fillX();
    gbl.row().cell(lblAddress).cellXRemainder(txtAddress).fillX();

    gbl.doneAndPushEverythingToTop();
查看更多
贪生不怕死
5楼-- · 2019-03-18 02:41

The only layout manager that I have found, that I actually like is the Relative Layout Manager. The Relative Layout Manager works in a way that is consistent with how dialog boxes are conceptually organized. One draw-back is that while this layout manager deals with additive constraints. It does not seem to deal with ratio constraints. Fortunately it is pretty well designed, and I was able to implement this feature.

查看更多
迷人小祖宗
6楼-- · 2019-03-18 02:42

Spring layout which was developed for the mantissa gui builder which is part of netbeans.

查看更多
smile是对你的礼貌
7楼-- · 2019-03-18 02:44

I use GridBagLayout for form like layouts, use BorderLayout for simple layouts, and FlowLayout for number of horizontal icons/buttons that have some spaces in between. Netbeans is also a good GUI builder that can avoid a lot of tedious layout codings to save your time.

查看更多
登录 后发表回答