简体   繁体   English

更改Gridlayout Java的间距

[英]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. 我正在制作一个Sudoku程序,并且想更改JFrame的My GridLayout的间距。 Currently, there are 2 JPanels, one on the top and one on the bottom, and the spacing is exactly half. 当前,有两个JPanel,一个在顶部,一个在底部,并且间距恰好是一半。 However, I want the top JPanel to have more space on the JFrame. 但是,我希望顶级JPanel在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. 如果需要JPanel类,则可以安排。

IIRC you can't do this. IIRC,您不能这样做。 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. 另外, GridBagLayout对您可能很有用,但没有它也可以完成。

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. 当我遇到同样的问题时,我记得将边框和网格布局的某种组合“解决”了……其他布局也可能被使用过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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