/**
* baidu :组件不会直接放到框架上,而是放在若干个面板上,这些面板再放到窗格上?
* 实际上在JFrame上可直接添加Jbutton
*
* BorderLayout
FlowLayout
GridLayout
GridBagLayout
CardLayout
BoxLayout
*
* GridBagLayout稍复杂,主要看BoxLayout、BorderLayout
*/
package layoutmanager;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Albert Li
*/
public class LayoutManager {
JFrame mJFrame = new JFrame();
public void InitGridLayout(){
mJFrame.setTitle("GridLayout");
mJFrame.setLayout(new GridLayout(3,3,5,5));
for(int i=1; i<10; i++){
JButton mJButton = new JButton(String.valueOf(i));
mJFrame.add(mJButton);
}
mJFrame.setBounds(300, 300, 300, 300);
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void InitFlowLayout(){
mJFrame.setTitle("FlowLayout");
mJFrame.setLayout(new FlowLayout(5, 10, 10));
for(int i=1; i<10; i++){
JButton mJButton = new JButton(String.valueOf(i));
mJFrame.add(mJButton);
}
mJFrame.setBounds(300, 300, 300, 300);
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Panel pnlCommandArea = new Panel();
Panel pnlDisplayArea = new Panel();
CardLayout mCardLayout = new CardLayout();
JButton mjButton = new JButton("Command");
public void InitCardLayout(){
mJFrame.setTitle("CardLayout");
mJFrame.setLayout(new BorderLayout(10,10));
pnlDisplayArea.setLayout(mCardLayout);
Panel pnlFirst = new Panel();
Panel pnlSed = new Panel();
Panel pnlThird = new Panel();
pnlFirst.add(new Label("first"));
pnlSed.add(new Label("second"));
pnlThird.add(new Label("third"));
pnlFirst.add(new JButton("button 1"));
pnlSed.add(new JButton("button 2"));
pnlThird.add(new JButton("button 3"));
pnlDisplayArea.add(pnlFirst,"Cardlayout_first");
pnlDisplayArea.add(pnlSed,"Cardlayout_sed");
pnlDisplayArea.add(pnlThird,"Cardlayout_Third");
((CardLayout)pnlDisplayArea.getLayout()).show(pnlDisplayArea,"CardLayout_first");
mjButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
// ProcessAction(e);
mCardLayout.next(pnlDisplayArea);
// ((CardLayout)pnlDisplayArea.getLayout()).show(pnlDisplayArea, "Cardlayout_sed");
}
});
pnlCommandArea.add(mjButton);
mJFrame.add(pnlCommandArea,BorderLayout.NORTH);
mJFrame.add(pnlDisplayArea,BorderLayout.CENTER);
mJFrame.setBounds(300, 300, 300, 300);
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//将组件添加到Box,此时且不需要设置布局,由新建Box时设置水平还是垂直
public void InitBoxLayout(){
mJFrame.setTitle("BoxLayout");
//不需要下面这句话,错误
//mJFrame.setLayout(new BoxLayout(mJFrame,BoxLayout.X_AXIS ));
Box mBox =Box.createHorizontalBox();
for(int i=1; i<15; i++){
JButton mJButton = new JButton(String.valueOf(i));
mBox.add(mJButton);
}
mJFrame.add(mBox);
mJFrame.setBounds(300, 300, 300, 300);
mJFrame.pack();
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//将组件直接添加到Panel,需先设置panel 的布局(为BoxPanel , 垂直还是水平)
//我坐在窗前,有时候还可以看到鸟飞过,面对不过空无一物的天空,底下是城市的边缘,很矮很矮的地方。
public void boxLayout(){
Panel mPanel = new Panel();
BoxLayout mBoxLayout = new BoxLayout(mPanel, BoxLayout.Y_AXIS);
mPanel.setLayout(mBoxLayout);
for(int i=1; i<15; i++){
JButton mJButton = new JButton(String.valueOf(i));
mPanel.add(mJButton);
}
mJFrame.add(mPanel);
mJFrame.setBounds(300, 300, 300, 300);
mJFrame.pack();
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void BoxTopPanel() {
JPanel topPanel = new JPanel();
String[] columnName = { "姓名", "性别", "单位", "参加项目", "备注" };
String[][] rowData = { { "张三", "男", "计算机系", "100 米 ,200 米", "" },
{ "李四", "男", "化学系", "100 米,铅球", "" },
{ "李四", "男", "化学系", "100 米,铅球", "" },
{ "李四", "男", "化学系", "100 米,铅球", "" },
{ "李四", "男", "化学系", "100 米,铅球", "" },
};
// 创建表格
JTable table = new JTable(new DefaultTableModel(rowData, columnName));
// 创建包含表格的滚动窗格
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// 定义 topPanel 的布局为 BoxLayout,BoxLayout 为垂直排列
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
// 先加入一个不可见的 Strut,从而使 topPanel 对顶部留出一定的空间
topPanel.add(Box.createVerticalStrut(10));
// 加入包含表格的滚动窗格
topPanel.add(scrollPane);
// 再加入一个不可见的 Strut,从而使 topPanel 和 middlePanel 之间留出一定的空间
topPanel.add(Box.createVerticalStrut(10));
mJFrame.add(topPanel);
mJFrame.setBounds(300, 300, 300, 150);
//mJFrame.pack();
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void InitGridBagLayout(){
GridBagLayout mGridBagLayout = new GridBagLayout();
mJFrame.setLayout(mGridBagLayout);
GridBagConstraints mGridBagConstraints = new GridBagConstraints();
mGridBagConstraints.fill = GridBagConstraints.BOTH;
mGridBagConstraints.gridx = 0;
mGridBagConstraints.gridy = 0;
mGridBagConstraints.gridheight = 2;
mGridBagConstraints.gridwidth = 1;
mGridBagConstraints.anchor = GridBagConstraints.NORTH;
mGridBagConstraints.weightx = 0;
mGridBagConstraints.weighty = 0;
JButton btn = new JButton("button 1");
mGridBagLayout.setConstraints(btn, mGridBagConstraints);
mJFrame.add(btn);
mGridBagConstraints.fill = GridBagConstraints.BOTH;
mGridBagConstraints.gridx = 300;
mGridBagConstraints.gridy = 300;
mGridBagConstraints.gridheight = GridBagConstraints.REMAINDER;
mGridBagConstraints.gridwidth = 10;
mGridBagConstraints.anchor = GridBagConstraints.SOUTHEAST;
mGridBagConstraints.weightx = 0;
mGridBagConstraints.weighty = 0;
btn = new JButton("button 2");
mGridBagLayout.setConstraints(btn, mGridBagConstraints);
mJFrame.add(btn);
mJFrame.setBounds(300, 300, 300, 300);
// mJFrame.pack();
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void ProcessAction(ActionEvent e){
if(((Button)e.getSource()).equals(mjButton)){
mCardLayout.next(pnlDisplayArea);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
LayoutManager mLayoutManager = new LayoutManager();
// mLayoutManager.InitGridLayout();
//mLayoutManager.InitFlowLayout();
//mLayoutManager.InitCardLayout();//BorderLayout
//
// mLayoutManager.InitBoxLayout();
//mLayoutManager.InitGridBagLayout();
// mLayoutManager.boxLayout();
mLayoutManager.BoxTopPanel();
}
}