GUI简介
1.图像用户界面
为什么少人用?
图形界面不美观,需要JRE环境(比较大)
为什么要学?
mvc思想
把之前java代码语法串连起来
可以做一些小工具
1.AWT
new来创建的 面向对象思想
1.包含许多类和窗口
2.元素 窗口 按钮 文本框
3.java.awt
awt的类图谱
2.组件和容器
2.1frame
第一个frame窗口
Frame frame = new Frame("hello");
//设置窗口可见性
frame.setVisible(true);
//设置窗口初始位置
frame.setLocation(100, 100);
//设置窗口大小
frame.setSize(300, 200);
//设置窗口背景颜色
frame.setBackground(Color.white);
//这是final修饰的常量,直接用类名访问
frame.setBackground(new Color(155,22,55));
//参数是一个color类的类型,通过new一个color类型,构造器,传入rgb的参数
//设置窗口是否可以缩放 false 不缩放
frame.setResizable(false);
//设置窗口标题
frame.setTitle("第一个frame的窗口");
遇到不会的类,直接看源码frame 遇到方法,直接. 遇到参数是另一个类, 直接通过new一个来看源码,看源码的构造器有哪些??
类的封装 封装一个frame
public class lesson1_02 {
public static void main(String[] args) {
myFrame myFrame = new myFrame(100, 100, 300, 200, Color.red);
myFrame myFrame1 = new myFrame(400, 100, 300, 200, Color.BLUE);
myFrame myFrame2 = new myFrame(100, 300, 300, 200, Color.yellow);
myFrame myFrame3 = new myFrame(400, 300, 300, 200, Color.magenta);
}
}
//对frame类的封装;
class myFrame extends Frame{
static int id=0;
public myFrame() {}
public myFrame(int x,int y,int w,int h,Color color)
{
super("myframe"+( ++id));
setVisible(true);
setBounds(x, y, w, h);
setBackground(color);
}
}
2.2panel面板
可以看成一个空间,但是不能单独存在
PANEL的坐标和frame一模一样
//设置panel的坐标 相对于外层frame的坐标
panel.setBounds(50, 50, 400, 300);
panel.setBackground(new Color(155, 50, 99));
需要
LAYOUT 布局
frame.setLayout(null);
可见
frame.setVisible(true);
frame添加组件是panel
frame.add(panel);
关闭窗口de 实现
事件监听 //监听
addwindowliastener(需要windowListenerl类型 )
(需要什么new什么类型)
addwindowliastener(new windowListenerl() )
windowListenerl()发现是个接口,需要实现太多方法
///使用适配器模式
ctrl+t 找到这个接口的实现类
通过new这个实现类类实现这个接口,因为实现类必须实现接口的所有抽象方法
addwindowliastener(new windowAdapter() )
///发现这个 windowAdapter()
重写里面windowsclose()方法
//当关闭按钮,窗口要执行的事件是,关闭程序
system.exit(0);
public static void main(String[] args) {
//panel 可以看成一个空间,不能独立存在
Frame frame = new Frame();
Panel panel = new Panel();
//设置frame的布局为null(不设置看不到里面的pannl,只看到窗口)
frame.setLayout(null);
//设置坐标
frame.setBounds(100 ,100, 500, 400);
frame.setBackground(new Color(0xff5566));
//设置panel的坐标 相对于外层frame的坐标
panel.setBounds(50, 50, 400, 300);
panel.setBackground(new Color(155, 50, 99));
//frame.add(panel)
frame.add(panel);
//设置可以见
frame.setVisible(true);
//监听 监听事件
//因为参数是一个windowListener()的接口,需要实现太多方法
//采用适配器模式
//通过实例化 一个实现了这个接口的的类,就是通过ctrl+t可以查看接口的类
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
3.布局管理
3.1 流式布局
flowlayout
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setLayout(new FlowLayout());
注意frame使用居右或者居左的时候注意
注意frame的layout写法
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
与前面的color不同
frame.setBackground(Color.GREEN);
frame.setBackground(new Color(11,22,99));
因为color类中是final修饰的常量直接 类名调用
farame是构造器中的常量。所以是先写构造器,再按类名.常量
public static void main(String[] args) {
//流式布局(从左到右)
Frame frame = new Frame("这是个布局的窗口");
//button 元素
Button button1 = new Button("标题1");
Button button2 = new Button("标题2");
Button button3 = new Button("标题3");
Button button4 = new Button("标题4");
//布局flow流式布局(默认为center)
//frame.setLayout(new FlowLayout());//默认居中
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
//frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//这个不知是什么
//frame.setLayout(new FlowLayout(FlowLayout.LEADING));
//frame.setLayout(new FlowLayout(FlowLayout.TRAILING));
//添加按钮进frame 一般添加进panel
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
//frame可见性
frame.setVisible(true);
//frame 大小
frame.setSize(400, 200);
//关闭窗口
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
3.2东西南北中 (上下结构)
boderlayout
frame.add(east,BorderLayout.EAST);
注意
没有流式布局中需要setlayut
直接在frame中添加了add(组件,对象)
Button east = new Button("east");
frame.add(east,BorderLayout.EAST);
borderlayout类是一个实现类,实现了接口layoutmanager2
public static void main(String[] args) {
//东西南北中
Frame frame = new Frame();
//按钮元素
Button east = new Button("east");
Button west = new Button("west");
Button north = new Button("north");
Button south = new Button("south");
Button center = new Button("center");
frame.setVisible(true);
frame.setSize(500, 500);
//frame 添加 button
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(north,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
frame.add(center,BorderLayout.CENTER);
//关闭按钮
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
3.3表格布局grid
frame.setLayout(new GridLayout(3,2));
frame.park()//java函数,最优秀的排列
pack函数源码
public static void main(String[] args) {
//grid
Frame frame = new Frame();
//元素button
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
//添加元素button进来
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
//设置布局
frame.setLayout(new GridLayout(3,2));
frame.pack(); //java函数
//设置窗口可见性
frame.setVisible(true);
frame.setSize(500, 500);
//关闭窗口
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});