简体   繁体   中英

Changing the Spacing of Gridlayout Java

I am making a Sudoku program, and I want to change the spacing of My GridLayout for my JFrame. Currently, there are 2 JPanels, one on the top and one on the bottom, and the spacing is exactly half. However, I want the top JPanel to have more space on the JFrame. My code is such:

    import java.awt.GridLayout;

    import javax.swing.BoxLayout;
    import javax.swing.JFrame;


    public class GuiFrame1 extends JFrame {
    private static final long serialVersionUID = 1L;
    private static JFrame frame;
    static GridLayout gLayout2 = new GridLayout(2,1);
    private GuiPanel2 panel;
    private GuiPanel1 panel2;
    public GuiFrame1(){

    super("Sudoku");
    setSize(700, 700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(gLayout2);
    panel = new GuiPanel2();
    panel2 = new GuiPanel1();
    setVisible(true);
    add(panel);
    add(panel2);


    }
    public static void main(String[] args) {
        new GuiFrame1();
    }

    }

if you need the JPanel classes, that can be arranged.

IIRC you can't do this. You should try to "emulate" this by placing it inside other layout types -- usually (always?) you are able to achieve any layout you want, by stacking layout types within one another. But grid layout itself, I'm pretty sure, HAS to have equal dimensions everywhere.

Alternatively, GridBagLayout may be useful for you, but it can be done without it.

When I was faced with your same problem, I remember "hacking" together a solution with some combination of border and grid layouts... other layouts may have been used also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM