GridBagLayOut向上跨越列?

时间:2014-09-02 03:50:08

标签: java swing layout-manager gridbaglayout

我可以在GridBagLayout中向上或向后跨越按钮吗? 这里,按钮五高度为2,宽度为1,但不起作用。有一个空间向上,这是我想要按钮五向上跨越的地方。

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);
    }
}

1 个答案:

答案 0 :(得分:3)

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

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

会导致您的评论要求,例如......

GridBagLayout

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);
    }
}

但是,等一下,第二排/第三排发生了什么?第二行基本上是折叠的,它不再包含任何东西(可以用来计算可行的高度)

你可以使用"空"来解决这个问题。有助于它做出决定的组件......

GridBagLayout

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);
    }
}