GridBagLayout布局

1.先在一個固定的范圍內(nèi)做好規(guī)劃,先別考慮拉伸;
把能固定的先固定了;

2.在水平方向和垂直方向上找一個最適合用來拉伸的組件,將其設(shè)置為可拉伸;

3.然后可以調(diào)整原先設(shè)定好的參數(shù),設(shè)定容器的bounds

補充:
0.組件的GridWidth GridHeight針對的是自身的寬高
1.JPanel 沒有默認(rèn)的寬高,所以一般用來占位,填充空隙,拉伸;
2.JPanel 必須設(shè)置為某一方向上的拉伸,要不然就無用了
3.JPanel 因為沒有寬高,所以JPanel之間布局時,只有設(shè)置其約束的iPadX iPadY,固定其寬高
4.JPanel因為用來拉伸,所以其約束GridWidth 和 GridHeight 有一個規(guī)范的作用,比如
JPanel (2,1),另外同一列的組件為(1,1),那么這個組件是不會超過JPanel的
5.組件布局直接在一個容器中進行,不要再劃分為JPanel布局,因為JPanel布局難
確定寬高,拉伸復(fù)雜
6.一個可拉伸的組件被水平拉伸時,垂直方向上與之有交集的組件也會被拉伸
7.一個可拉伸的組件被垂直拉伸時,水平方向上與之有交集的組件也會被拉伸

Test

package Test;
import javax.swing.*;



import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame{

    public Test (){
        super();
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
        this.setLayout(new GridBagLayout());
        this.setBounds(200,200,300,300);
        this.initComponent();
//      this.pack();
    }
    
    private void initComponent(){
     
        //上側(cè)的工具選擇面板  
        JPanel toolSelectPanel = new JPanel();  
        toolSelectPanel.setBackground(Color.green);  
        this.add(toolSelectPanel, new GBC(0,0,2,1).setIpad(200,50).  
                     setFill(GBC.BOTH).setWeight(100, 0));  
        
        //左側(cè)的具體工具面板  
        JPanel toolConcretePanel = new JPanel();  
        toolConcretePanel.setBackground(Color.YELLOW);  
        this.add(toolConcretePanel,new GBC(0,1,1,1).setIpad(50,100).  
                     setFill(GBC.BOTH).setWeight(0, 100));  
        
        //右側(cè)的繪圖面板  
        JPanel drawPanel = new JPanel();  
        drawPanel.setBackground(Color.pink);  
        this.add(drawPanel,new GBC(1,1,1,1).setFill(GBC.BOTH).setWeight(100, 100));  
        
        /*這里面的GridWidth和GridHeight是起到的一個規(guī)范的作用,比如:
         右側(cè)的繪圖面板(1,1),上側(cè)的工具選擇面板(2,1),那么(1,1)是不會超過(2,1)的*/
        
        //下側(cè)的顏色選擇面板  
        JPanel colorPanel = new JPanel();  
        colorPanel.setBackground(Color.LIGHT_GRAY);  
        this.add(colorPanel,new GBC(0,2,2,1).setIpad(200,20).  
                     setFill(GBC.BOTH).setWeight(100, 0)); 
        
        //下側(cè)的狀態(tài)面板  
        JPanel statePanel = new JPanel();  
        statePanel.setBackground(Color.CYAN);  
        this.add(statePanel,new GBC(0,3,2,1).setIpad(200,50).  
                      setFill(GBC.BOTH).setWeight(100, 0));  
        
    }
    
    public static void main(String[] args) {
        new Test ().setVisible(true);;
    }
}

Paste_Image.png

Test2

package Test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

public class Test2 extends JFrame{
    public Test2 (){
        super();
        this.setLayout(new GridBagLayout());
        this.initComponents();
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
        this.setBounds(200,200,500,400);
//      this.pack();
    }   
    
    private void initComponents (){
        //第一行
        this.add(new JButton("打開"),new GBC(0,0,1,1).setFill(GBC.BOTH).setWeight(0, 0));
        this.add(new JButton("保存"),new GBC(1,0,1,1).setFill(GBC.BOTH).setWeight(0, 0));
        this.add(new JButton("另存為"),new GBC(2,0,1,1).setFill(GBC.BOTH).setWeight(0, 0));
        this.add(new JPanel(),new GBC(3,0,1,1).setFill(GBC.BOTH).setWeight(100, 0));
        
        //第二行
        JLabel label = new JLabel("JLabel");
        label.setOpaque(true);//設(shè)置為不透明,才能設(shè)置背景顏色
        label.setBackground(Color.cyan);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setVerticalAlignment(SwingConstants.CENTER);
        
        this.add(label,new GBC(0,1,2,1).setFill(GBC.BOTH).setWeight(0, 0));
        this.add(new JTextField("JTextField"),new GBC(2,1,5,1).setFill(GBC.BOTH).setWeight(100, 0));
        this.add(new JButton("清空"),new GBC(7,1,1,1).setFill(GBC.BOTH).setWeight(0, 0));
        
        //第三行
        JTextArea textArea = new JTextArea("");
        textArea.setLineWrap(true);
        textArea.setForeground(Color.BLACK);
        textArea.setMargin(new Insets(7,7,7,7));
        textArea.setBackground(Color.PINK);
        String arr[] = {"java","android","ios","php","python","c++"};
        this.add(new JList(arr),new GBC(0,2,2,1).setFill(GBC.BOTH).setWeight(0, 100));
        this.add(textArea,new GBC(2,2,6,1).setFill(GBC.BOTH).setWeight(100, 100));
    }
    
