一、絕對佈局管理器java
硬性指定組件在容器中的位置和大小佈局
使用絕對佈局的步驟:spa
Container.setLayout(null);// 取消佈局管理器 Component.setBounds();//設置組件的大小位置
package Eleven; import javax.swing.JFrame; import java.awt.Container; import javax.swing.JButton; import javax.swing.WindowConstants; public class AbsoluteLayout extends JFrame { public AbsoluteLayout(){ setTitle("本窗體使用絕對佈局"); getContentPane().setLayout(null); setBounds(0,0,200,200); /*JFrame窗體類包含一個容器類,全部放置在窗體上的組件其實都是放置在這個容器類中的,經過 * getContentPane()方法獲取*/ Container container = getContentPane(); JButton b1 = new JButton("按鈕1"); JButton b2 = new JButton("按鈕2"); //void java.awt.Component.setBounds(int x, int y, int width, int height) b1.setBounds(60, 70, 100, 20); b2.setBounds(10, 30, 80, 30); container.add(b1); container.add(b2); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//退出程序,關閉按鈕 } public static void main(String[] args){ new AbsoluteLayout(); } }
二、流佈局管理器FlowLayoutcode
像流同樣按指定方向擺放組件,直到佔據了這一行的全部空間再往下移一行。get
package Eleven; import javax.swing.JFrame; import javax.swing.WindowConstants; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JButton; public class FlowLayoutPosition extends JFrame{ public FlowLayoutPosition(){ setTitle("FlowLayout"); Container container = getContentPane(); /*Flow layouts are typically used to arrange buttons in a panel. * It arranges buttons horizontally until no more buttons fit on * the same line. The line alignment is determined by the align property. The possible values are: LEFT RIGHT CENTER LEADING TRAILING */ /* * public FlowLayout(int alignment,int horizGap,int vertGap) * alignment:FlowLayout.LEFT(單行中左對齊)/FlaoLayout.CENTER/FlowLayout.RIGHT * horizGap/vertGap:以像素爲單位指定組件之間的水平和垂直間隔*/ //Container javax.swing.JFrame.getContentPane()獲取窗體的組件容器 getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT,10,10)); for(int i = 0;i < 10;i++){ //Component java.awt.Container.add(Component comp) container.add(new JButton("Button"+i)); } setSize(300,200);//設置窗體大小 setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//設置窗體關閉方式 } public static void main(String[] args){ new FlowLayoutPosition(); } }
三、網格佈局管理器GridLayoutit
網格佈局中每一個組件大小都相同,單元格數由行數列數決定io
public GridLayout(int rows,int colums); public GridLayout(int rows,int colums,int horizGap,int vertGap); //horizGap,vertGap組件之間的間距
package Eleven; import javax.swing.JFrame; import javax.swing.WindowConstants; import java.awt.GridLayout; import javax.swing.JButton; public class GridLayoutPosition extends JFrame{ public GridLayoutPosition(){ /*public GridLayout(int rows,int columns,int horizGap,int vertGap) * 行數、列數能夠有一個爲0,表示這一行、列能夠排列任意多組件*/ final GridLayout gridLayout = new GridLayout(0,3,2,2); getContentPane().setLayout(gridLayout); setTitle("GridLayout"); final JButton button1 = new JButton(); button1.setText("B1"); getContentPane().add(button1); final JButton button2 = new JButton(); button2.setText("B2"); getContentPane().add(button2); final JButton button3 = new JButton(); button3.setText("B3"); getContentPane().add(button3); final JButton button4 = new JButton(); button4.setText("B4"); getContentPane().add(button4); final JButton button5 = new JButton(); button5.setText("B5"); getContentPane().add(button5); final JButton button6 = new JButton(); button6.setText("B6"); getContentPane().add(button6); setSize(253,189); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String [] args){ new GridLayoutPosition(); } }