swing的高階佈局(二)
1,箱式佈局BoxLayout
箱式佈局是管理一組水平或者垂直的元件的佈局,就是說每一組水平元件或者垂直元件,都要進行管理。
這裡有兩種型別的不可見元件,分別為strut和glue
strut有 createHorizontalStrut(int width)
createVerticalStrut(int height)
createRigidArea(Dimension d)
glue createHorizontalGlue(int width)
createVerticalGlue(int height)
createGlue(Dimension d)
strut的作用就相當於把一組元件的x座標確定,然後用setAlignmentX()把該組元件的對齊方式確定,值為0,則採用上對齊,值為0.5,則採用居中對齊,值為1,則採用右對齊。故而確定整組元件的位置。
而Glue則類似一個彈簧,把元件平均分配到容器中。即確定間距。
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class BoxLayout extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
BoxLayout boxLayout=new BoxLayout();
boxLayout.setVisible(true);
boxLayout.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boxLayout.setSize(200,200);
}
public BoxLayout()
{
Box topicBox=Box.createHorizontalBox();
getContentPane().add(topicBox,BorderLayout.NORTH);
topicBox.add(Box.createHorizontalStrut(5));
JLabel topicLabel=new JLabel("主題");
topicBox.add(topicLabel);
topicBox.add(Box.createHorizontalStrut(5));
JTextField topicField=new JTextField(30);
topicBox.add(topicField);
Box box=Box.createVerticalBox();
getContentPane().add(box,BorderLayout.CENTER);
box.add(Box.createVerticalStrut(5));
Box contentBox=Box.createHorizontalBox();
contentBox.setAlignmentX(1); //設定元件的水平調整值,靠右對齊
box.add(contentBox);
contentBox.add(Box.createHorizontalStrut(5));
JLabel contentLabel=new JLabel("內容");
contentLabel.setAlignmentY(0); //設定垂直調整值,靠上放對齊
contentBox.add(contentLabel);
contentBox.add(Box.createHorizontalStrut(5));
JScrollPane scrollPane=new JScrollPane();
scrollPane.setAlignmentY(0); //設定垂直調整度,考上方對齊
contentBox.add(scrollPane);
JTextArea contentArea=new JTextArea();
contentArea.setLineWrap(true);
scrollPane.setViewportView(contentArea);
box.add(Box.createVerticalStrut(5));
JButton submitButton=new JButton("確定");
submitButton.setAlignmentX(1); //設定對平調整值,靠右側對齊
box.add(submitButton);
}
}
2.CardLayout
卡片佈局中用來顯示卡片的方法 first,last,next,previous,show(顯示指定標籤的卡片)
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutExample extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel cardPane=null;
private CardLayout cardLayout=null;
public static void main(String[] args)
{
CardLayoutExample card=new CardLayoutExample();
card.setVisible(true);
}
public CardLayoutExample()
{
super("使用卡片佈局管理器");
setBounds(100,100,600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cardLayout=new CardLayout();
cardPane=new JPanel(cardLayout); //建立一個卡片佈局的面板物件
getContentPane().add(cardPane,BorderLayout.CENTER);
JPanel images = new JPanel() {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//新增資源中的圖
g.drawImage(
Toolkit.getDefaultToolkit().getImage(
"G:\\sourse\\flawer.jpg"), 50, 50, this);
}
};
cardPane.add("flawer", images);
JPanel timg = new JPanel() {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(
Toolkit.getDefaultToolkit().getImage(
"G:\\sourse\\timg.jpg"),50,50, this);
}
};
cardPane.add("timg",timg);
final JPanel ButtonPanel=new JPanel();
getContentPane().add(ButtonPanel,BorderLayout.SOUTH);
String[] buttonNames={"flawer","timg","next"}; //利用按鈕陣列將按鈕名字新增到按鈕中
for(int i=0;i<buttonNames.length;i++)
{
final JButton button=new JButton(buttonNames[i]);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if("flawer".equals(cmd))
cardLayout.show(cardPane,"flawer");
else if("next".equals(cmd))
cardLayout.next(cardPane);
else
cardLayout.show(cardPane,"timg");
}
}
);
ButtonPanel.add(button);
}
}
}
3.網格組佈局
網格組佈局實現了一個動態的矩形網格,這個矩形由無數個舉行單元格組成。
gridx和gridy分別表示的是元件起始點所在列的索引和元件起始點所在行的索引(據我個人理解 ,是確定相對位置,例如第一行有元件,讓girdx=1則可以從第二行開始佈局新的元件)
gridwidth和gridheight設定元件所佔的列和行
anchor用來設定元件在顯示區域的顯示位置,通常有九個方位,分別為north,northeast,east,southeast,south,southwest,west,northwest,center。
fill用來設定元件的填充方式,當單元格的顯示區域的面積大於元件的面積,或者一個元件佔用多個單元格時,可以用fill設定填充方式,可以利用4個靜態常量,預設情況下是設定為none,即不改變元件大小填充顯示區域,設定為Horizontal,即調整水平方向大小至填滿顯示區域,設定為Vertical,即只調整垂直方向的大小至填滿區域,設定為Both則調整長度和寬度至填滿區域。
insets用來設定元件與單元格邊緣之間的最小距離,該屬性的型別為insets,insets僅僅有一個構造方法insets(int top,int left,int bottom,int right) 單位為畫素
ipadx和ipady用來修改元件的首選大小,屬性ipadx用來修改寬度,屬性ipady用來修改元件的高度,若為正數,則增加指定的寬度和高度,若為負數,則減小指定的寬度和高度。
weigthx和weigthy 用來設定每一行和每一列對額外空間的分佈空間的分佈方式。
weigthx=30 //第一列的分佈方式為30%
4.彈簧佈局管理器 springLayout
利用該窗體佈局器時,當改變窗體的大小時,能夠在不改變元件間相對位置的情況下自動調整元件的大小,使元件依舊佈滿整個窗體。
通過方法putConstraint(String e1,Component c1,int pad,String e2,Contonent c2)
c1 是需要參考的元件物件,e1 是參考的元件物件的具體需要參考的邊 , c1 是被參考的物件,e2 是被參考的元件物件具體被參考的邊,pad 是兩條邊之間的距離
e1和e2可以從east,west,north,south中選擇
例如 springLayout.putConstraint(StringLayout.WEST,元件A,60,SpringLayout.EAST,容器物件)
元件A的北側的邊參考容器的北側邊之間的距離為60畫素 ,其中e1在e2的北方或者西方時,pad為正數,在東方和南方時,pad為負數。
使用彈簧和支柱,利用 constant(int min,int pref,int max)
min是彈簧被壓縮到極限是的長度,max是彈簧拉到極限時的長度,perf是首選值,即是自然長度的長度。min和max為0時,即constant(int pref)用來建立支柱
Spring widthSP=Spring.constant(60,300,600);
Spring heightST=Spring.constant(60);
Constrains IButtonCons=springLayout.getConstrains(Button);
IButtonCons.setWidth(widthSP);
IButtonCons.setHeight(heightST);
Constrains rButtonCons=springLayout.getConatrains(rButton);
rButtonCons.setWidth(widthSp);
rButtonCons.setHeight(heightST);