我想了解Swing的基本概念,目前我正在看看GridBagLayout。反正我想生產只使用GridBagLayout的畫面中的佈局:http://i.imgur.com/Sa59qNz.jpgGridBagLayout的一些問題
我設法生產出它嵌入與主框架也具有GridBagLayout的內部網格包佈局的面板gridBagHelper。
這裏是產生所需的佈局兩類:
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的中間開始。我想知道是否有人知道如何使它在一個Frame中工作,只需一個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);
}
}
網格包是坦率地說老。我們現在有[更好的工具](http://www.developer.com/lang/article.php/3589961/Building-Java-GUIs-with-Matisse-A-Gentle-Introduction.htm)。請查看[GUI構建器](https://www.google.com/search?q=java+gui+builders),例如[Matisse,](https://netbeans.org/kb/docs/java/quickstart- gui.html)我可以在2分鐘內完成你的佈局。 – markspace 2014-09-19 21:12:54
BorderPanel類缺少編譯正確... – Betlista 2014-09-19 21:32:08
@Betlista感謝您的回覆。上面的兩個類生成所需的佈局(因爲它在給定的圖像中顯示)並且編譯沒有錯誤。當我粘貼代碼時,我會檢查是否遺漏了一些東西。 – Nikos 2014-09-19 21:39:16