我可以在GridBagLayout中向上或向后扩展按钮吗?
在此,按钮5的高度为2且宽度为1,但不起作用。左边还有一个空间,这是我希望按钮5向上跨越的地方。

public class GridBag2 extends JPanel
{
    GridBagConstraints constraints = new GridBagConstraints();

    public static void main (String args [])
    {
        JFrame fr = new JFrame("GridBag2 Span R&C");
        fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
        fr.setSize(600,600);
        fr.add(new GridBag2());
        fr.setVisible(true);

    }

    GridBag2()
    {
        setLayout(new GridBagLayout());
        constraints.fill=GridBagConstraints.BOTH;
        addGB(new JButton("one"),0,0,1.0,1.0,1,1);
        addGB(new JButton("two"),1,0,1.0,1.0,1,1);
        addGB(new JButton("three"),2,0,1.0,1.0,1,1);
        addGB(new JButton("four"),1,1,1,1,1,2); //spans 2 rows
        addGB(new JButton("five"),0,2,1,1,2,1); //spans 2 columns

    }

    void addGB(Component comp,int gx, int gy, double wx, double wy, int h, int w)
    {
        constraints.gridx=gx;
        constraints.gridy=gy;
        constraints.weightx=wx;
        constraints.weighty=wy;
        constraints.gridheight=h;
        constraints.gridwidth=w;
        this.add(comp,constraints);
    }
}

最佳答案

根据您的评论,通过工厂方法传递的坐标没有意义...

addGB(new JButton("four"),1,1,1,1,1,2); //spans 2 rows
addGB(new JButton("five"),0,2,1,1,2,1); //spans 2 columns


基本上,您说的是要跨越2行,然后跨越2列,但是参数的计算方式是错误的...

addGB(new JButton("four"),1,1,1,1,2,1); //spans 2 rows
addGB(new JButton("five"),0,2,1,1,1,2); //spans 2 columns


例如,将导致您的评论提出要求。



import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GridBag2 extends JPanel {

    GridBagConstraints constraints = new GridBagConstraints();

    public static void main(String args[]) {
        JFrame fr = new JFrame("GridBag2 Span R&C");
        fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
        fr.setSize(600, 600);
        fr.add(new GridBag2());
        fr.setVisible(true);

    }

    GridBag2() {
        setLayout(new GridBagLayout());
        constraints.fill = GridBagConstraints.BOTH;
        addGB(new JButton("one"), 0, 0, 1.0, 1.0, 1, 1);
        addGB(new JButton("two"), 1, 0, 1.0, 1.0, 1, 1);
        addGB(new JButton("three"), 2, 0, 1.0, 1.0, 1, 1);
        addGB(new JButton("four"), 1, 1, 1, 1, 2, 1); //spans 2 rows
        addGB(new JButton("five"), 0, 2, 1, 1, 1, 2); //spans 2 columns

    }

    void addGB(Component comp, int gx, int gy, double wx, double wy, int h, int w) {
        constraints.gridx = gx;
        constraints.gridy = gy;
        constraints.weightx = wx;
        constraints.weighty = wy;
        constraints.gridheight = h;
        constraints.gridwidth = w;
        this.add(comp, constraints);
    }
}


但是,等等,第二/第三行发生了什么?第二行基本上已折叠,不再包含任何内容(可用于计算可行高度)

您可以使用“空”组件解决此问题,这将有助于它做出决定...



import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GridBag2 extends JPanel {

    GridBagConstraints constraints = new GridBagConstraints();

    public static void main(String args[]) {
        JFrame fr = new JFrame("GridBag2 Span R&C");
        fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
        fr.setSize(600, 600);
        fr.add(new GridBag2());
        fr.setVisible(true);

    }

    GridBag2() {
        setLayout(new GridBagLayout());
        constraints.fill = GridBagConstraints.BOTH;
        addGB(new JButton("one"), 0, 0, 1.0, 1.0, 1, 1);
        addGB(new JButton("two"), 1, 0, 1.0, 1.0, 1, 1);
        addGB(new JButton("three"), 2, 0, 1.0, 1.0, 1, 1);
        addGB(new JLabel(), 0, 1, 1, 1, 1, 1); //spans 2 rows
        addGB(new JButton("four"), 1, 1, 1, 1, 2, 1); //spans 2 rows
        addGB(new JButton("five"), 0, 2, 1, 1, 1, 2); //spans 2 columns

    }

    void addGB(Component comp, int gx, int gy, double wx, double wy, int h, int w) {
        constraints.gridx = gx;
        constraints.gridy = gy;
        constraints.weightx = wx;
        constraints.weighty = wy;
        constraints.gridheight = h;
        constraints.gridwidth = w;
        this.add(comp, constraints);
    }
}

09-26 19:25