2013-10-19 40 views
0

我曾與絕對定位(的setBounds和空佈局) 現在開始練佈局管理器, 這個代碼與網格包佈局,但不被顯示很少的部件,無論是有一些問題,與細胞或別的東西 請幫忙!GridBagLayout中不能正常工作

 
import java.util.StringTokenizer; 
import java.awt.event.*; 
import java.awt.*; 
import javax.swing.*; 
class Calculator extends JFrame 
{ 
    JButton add,sub,mul,div,sin,cos,tan,clear,negate,inverse,zero,one,two,three,four,five,six,seven,eight,nine,equalTo,percentage,sqrt; 
    JTextField input; 
    GridBagLayout gbl; 
    private void addComponent(Component component,int gridx, int gridy, int gridwidth, int gridheight,Insets insets) 
    { 
     add(component, new GridBagConstraints(gridx, gridy,gridwidth, gridheight, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0,0)); 
    } 
    Calculator() 
    { 
     //setSize(500,400); 
     setVisible(true); 
     setLayout(gbl=new GridBagLayout()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     add= new JButton("+"); 
     sub= new JButton("-"); 
     mul= new JButton("*"); 
     div= new JButton("/"); 
     sin= new JButton("sin"); 
     cos= new JButton("cos"); 
     tan= new JButton("tan"); 
     clear= new JButton("C"); 
     negate= new JButton("neg"); 
     inverse= new JButton("1/x"); 
     zero= new JButton("0"); 
     one= new JButton("1"); 
     two= new JButton("2"); 
     three= new JButton("3"); 
     four= new JButton("4"); 
     five= new JButton("5"); 
     six= new JButton("6"); 
     seven= new JButton("7"); 
     eight= new JButton("8"); 
     nine= new JButton("9"); 
     equalTo= new JButton("="); 
     percentage= new JButton("%"); 
     sqrt= new JButton("sqrt"); 
     input = new JTextField(20); 

     addComponent(input,0,0,0,1,new Insets(10,10,100,4)); //tldr 

     addComponent(add,0,1,1,1,new Insets(4,4,4,4)); 
     addComponent(sub,1,1,2,1,new Insets(4,4,4,4)); 
     addComponent(mul,2,1,3,1,new Insets(4,4,4,4)); // this is not displayed 
     addComponent(div,3,1,4,1,new Insets(4,4,4,4)); 

     addComponent(sin,0,2,1,2,new Insets(4,4,4,4)); 
     addComponent(cos,1,2,2,2,new Insets(4,4,4,4)); 
     addComponent(tan,2,2,3,2,new Insets(4,4,4,4)); // this is not displayed 
     addComponent(clear,3,2,4,2,new Insets(4,4,4,4)); 

     addComponent(negate,0,3,1,3,new Insets(4,4,4,4)); // these 4 are not visible 
     addComponent(inverse,1,3,2,3,new Insets(4,4,4,4)); 
     addComponent(zero,2,3,3,3,new Insets(4,4,4,4)); 
     addComponent(one,3,3,4,3,new Insets(4,4,4,4)); 
     pack(); 
    } 
    public static void main(String...args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new Calculator(); 
      } 
     }); 
    } 
}

回答

4

首先,做好使用佈局的準備!

,在跳出我的事情馬上是值傳遞給gridwidthgridheight

addComponent(add,0,1,1,1,new Insets(4,4,4,4)); 
addComponent(sub,1,1,2,1,new Insets(4,4,4,4)); 
addComponent(mul,2,1,3,1,new Insets(4,4,4,4)); // this is not displayed 
addComponent(div,3,1,4,1,new Insets(4,4,4,4)); 

基本上,這是說,加add電網X/Y位置爲0x1,與1x1gridwidth/gridheight , 目前很好。

然後在X/Y軸1x11x2,還好一個gridwidth/gridheight添加sub ...

然後用1x3一個gridwidth/gridheight添加mul在X/Y位置2x1,現在我們開始運行進入問題......基本上,sub實際上正在擴展到我們的小區,覆蓋了我們的一部分!

gridwidthgridheight允許您定義組件將多少個單元擴展到,大部分的時間,你想這是1x1

一旦我糾正所有gridwidthgridheight1x1,我能得到這個

enter image description here

import java.awt.Component; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

class Calculator extends JFrame { 

    JButton add, sub, mul, div, sin, cos, tan, clear, negate, inverse, zero, one, two, three, four, five, six, seven, eight, nine, equalTo, percentage, sqrt; 
    JTextField input; 
    GridBagLayout gbl; 

    private void addComponent(Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets) { 
     add(component, new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0)); 
    } 

    Calculator() { 
     //setSize(500,400); 
     setLayout(gbl = new GridBagLayout()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     add = new JButton("+"); 
     sub = new JButton("-"); 
     mul = new JButton("*"); 
     div = new JButton("/"); 
     sin = new JButton("sin"); 
     cos = new JButton("cos"); 
     tan = new JButton("tan"); 
     clear = new JButton("C"); 
     negate = new JButton("neg"); 
     inverse = new JButton("1/x"); 
     zero = new JButton("0"); 
     one = new JButton("1"); 
     two = new JButton("2"); 
     three = new JButton("3"); 
     four = new JButton("4"); 
     five = new JButton("5"); 
     six = new JButton("6"); 
     seven = new JButton("7"); 
     eight = new JButton("8"); 
     nine = new JButton("9"); 
     equalTo = new JButton("="); 
     percentage = new JButton("%"); 
     sqrt = new JButton("sqrt"); 
     input = new JTextField(20); 

     addComponent(input, 0, 0, 0, 1, new Insets(10, 10, 100, 4)); //tldr 

     addComponent(add, 0, 1, 1, 1, new Insets(4, 4, 4, 4)); 
     addComponent(sub, 1, 1, 1, 1, new Insets(4, 4, 4, 4)); 
     addComponent(mul, 2, 1, 1, 1, new Insets(4, 4, 4, 4)); // this is not displayed 
     addComponent(div, 3, 1, 1, 1, new Insets(4, 4, 4, 4)); 

     addComponent(sin, 0, 2, 1, 1, new Insets(4, 4, 4, 4)); 
     addComponent(cos, 1, 2, 1, 1, new Insets(4, 4, 4, 4)); 
     addComponent(tan, 2, 2, 1, 1, new Insets(4, 4, 4, 4)); // this is not displayed 
     addComponent(clear, 3, 2, 1, 1, new Insets(4, 4, 4, 4)); 

     addComponent(negate, 0, 3, 1, 1, new Insets(4, 4, 4, 4)); // these 4 are not visible 
     addComponent(inverse, 1, 3, 1, 1, new Insets(4, 4, 4, 4)); 
     addComponent(zero, 2, 3, 1, 1, new Insets(4, 4, 4, 4)); 
     addComponent(one, 3, 3, 1, 1, new Insets(4, 4, 4, 4)); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String... args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new Calculator(); 
      } 

     }); 
    } 

} 
+0

我正在採取gridwidth,gridheight絕對的,而不是PO sitional(相對)這是錯誤的 – Gagan93