a- package com.fmi110;
- import java.awt.Button;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class FrameDemo4 {
- /**
- * 创建窗口,添加按钮,并对按钮添加事件
- */
- public static void main(String[] args) {
- //creat a frame
- Frame f = new Frame("添加按钮和事件演示");
- //set sizes
- f.setBounds(300,300,400,300);
- //add listener for closing
- f.addWindowListener(new WindowAdapter(){
- @Override
- public void windowClosing(WindowEvent e) {
- super.windowClosing(e);
- System.out.println("窗口关闭");
- System.exit(0);
- }
-
- });
- //设置布局方式
- f.setLayout(new FlowLayout());//流式布局
- // f.setLayout(new CardLayout());//卡片式布局
- // f.setLayout(new GridLayout());//网格布局
- // f.setLayout(new BorderLayout());
- //添加按钮,并设置标签
- Button b = new Button("这是按钮1"); b.setSize(20, 10);
- Button b2 = new Button("这是按钮2");b2.setSize(20, 10);
- Button b3 = new Button("这是按钮3");b3.setSize(20, 10);
- f.add(b);
- f.add(b2);
- f.add(b3);
- //给按钮添加事件监听
- b.addActionListener(new ActionListener(){
- @Override //事件执行
- public void actionPerformed(ActionEvent e){
- System.out.println("点了,我就要显示~~~");
- }
-
- });
- //设置窗口可见
- f.setVisible(true);
- }
- }
复制代码
|
|