這是第一次寫博客,是有關(guān)JAVA頁面設(shè)計(jì)當(dāng)中的布局管理器,可能大多數(shù)人會(huì)選擇使用 NetBeans 或者是Eclipse 的組件來實(shí)現(xiàn)可視化拖拽組件來達(dá)到自己頁面設(shè)計(jì)布局,因?yàn)槭堑谝淮巫鼋缑妫偨Y(jié)一下,以供以后復(fù)習(xí)能用到。
JAVA中Layout Mananager這次界面中主要用到的有BorderLayout、FlowLayout、GridLayout、GridLayBagout
1、BorderLayout是JFrame中的默認(rèn)布局方式,如果你沒有在容器類明確的定義布局方式,它將是默認(rèn)的布局方式,當(dāng)你想在容器中添加組件的時(shí)候,默認(rèn)添加到中央的位置,所以第二個(gè)組件會(huì)遮住第一個(gè)組件,下面是BorderLayout 一個(gè)小小的例子;
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(); } } |
總結(jié):在整體的界面當(dāng)中沒有很規(guī)范能夠使用這種布局方式,需要和其他的布局方式進(jìn)行搭配才能夠達(dá)到自己想要的界面布局效果。
2、FlowLayout 設(shè)置流布局以后你所要添加的組件就會(huì)按照順序排列在容器里面,能保證沒有組件會(huì)被阻擋起來,當(dāng)時(shí)當(dāng)你拉動(dòng)界面的時(shí)候會(huì)很不滿意,組將也同樣會(huì)想水一樣流動(dòng)起來,如果有使用流布局的容器能夠固定大小是最好不過的了,例子如下:
import javax.swing.*; import java.awt.*; public class FlowLayout1{ public FlowLayout1() { JFrame frame=new JFrame(); frame.setLayout(new FlowLayout()); for(int i = 1; i <=5; i++) frame.add(new JButton("Button " + i)); frame.setVisible(true); frame.setSize(500,100); } public static void main(String[] args) { new FlowLayout1(); } } |
3、GridLayout 表格布局能將你的組將整齊的擺放在容器當(dāng)中,當(dāng)組件的數(shù)量超出表格的數(shù)量的時(shí)候,表格會(huì)自動(dòng)添加來滿足組件的數(shù)量要求,同BorderLayout 相同,完整的界面一般不會(huì)是整齊的表格樣式,所以這種布局方式和其他的搭配起來才能夠真正的達(dá)到你想要的界面效果,下面是個(gè)小例子:
import javax.swing.*; import java.awt.*; 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); } public static void main(String[] args) { new GridLayout1(); } } |
4、GridBagLayout 這個(gè)布局方式是最復(fù)雜的,它動(dòng)態(tài)的給每一個(gè)組件精確的進(jìn)行位置的約束,為此還專門有個(gè)約束的對(duì)象GridBagConstraints,總共有11個(gè)參數(shù)能夠?qū)M件進(jìn)行約束; GridBagConstraints(int gridx,int gridy,int gridwidth,int gridheight,double weightx,double weighty,int anchor,int fill, Insets,int ipadx,int ipady);
gridx和gridy是組件在網(wǎng)格中的位置,這位置的計(jì)算方法非常有趣,類似與直角坐標(biāo)系里面的位置分布;
gridwidth和gridheight 這倆個(gè)參數(shù)是設(shè)置組件在表格當(dāng)中的大小的,設(shè)置的時(shí)候要小心一點(diǎn),自己要有一個(gè)界面的草圖在旁邊參考;
weightx和weighty參數(shù)可以設(shè)置當(dāng)你的窗口被拉大(或拉小)的時(shí)候,組件所按照什么比例進(jìn)行縮放,數(shù)字越大,組件變化的會(huì)越大;
anchor 參數(shù)是有兩個(gè)組件,相對(duì)小的的組件應(yīng)該放在哪里;
fill 參數(shù)是當(dāng)組件所處的動(dòng)態(tài)表格里面有空余的位置的時(shí)候,組件將按什么方向填充,這個(gè)參數(shù)在界面中比較關(guān)鍵一點(diǎn);
Insets 參數(shù)是一個(gè)小的對(duì)象,其中也有4個(gè)不同的參數(shù),分別是上,左,右,下來設(shè)置組件之間的間距;
ipadx和ipady是組件邊框離組件中心的距離,對(duì)有些組件來說這個(gè)參數(shù)是沒有必要的;
感覺有很多的參數(shù)來設(shè)置一個(gè)組件,還有專門的約束對(duì)象,其實(shí)每次并不是哪一個(gè)參數(shù)都要重新設(shè)置的,下面是一個(gè)例子
import javax.swing.*; import java.awt.*; public class GridBagLayout1 { public GridBagLayout1(){ JFrame frame =new JFrame(); GridBagLayout grid=new GridBagLayout(); GridBagConstraints c1=new GridBagConstraints(); frame.setLayout(grid); //為button1進(jìn)行約束 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進(jìn)行約束 c1.fill=GridBagConstraints.HORIZONTAL; JButton button2=new JButton("button2"); grid.setConstraints(button2,c1); frame.add(button2); //為button3進(jìn)行約束 c1.gridx=0; c1.gridy=1; //動(dòng)態(tài)表格(0,1)位置 c1.gridwidth=4; //組件長占4個(gè)單元格,高占一個(gè)單元格 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(); } } |
下面是學(xué)校實(shí)驗(yàn)里面的一個(gè)聊天界面的實(shí)現(xiàn),里面綜合了上面講到的幾種布局方式:
import javax.swing.*; import java.awt.*; 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)); //兩個(gè)菜單項(xiàng) //實(shí)例化菜單與菜單項(xiàng) 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]); //實(shí)例化菜單棒,添加菜單項(xiàng)至菜單棒 JMenuBar mb = new JMenuBar(); mb.add(menus[0]); mb.add(menus[1]); //設(shè)置菜單條的位置在界面的最上方 interfacePanel.add(mb,BorderLayout.NORTH); //界面中央的信息面板 //實(shí)例化共有和私有的文本域 、 滾動(dòng)面板、設(shè)置不可讀 publicTextPane=new JTextPane(); publicScrollPane=new JScrollPane(publicTextPane); publicTextPane.setEditable(false); privateTextPane=new JTextPane(); privateScrollPane=new JScrollPane(privateTextPane); privateTextPane.setEditable(false); //實(shí)例化動(dòng)態(tài)選項(xiàng)卡 textTabbedPane=new JTabbedPane(); textTabbedPane.addTab("public",publicScrollPane); textTabbedPane.addTab("private",privateScrollPane); textTabbedPane.setTabPlacement(JTabbedPane.BOTTOM); interfacePanel.add(textTabbedPane,BorderLayout.CENTER); //界面底部的用戶面板 //實(shí)例化并初始化化各組件 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面板進(jìn)行布局 //userPanel 設(shè)置為兩行一列的網(wǎng)格布局,兩行分別放兩個(gè)面板,userPanel2.與userPanel userPanel.setLayout(new GridLayout(2,1)); JPanel userPanel2 =new JPanel(); JPanel userPanel3 =new JPanel(); userPanel.add(userPanel2 ); userPanel.add(userPanel3); //第一行的面板 userPanel2 采用網(wǎng)格精準(zhǔn)定位布局,并添加一個(gè)標(biāo)簽與組合框 userPanel2.add(userLabel); userPanel2.add(userComboBox); GridBagLayout gridbag=new GridBagLayout(); userPanel2.setLayout(gridbag); //對(duì)第一行第二個(gè)組件組合框進(jìn)行布局約束,實(shí)例化一個(gè)對(duì)象C GridBagConstraints c=new GridBagConstraints(); //當(dāng)組合框被拉伸后所按的的比例 c.weightx=1; c.weighty=1; //當(dāng)組件框所占的單位行數(shù)還有剩余的時(shí)候,組件的填充方式為水平 c.fill=GridBagConstraints.HORIZONTAL; //組件與組件之前的距離,參數(shù)依次為上 左 下 右 c.insets=new Insets(0,10,0,5); //將布局約束添加在組合框上 gridbag.setConstraints(userComboBox,c); //第二行的面板 userPanel3采用流布局,添加一個(gè)標(biāo)簽,一個(gè)輸入文本的框,一個(gè)發(fā)送按鈕 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.setVisible(true); frame.setSize(410,400); } public static void main(String[] args) { new ChatDisplay(); } }; |
界面效果如下:

應(yīng)該總結(jié)一下,簡單的界面實(shí)現(xiàn),首先需要自己畫一個(gè)草圖,將自己需要的組件都放進(jìn)去,然后開始敲鍵盤,復(fù)雜一點(diǎn)的界面,借助一點(diǎn)工具是必然的,這樣可以省去很大的一部分時(shí)間,專注在功能的實(shí)現(xiàn)上面。