import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FlowLayoutDemo{
  public static void main(String args[]){
    final JFrame f=new JFrame("Hello");

    JPanel jPanel=new JPanel();  
    final FlowLayout fl=(FlowLayout)jPanel.getLayout();
    f.setContentPane(jPanel);
  
    JButton leftButton=new JButton("left");
    leftButton.addActionListener(new ActionListener(){  //注册事件监听器
       public void actionPerformed(ActionEvent event){
         fl.setAlignment(FlowLayout.LEFT);  //左对齐
         fl.layoutContainer(jPanel);  //使面板重新布局
       }
    });

    JButton centerButton=new JButton("center");
    centerButton.addActionListener(new ActionListener(){  //注册事件监听器
       public void actionPerformed(ActionEvent event){
         fl.setAlignment(FlowLayout.CENTER);  //居中对齐
         fl.layoutContainer(jPanel);  //使面板重新布局
       }
    });

    JButton rightButton=new JButton("right");
    rightButton.addActionListener(new ActionListener(){  //注册事件监听器
       public void actionPerformed(ActionEvent event){
         fl.setAlignment(FlowLayout.RIGHT);  //右对齐
         fl.layoutContainer(jPanel); //使面板重新布局
       }
    });

    jPanel.add(leftButton);
    jPanel.add(centerButton);
    jPanel.add(rightButton);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300,100);
    f.setVisible(true);
  }
}


/****************************************************
 * 作者：孙卫琴                                     *
 * 来源：<<Java面向对象编程>>                       *
 * 技术支持网址：www.javathinker.net                *
 ***************************************************/
