Java GridBagLayout - How to position my components gap-less and one by one?

匿名 (未验证) 提交于 2019-12-03 09:52:54

问题:

I'm using GridBagLayout to place my GUI components by the following code, wanting the components lay one by one in a column, without any gaps :

import java.awt.GridBagConstraints; import java.awt.GridBagLayout;  import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel;  public class TestGUI extends JFrame{      public TestGUI(){          JPanel bigPanel = new JPanel(new GridBagLayout());         JPanel panel_a = new JPanel();         JButton btnA = new JButton("button a");         panel_a.add(btnA);          JPanel panel_b = new JPanel();         JButton btnB = new JButton("button b");         panel_b.add(btnB);          GridBagConstraints c = new GridBagConstraints();         c.gridx = 0;         c.gridy = 0;         c.weighty = 1D;         c.fill = GridBagConstraints.HORIZONTAL;         c.anchor = GridBagConstraints.NORTH;         bigPanel.add(panel_a, c);          c.gridx = 0;         c.gridy = 1;         c.fill = GridBagConstraints.HORIZONTAL;         bigPanel.add(panel_b, c);          this.add(bigPanel);     }      public static void main(String[] args) {          TestGUI gui = new TestGUI();         gui.setVisible(true);         gui.pack();     } }

I wish the panels will be shown one by one in the column. but now i got this :

As i am going to add some more components in the bigPanel, and required some more customization to the layout, so i need to use GridBagLayout instead of other Layout.

回答1:

You need to add an extra component so that it will fill the rest of the available space and push the two button-panels to the top. When you will add more components, you can of course remove that component.

Another option (without requiring an extra component) would have been to set weighty=1.0 for the panel_b and anchor=NORTH, but then you would have to change that when you add more components.

import java.awt.GridBagConstraints; import java.awt.GridBagLayout;  import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities;  public class TestGUI extends JFrame {      public TestGUI() {          JPanel bigPanel = new JPanel(new GridBagLayout());         JPanel panel_a = new JPanel();         JButton btnA = new JButton("button a");         panel_a.add(btnA);          JPanel panel_b = new JPanel();         JButton btnB = new JButton("button b");         panel_b.add(btnB);          GridBagConstraints c = new GridBagConstraints();         c.gridwidth = GridBagConstraints.REMAINDER;         c.fill = GridBagConstraints.HORIZONTAL;         c.weightx = 1.0;         bigPanel.add(panel_a, c);         bigPanel.add(panel_b, c);         c.weighty = 1.0;         // Temporary panel to fill the rest of the bigPanel         bigPanel.add(new JPanel(), c);         this.add(bigPanel);     }      public static void main(String[] args) {         SwingUtilities.invokeLater(new Runnable() {             @Override             public void run() {                 TestGUI gui = new TestGUI();                 gui.pack();                 gui.setVisible(true);             }         });     } }


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