GridBagLayout的一些问题

时间:2014-09-19 21:04:11

标签: java swing gridbaglayout

我正在尝试理解Swing的基本概念,目前我正在研究GridBagLayout。无论如何,我只想使用GridBagLayout在图片中生成布局:http://i.imgur.com/Sa59qNz.jpg

我设法生成了一个嵌入了gridBagHelper的面板gridBagHelper,其中gridbag布局位于主框架内,并且还有gridbaglayout。

以下是产生所需布局的两个类:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JFrame;
import Borders.BorderPanel;

public class GridBagFrame extends JFrame
{
private static final long serialVersionUID = 1L;
private GridBagLayout gbl = new GridBagLayout();
private GridBagHelper gridBagHelper;


public GridBagFrame()
{
    this.setSize(200,200);
    this.setLocation(200,200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(new Dimension(600,600));
    this.getContentPane().setLayout(gbl);

    gridBagHelper = new GridBagHelper();
    BorderPanel panelA = new BorderPanel("A");
    BorderPanel panelB = new BorderPanel("B");
    BorderPanel panelD = new BorderPanel("D");
    BorderPanel panelF = new BorderPanel("F");
    BorderPanel panelG = new BorderPanel("G");
    BorderPanel panelH = new BorderPanel("H");
    BorderPanel panelJ = new BorderPanel("J");

    this.getContentPane().add(panelA);
    this.getContentPane().add(panelB);
    this.getContentPane().add(gridBagHelper);
    this.getContentPane().add(panelD);
    this.getContentPane().add(panelF);
    this.getContentPane().add(panelG);
    this.getContentPane().add(panelH);
    this.getContentPane().add(panelJ);

    easyConstraints(gbl, panelA, 1, 3, 0, 0, 1.0, 1.0);
    easyConstraints(gbl, panelB, 1, 2, 1, 0, 3.0, 2.0);
    easyConstraints(gbl, gridBagHelper, 1, 3, 2, 0, 1.0, 1.0);
    easyConstraints(gbl, panelD, 1, 2, 3, 0, 1.0, 2.0);
    easyConstraints(gbl, panelF, 1, 1, 1, 2, 3.0, 2.0);
    easyConstraints(gbl, panelG, 1, 1, 3, 2, 1.0, 2.0);
    easyConstraints(gbl, panelH, 2, 1, 0, 3, 1.0, 1.0);
    easyConstraints(gbl, panelJ, 2, 1, 2, 3, 1.0, 1.0);

}

private void easyConstraints(GridBagLayout GLB, JComponent comp, int w,
        int h, int x, int y, double wx, double wy)
{
    GridBagConstraints constraints= new GridBagConstraints();
    constraints.fill=GridBagConstraints.BOTH;
    constraints.gridwidth = w;
    constraints.gridheight = h;
    constraints.gridx = x;
    constraints.gridy = y;
    constraints.weightx = wx;
    constraints.weighty = wy;
    GLB.setConstraints(comp, constraints);
}


public static void main (String[] args)
{
    GridBagFrame gridBagFrame = new GridBagFrame();
    gridBagFrame.setVisible(true);
}
}

和面板C和E的辅助类:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JPanel;

import Borders.BorderPanel;

public class GridBagHelper extends JPanel
{
private static final long serialVersionUID = 1L;
private GridBagLayout gbl = new GridBagLayout();

public GridBagHelper()
{
    this.setSize(200,200);
    this.setLocation(200,200);
    this.setSize(new Dimension(600,600));
    this.setLayout(gbl);
    BorderPanel panelC = new BorderPanel("C");
    BorderPanel panelE = new BorderPanel("E");

    this.add(panelC);
    this.add(panelE);

    easyConstraints(gbl, panelC, 1, 1, 0, 0, 1.0, 1.0);
    easyConstraints(gbl, panelE, 1, 3, 0, 1, 1.0, 3.0);
}

private void easyConstraints(GridBagLayout GLB, JComponent comp, int w,
        int h, int x, int y, double wx, double wy)
{
    GridBagConstraints constraints= new GridBagConstraints();
    constraints.fill=GridBagConstraints.BOTH;
    constraints.gridwidth = w;
    constraints.gridheight = h;
    constraints.gridx = x;
    constraints.gridy = y;
    constraints.weightx = wx;
    constraints.weighty = wy;
    GLB.setConstraints(comp, constraints);
}

}

与此同时,我花了很多时间试图让它只使用一个没有运气的gridbaglayout工作。没有办法说服borderpanelC不扩展,也没办法说服borderpanelE从B中间开始。我想知道是否有人知道如何让它只在一个框架中工作只有一个gridbaglayout,什么是宽度,高度和重量的正确值。 非常感谢你提前!

编辑: 好的,如果它帮助我这里是我尝试的