    public static void main(String[] args) {
        new Test2().setVisible(true);
    }

}

Paste_Image.png

Test3

package Calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator extends JFrame{
    private JLabel _label;
    private JPanel _containerP;
    private StringBuffer _strBuffer;
    
    public Calculator (){
        super();
        this.setBounds(200,200,300, 350);
        _containerP = new JPanel ();
        _containerP.setLayout(new GridBagLayout());
        this.add(_containerP);
        this.initComponents();
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
    
    private void initComponents(){
        //第0行
        _label = new JLabel("結(jié)果");
        _label.setHorizontalAlignment(SwingConstants.CENTER);
        _containerP.add(_label, new GBC(0,0,4,1).setWeight(100, 100).setFill(GBC.BOTH));
        
        //第一行
        _containerP.add(new JButton("AC"),new GBC(0,1,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("+/-"),new GBC(1,1,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("%"),new GBC(2,1,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("÷"),new GBC(3,1,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        
        //第二行
        _containerP.add(new JButton("7"),new GBC(0,2,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("8"),new GBC(1,2,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("9"),new GBC(2,2,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("x"),new GBC(3,2,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        
        //第三行
        _containerP.add(new JButton("4"),new GBC(0,3,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("5"),new GBC(1,3,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("6"),new GBC(2,3,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("-"),new GBC(3,3,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        
        //第四行
        _containerP.add(new JButton("1"),new GBC(0,4,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("2"),new GBC(1,4,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("3"),new GBC(2,4,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("+"),new GBC(3,4,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        
        //第五行
        _containerP.add(new JButton("0"),new GBC(0,5,2,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("."),new GBC(2,5,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        _containerP.add(new JButton("="),new GBC(3,5,1,1).setWeight(100, 100).setFill(GBC.BOTH));
        
        
        _strBuffer = new StringBuffer();
        for (int i = 0;i<_containerP.getComponents().length;i++){
            
            Object obj = _containerP.getComponent(i);
            if (obj instanceof JButton){
                ((JButton) obj).addActionListener(new ActionListener(){

                    public void actionPerformed(ActionEvent e) {
                        JButton btn = (JButton) e.getSource();
                        switch (btn.getText()){
                        case "=":
                            _strBuffer.setLength(0);
                            _label.setText("哈哈,功能沒實現(xiàn)!");
                            break;
                        default:
                            _strBuffer.append(((JButton) obj).getText());
                            _label.setText(_strBuffer.toString());
                            break;
                        }
                        
                    }
                    
                });
            }
        }
    }
            
    public static void main(String[] args) {
        new Calculator().setVisible(true);
    }
}
Paste_Image.png

GBC

package 文件選擇器;

import java.awt.GridBagConstraints;
import java.awt.Insets;

public class GBC extends GridBagConstraints{
      
       //初始化左上角位置  
   public GBC(int gridx, int gridy)  
   {  
      this.gridx = gridx;  
      this.gridy = gridy;  
   }  
  
   //初始化左上角位置和所占行數(shù)和列數(shù)  
   public GBC(int gridx, int gridy, int gridwidth, int gridheight)  
   {  
      this.gridx = gridx;  
      this.gridy = gridy;  
      this.gridwidth = gridwidth;  
      this.gridheight = gridheight;  
   }  
  
   //對齊方式  
   public GBC setAnchor(int anchor)  
   {  
      this.anchor = anchor;  
      return this;  
   }  
  
   //是否拉伸及拉伸方向  
   public GBC setFill(int fill)  
   {  
      this.fill = fill;  
      return this;  
   }  
  
   //x和y方向上的增量  
   public GBC setWeight(double weightx, double weighty)  
   {  
      this.weightx = weightx;  
      this.weighty = weighty;  
      return this;  
   }  
  
   //外部填充  
   public GBC setInsets(int distance)  
   {  
      this.insets = new Insets(distance, distance, distance, distance);  
      return this;  
   }  
  
   //外填充  
   public GBC setInsets(int top, int left, int bottom, int right)  
   {  
      this.insets = new Insets(top, left, bottom, right);  
      return this;  
   }  
  
   //內(nèi)填充  
   public GBC setIpad(int ipadx, int ipady)  
   {  
      this.ipadx = ipadx;  
      this.ipady = ipady;  
      return this;  
   }  
      
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容