GUI------AWT

AWT介绍

组件和容器

  1. 容器------Frame
    创建Frame会实现一个窗口界面,可使用Frame中的方法来调节大小,颜色,初始位置等。
package org.ming.lesson01;

import java.awt.*;

public class TestFrame {
    public static void main(String[] args) {

        //Frame,JDK, 看源码!
        Frame frame = new Frame("Frame窗口");

        //设置可见性  
        frame.setVisible(true);

        //设置窗口大小
        frame.setSize(200,200);

        //设置背景颜色  
        frame.setBackground(new Color(30, 150, 40));

        //弹出的初始位置
        frame.setLocation(200,200);

        //设置大小固定
        frame.setResizable(false);//不可更改大小

    }
}

使用AWT创建窗口需要些鉴定窗口关闭事件,否则点击窗口"X“”按钮不会关闭窗口,只能手动终止程序。

frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //关闭
                System.exit(0);
            }
        });
  1. 组件------button(按钮)
    流式布局
package org.ming.lesson01;

import java.awt.*;

public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        //组件-按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        //设置流式布局
        
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));

        frame.setSize(200,200);

        //添加按钮
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);


    }
}

  1. 练习
    在这里插入图片描述
    代码实现
package org.ming.lesson01;

import java.awt.*;

public class ExDemo {

    public static void main(String[] args) {
        //总 Frame
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(300,400);
        frame.setBackground(Color.BLACK);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));


        //4个面板
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,2));

        p1.add(new Button("East-1"),BorderLayout.EAST);
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
        p1.add(p2,BorderLayout.CENTER);

        //下面
        p3.add(new Button("East-2"),BorderLayout.EAST);
        p3.add(new Button("West-2"),BorderLayout.WEST);
        //中间4个
        for (int i = 0; i < 4; i++) {
            p4.add(new Button("for-"+i));
        }
        p3.add(p4,BorderLayout.CENTER);

        frame.add(p1);
        frame.add(p3);
    }

}

总结:

  1. Frame是一个顶级窗口
  2. Panel 无法单独显示,必须添加到某个容器中。
  3. 布局管理器
    1. 流式
    2. 东西南北中
    3. 表格
  4. 大小,定位,背景颜色,可见性,监听!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值