  import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;

  import javax.swing.JComponent;
  import javax.swing.JFrame;
  import Borders.BorderPanel;

  public class GridBagFrameBad extends JFrame
{
private static final long serialVersionUID = 1L;
private GridBagLayout gbl = new GridBagLayout();

public GridBagFrameBad()
{
    this.setTitle("This is not supposed to work");
    this.setSize(200,200);
    this.setLocation(200,200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(new Dimension(600,600));
    this.getContentPane().setLayout(gbl);


    BorderPanel panelA = new BorderPanel("A");
    BorderPanel panelB = new BorderPanel("B");
    BorderPanel panelC = new BorderPanel("C");
    BorderPanel panelD = new BorderPanel("D");
    BorderPanel panelE = new BorderPanel("E");
    BorderPanel panelF = new BorderPanel("F");
    BorderPanel panelG = new BorderPanel("G");
    BorderPanel panelH = new BorderPanel("H");
    BorderPanel panelJ = new BorderPanel("J");

    this.getContentPane().add(panelA);
    this.getContentPane().add(panelB);
    this.getContentPane().add(panelC);
    this.getContentPane().add(panelD);
    this.getContentPane().add(panelE);
    this.getContentPane().add(panelF);
    this.getContentPane().add(panelG);
    this.getContentPane().add(panelH);
    this.getContentPane().add(panelJ);

    easyConstraints(gbl, panelA, 1, 3, 0, 0, 1.0, 1.0);
    easyConstraints(gbl, panelB, 1, 2, 1, 0, 3.0, 2.0);
    easyConstraints(gbl, panelC, 1, 1, 2, 0, 1.0, 1.0);
    easyConstraints(gbl, panelD, 1, 2, 3, 0, 1.0, 2.0);
    easyConstraints(gbl, panelE, 1, 2, 2, 1, 1.0, 1.0);
    easyConstraints(gbl, panelF, 1, 1, 1, 2, 3.0, 2.0);
    easyConstraints(gbl, panelG, 1, 1, 3, 2, 1.0, 2.0);
    easyConstraints(gbl, panelH, 2, 1, 0, 3, 1.0, 1.0);
    easyConstraints(gbl, panelJ, 2, 1, 2, 3, 1.0, 1.0);

}

private void easyConstraints(GridBagLayout GLB, JComponent comp, int w,
        int h, int x, int y, double wx, double wy)
{
    GridBagConstraints constraints= new GridBagConstraints();
    constraints.fill=GridBagConstraints.BOTH;
    constraints.gridwidth = w;
    constraints.gridheight = h;
    constraints.gridx = x;
    constraints.gridy = y;
    constraints.weightx = wx;
    constraints.weighty = wy;
    GLB.setConstraints(comp, constraints);
}


public static void main (String[] args)
{
    GridBagFrameBad gridBagFrameBad = new GridBagFrameBad();
    gridBagFrameBad.setVisible(true);
}
}

1 个答案:

答案 0 :(得分:2)

我想,你错过了一件重要的事情,即体重是从[0,1]间隔开始,规范你的体重,也许有帮助,让我检查...

另一方面,我看到你的网格宽度和网格高度为0,可能你也不想要这个......


好吧,看来,我们必须从头开始......

您的代码为我生成了这个:

enter image description here

我的Java版本是jdk1.7.0_67 ...

我将其用作BorderPanel

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JPanel;


public class BorderPanel extends JPanel {

    public BorderPanel(String title) {
        setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), title));
    }

}