Starting GridBagLayout from top left corner in Java Swing

后端 未结 6 640
执念已碎
执念已碎 2021-02-13 18:39

I\'m new to Java Swing and I have been struggling to start the GridBagLayout from top left corner so that c.gridx=0 c.gridy=0 will put my object on the top left corner.

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-13 18:49

    For those, who use IDE (e.g. NetBeans), I finally found nice trick: if you want to add components to top and use their preferred sizes: add another empty panel with weighty = 1.0. Copied from auto-generated code (NetBeans):

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.weighty = 1.0;
    jPanelOptions.add(jPanelFiller, gridBagConstraints);
    

提交回复
热议问题