Starting GridBagLayout from top left corner in Java Swing

后端 未结 6 639
执念已碎
执念已碎 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

    Read the section from the Swing tutorial on How to Use GridBagLayout. The secton on "weightx,weighty" states:

    Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-02-13 19:02

    a quick and simple way:

    add a blank JLabel to end of page:

        // your code goes here
        gbc.weightx = 1;
        gbc.weighty = 1;
    
        bg.add(new JLabel(" "), gbc);  // blank JLabel
    
    0 讨论(0)
  • 2021-02-13 19:07

    if you want the grid to go all the way to the top, simply set all your weighty = 0 until the last item, set it to any number greater than 0, it will effectively push the rest of the buttons to the top.

    Don't forget to also increment your button's gridy value.

    Otherwise it will be centered. (the same can be done using gridx and weightx value if you arent using the c.fill = GridBagConstraints.HORIZONTAL property.)

    0 讨论(0)
  • 2021-02-13 19:09

    There's a workaround. You can put the GridBagLayout panel in a BorderLayout panel with BorderLayout.NORTH. Then Component in GridBagLayout panel will start from top position.

    static void test4(){
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(480, 360);
    
        JPanel borderLayoutPanel=new JPanel(new BorderLayout());
        JPanel gridBagLayoutPanel = new JPanel(new GridBagLayout());
        borderLayoutPanel.add(gridBagLayoutPanel, BorderLayout.NORTH);
        frame.add(borderLayoutPanel);
    
        JButton testButton=new JButton("test button");
        GridBagConstraints c = new GridBagConstraints();
        c.gridx=0;
        c.gridy=0;
        gridBagLayoutPanel.add(testButton, c);
    
        frame.setVisible(true);
    }
    

    0 讨论(0)
  • 2021-02-13 19:12

    You need to use your GridBagConstraints' anchor property. This should do it for you:

    frame.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTHWEST;
    frame.add(panel, gbc);
    

    I'm not guaranteeing that you won't have to set other properties of the constraints object to get the layout you desire. In particular, you may need to set weightx and weighty to be 1 so that the panel takes up all of the available space given to it.

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