天天看點

Java界面設計之頁面管理器

  java中layout mananager這次界面中主要用到的有borderlayout、flowlayout、gridlayout、gridlaybagout

  1、borderlayout是jframe中的預設布局方式,如果你沒有在容器類明确的定義布局方式,它将是預設的布局方式,當你想在容器中添加元件的時候,預設添加到中央的位置,是以第二個元件會遮住第一個元件,下面是borderlayout 一個小小的例子;

import javax.swing.*;

import java.awt.*;

public class borderlayout1 {       

  public borderlayout1(){

       jframe frame=new jframe();

        frame.add(borderlayout.north,new jbutton("north"));

        frame.add(borderlayout.south,new jbutton("south"));

        frame.add(borderlayout.west,new jbutton("west"));

        frame.add(borderlayout.east,new jbutton("east"));

        frame.add(borderlayout.center,new jbutton("center"));

        frame.setvisible(true);

        frame.setsize(400,200);

    } 

  public static void main(string[] args) {

         new borderlayout1();

  }

}

  總結:在整體的界面當中沒有很規範能夠使用這種布局方式,需要和其他的布局方式進行搭配才能夠達到自己想要的界面布局效果。

  2、flowlayout 設定流布局以後你所要添加的元件就會按照順序排列在容器裡面,能保證沒有元件會被阻擋起來,當時當你拉動界面的時候會很不滿意,組将也同樣會想水一樣流動起來,如果有使用流布局的容器能夠固定大小是最好不過的了,例子如下:

public class flowlayout1{

