package exerciseOne;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class exerciseOne extends JFrame implements ActionListener{
    Container c;
    FlowLayout fl;
    String str[]={"左齐","中齐","右齐"};
    JButton jb[]=new JButton[str.length];
    String actionStr="左齐";
        
    public exerciseOne() {
		// TODO 自动生成的构造函数存根
    	c=getContentPane();
    	fl=new FlowLayout(FlowLayout.LEFT, 5, 5);
    	c.setLayout(fl);
    	
    	for (int i = 0; i < jb.length; i++) {
			jb[i]=new JButton(str[i]);
			c.add(jb[i]);
			jb[i].addActionListener(this);
		}
    	setSize(400, 200);
    	setVisible(true);
	}    
    
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		actionStr = e.getActionCommand();
		repaint();
	}

	@Override
	public void paint(Graphics g) {
		// TODO 自动生成的方法存根
		super.paint(g);
		switch (actionStr) {
		case "左齐":
			System.out.println("1");
			fl.setAlignment(FlowLayout.LEFT);	
			c.setLayout(fl);
			break;
		case "中齐":
			System.out.println("2");
			fl.setAlignment(FlowLayout.CENTER);
			c.setLayout(fl);
			break;
		case "右齐":
			System.out.println("3");
			fl.setAlignment(FlowLayout.RIGHT);
			c.setLayout(fl);
			break;
		default:
			System.out.println("4");
			break;
		}
	}

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		exerciseOne t=new exerciseOne();
	}

}
