Java GridBagLayout 簡單使用

來源:互聯網
上載者:User

標籤:uvc   cot   nload   apt   ucf   cut   eve   lbp   hex   

    這裡只介紹了很基礎布局構建及使用,主要是關於 GridBagLayout.

    首先整套流程大概是,

    聲明一個 GridBagLayout 對象

    private GridBagLayout gridBagLayoutFrame = new GridBagLayout();

 

    然後把當前類的容器布局管理器設定為 GridBagLayout

    this.setLayout(gridBagLayoutFrame);

 

    最後聲明一個 JPanel 用於添加組件。(當然也可以是別的Panel。如JTabbedPane等)

    private JPanel checkBoxTreePanel = new JPanel();

    private JTabbedPane tabbedPane = new JTabbedPane();

 

    然後就可以開始使用這個布局管理器來增加和設定組件了。

  add(Component comp, Object constraints)

    下面是一個例子:

   

 1 import java.awt.*; 2 import javax.swing.JFrame; 3 import javax.swing.JTabbedPane; 4 import javax.swing.JPanel; 5  6  7 public class WriteForBlog extends JFrame 8 { 9     private GridBagLayout gridBagLayoutFrame = new GridBagLayout();10     private JTabbedPane tabbedPane = new JTabbedPane();11     private JPanel panelOne = new JPanel();12     private JPanel panelTwo = new JPanel();13     14     public WriteForBlog()15     {16         jbInit();17     }18     19     private void jbInit()20     {21         this.setLayout(gridBagLayoutFrame);22         this.setBounds(200, 200, 1000, 600);23         tabbedPane.add(panelOne, "One");24         tabbedPane.add(panelTwo, "Two");25         26         this.add(tabbedPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.027                  ,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));28     }29     30     public static void main(String[] args)31     {32         WriteForBlog test = new WriteForBlog();33         test.setVisible(true);34     }35 36 }

    :

   

   

Java GridBagLayout 簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.