  public flowlayout1() {

       frame.setlayout(new flowlayout());

       for(int i = 1; i <=5; i++)

       frame.add(new jbutton("button " + i));

       frame.setvisible(true);

       frame.setsize(500,100);

       new flowlayout1();

  3、gridlayout 表格布局能将你的組将整齊的擺放在容器當中,當元件的數量超出表格的數量的時候,表格會自動添加來滿足元件的數量要求,同borderlayout 相同,完整的界面一般不會是整齊的表格樣式,是以這種布局方式和其他的搭配起來才能夠真正的達到你想要的界面效果,下面是個小例子:

public class gridlayout1 {

  public gridlayout1() {

     jframe frame=new jframe();

    frame.setlayout(new gridlayout(3,2));   //3行2列的表格布局

      for(int i = 0; i < 7; i++)

      frame.add(new jbutton("button " + i));  

      frame.setvisible(true);

      frame.setsize(500,300);

       new gridlayout1();

  4、gridbaglayout 這個布局方式是最複雜的,它動态的給每一個元件精确的進行位置的限制,為此還專門有個限制的對象gridbagconstraints,總共有11個參數能夠對元件進行限制; gridbagconstraints(int gridx,int gridy,int gridwidth,int gridheight,double weightx,double weighty,int anchor,int fill, insets,int ipadx,int ipady);

  gridx和gridy是元件在網格中的位置,這位置的計算方法非常有趣,類似與直角坐标系裡面的位置分布;

  gridwidth和gridheight 這倆個參數是設定元件在表格當中的大小的,設定的時候要小心一點,自己要有一個界面的草圖在旁邊參考;

  weightx和weighty參數可以設定當你的視窗被拉大(或拉小)的時候,元件所按照什麼比例進行縮放,數字越大,元件變化的會越大;

  anchor 參數是有兩個元件,相對小的的元件應該放在哪裡;

  fill 參數是當元件所處的動态表格裡面有空餘的位置的時候,元件将按什麼方向填充,這個參數在界面中比較關鍵一點;

  insets 參數是一個小的對象,其中也有4個不同的參數,分别是上,左,右,下來設定元件之間的間距;

  ipadx和ipady是元件邊框離元件中心的距離,對有些元件來說這個參數是沒有必要的;

  感覺有很多的參數來設定一個元件,還有專門的限制對象,其實每次并不是哪一個參數都要重新設定的,下面是一個例子

public class gridbaglayout1 {    

public gridbaglayout1(){

     jframe frame =new jframe(); 

     gridbaglayout grid=new gridbaglayout(); 

     gridbagconstraints c1=new gridbagconstraints(); 

     frame.setlayout(grid);

     //為button1進行限制

     c1.gridwidth=2;     c1.gridheight=1;

     c1.insets=new insets(5,5,0,0);        //和上面的元件間距為5,右邊為間距5

     jbutton button1=new jbutton("button1");

     grid.setconstraints(button1,c1);

     frame.add(button1);

     //為button2進行限制

     c1.fill=gridbagconstraints.horizontal;  

     jbutton button2=new jbutton("button2");

     grid.setconstraints(button2,c1); 

     frame.add(button2);    

     //為button3進行限制

     c1.gridx=0;          c1.gridy=1;          //動态表格(0,1)位置

     c1.gridwidth=4;         //元件長占4個單元格,高占一個單元格            

     jbutton button3=new jbutton("button3"); 

     grid.setconstraints(button3,c1);

     frame.add(button3);  

     frame.setvisible(true);

     frame.setsize(200,150);

    public static void main(string[] args) {

          new gridbaglayout1();

    }

 下面是學校實驗裡面的一個聊天界面的實作,裡面綜合了上面講到的幾種布局方式:

public class chatdisplay extends jpanel{

    private jpanel interfacepanel;

    private jpanel userpanel;

    private jlabel userlabel;

    private jcombobox usercombobox;

    private jlabel messagelabel;

    private jbutton sendbutton;

    private jtextfield messagetext;      

    private jtabbedpane texttabbedpane;

    private jscrollpane publicscrollpane;

    private jtextpane publictextpane;

    private jscrollpane privatescrollpane;

    private jtextpane privatetextpane;

    public chatdisplay(){      

      interfacepanel=new jpanel();

      interfacepanel.setlayout(new borderlayout(10,10));    

    //兩個菜單項

          //執行個體化菜單與菜單項

        jmenu[] menus={ new jmenu("file"),new jmenu("action")};

        jmenuitem[] items={new jmenuitem("save"),new jmenuitem("exit")};

        menus[0].add(items[0]);

      menus[0].add(items[1]);

          //執行個體化菜單棒,添加菜單項至菜單棒

        jmenubar mb = new jmenubar();

      mb.add(menus[0]);

      mb.add(menus[1]);

        //設定菜單條的位置在界面的最上方

      interfacepanel.add(mb,borderlayout.north);    

    //界面中央的資訊面闆    

         //執行個體化共有和私有的文本域 、 滾動面闆、設定不可讀        

        publictextpane=new jtextpane();

        publicscrollpane=new jscrollpane(publictextpane);    

        publictextpane.seteditable(false);

        privatetextpane=new jtextpane();

        privatescrollpane=new jscrollpane(privatetextpane);        

        privatetextpane.seteditable(false);

          //執行個體化動态頁籤

        texttabbedpane=new jtabbedpane();

        texttabbedpane.addtab("public",publicscrollpane);

        texttabbedpane.addtab("private",privatescrollpane);

        texttabbedpane.settabplacement(jtabbedpane.bottom);

        interfacepanel.add(texttabbedpane,borderlayout.center);

   //界面底部的使用者面闆

             //執行個體化并初始化化各元件

        userpanel =new jpanel();                  

        userlabel=new jlabel("    send to :");

        usercombobox=new jcombobox();

        string users[]={"public","clientb","cienta"};

        usercombobox.additem(users[0]); 

        usercombobox.additem(users[1]); 

        usercombobox.additem(users[2]);

        messagelabel=new jlabel("message:");

        messagetext=new jtextfield(22);

        sendbutton=new jbutton("send");    

    //為下面的uesepanel面闆進行布局        

        //userpanel 設定為兩行一列的網格布局,兩行分别放兩個面闆,userpanel2.與userpanel

        userpanel.setlayout(new gridlayout(2,1));

        jpanel userpanel2 =new jpanel();        

        jpanel userpanel3 =new jpanel();

        userpanel.add(userpanel2 );

        userpanel.add(userpanel3);

        //第一行的面闆 userpanel2 采用網格精準定位布局,并添加一個标簽與組合框 

        userpanel2.add(userlabel);

        userpanel2.add(usercombobox);

        gridbaglayout gridbag=new gridbaglayout();        

        userpanel2.setlayout(gridbag);

        //對第一行第二個元件組合框進行布局限制,執行個體化一個對象c

        gridbagconstraints c=new gridbagconstraints();

        //當組合框被拉伸後所按的的比例   

        c.weightx=1;

        c.weighty=1;

        //當元件框所占的機關行數還有剩餘的時候,元件的填充方式為水準

        c.fill=gridbagconstraints.horizontal;

        //元件與元件之前的距離,參數依次為上 左 下 右 

        c.insets=new insets(0,10,0,5);

        //将布局限制添加在組合框上

        gridbag.setconstraints(usercombobox,c);

        //第二行的面闆 userpanel3采用流布局,添加一個标簽,一個輸入文本的框,一個發送按鈕                              

        userpanel3.setlayout(new flowlayout());

        userpanel3.add(messagelabel);

        userpanel3.add(messagetext);

        userpanel3.add(sendbutton);           

        //放置在頁面下方,并添加面闆到使用者面闆中去

        interfacepanel.add(borderlayout.south,userpanel);   

        jframe frame=new jframe();     

        frame.add(interfacepanel);

        frame.setsize(410,400);

          new chatdisplay();

 };

  界面效果如下:

Java界面設計之頁面管理器

  應該總結一下,簡單的界面實作,首先需要自己畫一個草圖,将自己需要的元件都放進去,然後開始敲鍵盤,複雜一點的界面,借助一點工具是必然的,這樣可以省去很大的一部分時間,專注在功能的實作上面。

最新内容請見作者的github頁:http://qaseven.github.io/