flowlayoutdemo.java

来自「这是孙卫琴在"Java与面向对象编程"一书的源代码。」· Java 代码 · 共 46 行

JAVA
46
字号
import java.awt.*;
import java.awt.event.*;
public class FlowLayoutDemo{
  public static void main(String args[]){
    final Frame f=new Frame("hello");
    final FlowLayout fl=new FlowLayout();
    
    f.setLayout(fl);  //使Frame采用FlowLayout布局

    Button leftButton=new Button("left");
    leftButton.addActionListener(new ActionListener(){  //注册事件监听器
       public void actionPerformed(ActionEvent event){
         fl.setAlignment(FlowLayout.LEFT);  //左对齐
         fl.layoutContainer(f);  //使Frame重新布局
       }
    });
    Button centerButton=new Button("center");
    centerButton.addActionListener(new ActionListener(){  //注册事件监听器
       public void actionPerformed(ActionEvent event){
         fl.setAlignment(FlowLayout.CENTER);  //居中对齐
         fl.layoutContainer(f);  //使Frame重新布局
       }
    });
    Button rightButton=new Button("right");
    rightButton.addActionListener(new ActionListener(){  //注册事件监听器
       public void actionPerformed(ActionEvent event){
         fl.setAlignment(FlowLayout.RIGHT);  //右对齐
         fl.layoutContainer(f); //使Frame重新布局
       }
    });
    
    f.add(leftButton);
    f.add(centerButton);
    f.add(rightButton);
    f.setSize(200,100);
    f.setVisible(true);
  }
}


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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?