GUI编程
1、简介
怎么学?
- 是什么?
- 怎么用?
- 在平时中去应用?
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
2、AWT简介
Gui的核心技术:Swing AWT(abstract window tools)
1.界面不太美观
2.需要jre运行环境
为什么要学习?
1.写出自己想要的一些小工具
2.工作中是,需要维护swing界面
3.了解MVC架构,了解监听
2.1、AWT介绍
1.包含很多类和接口!GUI!
2.元素:窗口,按钮,文本框
3.java.awt
2.2、组件和容器
1、Frame
public class testFrame01 {
public static void main(String[] args) {
//Frame,JDK,看源码
Frame frame = new Frame("我的第一个Java图像界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 Color
frame.setBackground(new Color(132, 23, 199));
//弹出初始位置
frame.setLocation(200,200);
//设置窗口大小固定
frame.setResizable(false);//窗口大小不可以改变
}
}
2、封装
public class testFrame2 {
public static void main(String[] args) {
//展示多个窗口 new
MyFrame myFrame1 =new MyFrame(100,100,200,200,Color.blue);
MyFrame myFrame2 =new MyFrame(100,300,200,200,Color.pink);
MyFrame myFrame3 =new MyFrame(300,100,200,200,Color.black);
MyFrame myFrame4 =new MyFrame(300,300,200,200,Color.orange);
}
}
class MyFrame extends Frame {//子类继承父类的所有特性
static int id = 0;//可能存在多个窗口,需要一个计数器,默认窗口的id为0
public MyFrame(int x,int y,int w,int h,Color color){//创建一个构造器,定义几个变量参数
super("MyFrame+"+(++id));//调用父类构造器,每new一个就让id自增1
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);//封装,继承了父类的方法
}
}
实现效果如下:
3、面板Panel
解决关闭事件
//Panel 可以看成是一个空间,但是不能单独存在
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局 如果不设置默认置顶
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(6, 85, 173));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(50, 44, 44));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
3、布局管理器
- 流式布局
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.LEFT));
frame.setSize(200,200);
//把按钮添加进去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
-
东西南北中
public class TestBorderLayout { public static void main(String[] args) { Frame frame = new Frame(); Button east = new Button("East"); Button west = new Button("West"); Button south = new Button("South"); Button north = new Button("North"); Button center = new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); frame.setSize(210,210); frame.setVisible(true); } }
-
表格布局
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
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");
frame.setLayout(new GridLayout(2,3));//设置布局
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();//Java函数,自动设置布局
frame.setVisible(true);
}
}
课堂练习
代码实现
public class practise01 {
public static void main(String[] args) {
//总 Frame
Frame frame = new Frame();
frame.setSize(400,300);
frame.setLocation(400,400);
frame.setBackground(Color.blue);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
//四个面板
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2,2));
//上面
panel1.add(new Button("East-1"),BorderLayout.EAST);
panel1.add(new Button("West-1"),BorderLayout.WEST);
panel2.add(new Button("panel2-btn-1"));
panel2.add(new Button("panel2-btn-2"));
//将panel2加入到panel1中
panel1.add(panel2,BorderLayout.CENTER);//布局
//下面
panel3.add(new Button("East-2"),BorderLayout.EAST);
panel3.add(new Button("West-2"),BorderLayout.WEST);
//下面中间
panel4.add(new Button("panel4-btn-1"));
panel4.add(new Button("panel4-btn-2"));
panel4.add(new Button("panel4-btn-3"));
panel4.add(new Button("panel4-btn-4"));
//for循环进行添加panel4的按钮
// for (int i = 0; i < 4; i++) {
// panel4.add(new Button("for-"+i));
// }
panel3.add(panel4,BorderLayout.CENTER);
//将panel面板加入到frame中
frame.add(panel1);
frame.add(panel3);
//增加监听事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结:
1.Frame是一个顶级窗口
2.Panel无法单独显示,必须添加到某个容器中
3.布局管理器
1、流式
2、东西南北中
3、表格
4.大小,定位,背景颜色,可见性,监听!