//FlowLayout流式布局管理器练习: import java.awt.*; import java.awt.event.*; public class FlowLayoutDemo { public static void main(String[] args) { final Frame f = new Frame("hello"); final FlowLayout f1 = new FlowLayout(); f.setLayout(f1); // 使Frame采用Flowlayout布局 f.setBackground(Color.BLUE); Button leftButton = new Button("left"); leftButton.addActionListener(new ActionListener() { // 注册事件监听器 public void actionPerformed(ActionEvent event) { f1.setAlignment(FlowLayout.LEFT); // 左对齐 f1.layoutContainer(f); // 使Frame重新布局 } }); Button centerButton = new Button("center"); centerButton.addActionListener(new ActionListener() { // 注册事件监听器 public void actionPerformed(ActionEvent event) { f1.setAlignment(FlowLayout.CENTER); // 居中对齐 f1.layoutContainer(f); // 使Frame重新布局 } }); Button rightButton = new Button("right"); rightButton.addActionListener(new ActionListener() { // 注册事件监听器 public void actionPerformed(ActionEvent event) { f1.setAlignment(FlowLayout.RIGHT); // 右对齐 f1.layoutContainer(f); // 使Frame重新布局 } }); f.add(leftButton); f.add(centerButton); f.add(rightButton); f.setSize(300, 200); f.setVisible(true); } }