Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的GridBagLayout不起作用_Java_Jpanel_Gridbaglayout_Gridx - Fatal编程技术网

Java中的GridBagLayout不起作用

Java中的GridBagLayout不起作用,java,jpanel,gridbaglayout,gridx,Java,Jpanel,Gridbaglayout,Gridx,我一直在试着按我想要的方式调整我的显示器,但它似乎不起作用。我想使用GridBagLayout制作如下内容: 我找到了一段代码,并对其进行了编辑: public class GBLPanel extends JPanel { private static final long serialVersionUID = 1L; GridBagConstraints gbc = new GridBagConstraints(); public GBLPanel(Dimen

我一直在试着按我想要的方式调整我的显示器,但它似乎不起作用。我想使用GridBagLayout制作如下内容:

我找到了一段代码,并对其进行了编辑:

public class GBLPanel extends JPanel 
{
    private static final long serialVersionUID = 1L;

    GridBagConstraints gbc = new GridBagConstraints();

    public GBLPanel(Dimension appdim)
    {
        GridBagConstraints c = new GridBagConstraints();

        setLayout(new GridBagLayout());
        add(gbcComponent(0,0,2,1,0,0), gbc);               
        add(gbcComponent(0,1,1,1,0,50), gbc);            
        add(gbcComponent(1,1,1,1,0,50), gbc); 

    }

     private JPanel gbcComponent(int x, int y, int w, int h, int ipadyx, int ipadyy){

        gbc.gridx = x; 
        gbc.gridy = y;
        gbc.gridwidth = w;
        gbc.gridheight = h;

        gbc.weightx = 1.0;
        gbc.weighty = 1.0;

        gbc.ipadx=ipadyx;
        gbc.ipady=ipadyy;

        gbc.fill = GridBagConstraints.BOTH;
        JPanel panel = new JPanel();
        JTextField text = new JTextField("(" + w + ", " + h + ")");
        panel.setBorder(new TitledBorder("(" + x + ", " + y + ")"));        
        panel.add(text);
        return panel;

    }

}


我不知道如何塑造它,因为我想,任何人都可以帮助?非常感谢

A
BorderLayout
对您来说可能更容易做到这一点

但是如果您想要/需要使用
GridBagLayout
,当前的问题是您将每个面板的x和y的
权重设置为1。这意味着它们将均匀分布

通过这样做,尝试更改它们以反映您想要的值

public GBLPanel(Dimension appdim)
{
    GridBagConstraints c = new GridBagConstraints();

    setLayout(new GridBagLayout());
    // Pass in weights also
    add(gbcComponent(0,0,2,1,0,0, 1, 0.25), gbc);  // 100% x and 25% y
    add(gbcComponent(0,1,1,1,0,50, 0.25, 0.75), gbc); // 25% x and 75% y
    add(gbcComponent(1,1,1,1,0,50, 0.75, 0.75), gbc); // 75% x and 75% y

}

private JPanel gbcComponent(int x, int y, int w, int h, int ipadyx, int ipadyy, double wx, double wy)
{
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = w;
    gbc.gridheight = h;

    gbc.weightx = wx;  // Set to passed in values here
    gbc.weighty = wy;

    gbc.ipadx=ipadyx;
    gbc.ipady=ipadyy;

    gbc.fill = GridBagConstraints.BOTH;
    JPanel panel = new JPanel();
    JTextField text = new JTextField("(" + w + ", " + h + ")");
    panel.setBorder(new TitledBorder("(" + x + ", " + y + ")"));
    panel.add(text);
    return panel;

}

非常感谢你的回答,爪哇恶魔,你帮了我很多。我实际上使用了BorderLayout来制作第一张图片,但我最近开始学习Java,不知道如何制作ResizeHandler。我还读到GridBagLayout是最灵活的,所以我想试试。