GUI编程(图形界面编程)上
定义
图形用户界面是一种人与计算机通信的界面显示格式,允许用户使用鼠标等输入设备操纵屏幕上的图标或菜单选项,以选择命令、调用文件、启动程序或执行其它一些日常任务。与通过键盘输入文本或字符命令来完成例行任务的字符界面相比,图形用户界面有许多优点。图形用户界面由窗口、下拉菜单、对话框及其相应的控制机制构成,在各种新式应用程序中都是标准化的,即相同的操作总是以同样的方式来完成,在图形用户界面,用户看到和操作的都是图形对象,应用的是计算机图形学的技术。[1]
组件(Component)
窗口
单窗
面板
文本框
列表框
按钮
图片
监听事件
鼠标
键盘事件
破解工具
1、简介
Gui的核心技术:Swing,AWT
缺点:
1.界面不美观
2.需要jre环境
2、AWT
2.1Awt介绍
1.包含了很多类和接口! GUI:
2.元素:窗口,按钮,文本框
3.java.awt
2.2组件和容器
Window窗口
1.Frame
package com.zou.listen01;
import java.awt.*;
//Gui的第一个界面
public class TextFrame {
public static void main(String[] args) {
//Frame
Frame frame=new Frame("我的第一个Java图像界面");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(191, 56, 56));
//弹出的初始化位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
运行图:
问题:无法关闭界面
解决方案:停止Java程序进行
多个界面运行
package com.zou.listen01;
import java.awt.*;
public class TextFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.pink);
MyFrame myFrame2 = new MyFrame(100,300,200,200,Color.pink);
MyFrame myFrame3 = new MyFrame(400,100,200,200,Color.pink);
MyFrame myFrame4 = new MyFrame(400,300,200,200,Color.pink);
}
}
class MyFrame extends Frame{
static int id=0;//可能存在多个窗口
public MyFrame(int x,int y,int w,int h,Color color) {//构造器
super("MyFrame+"+(++id));
setBackground(color);//设置背景颜色
setBounds(x,y,w,h);//可以将初始化位置 窗口大小同时设定
setVisible(true);//可见性
setResizable(false);//设置大小固定
}
}
运行图:
2.Panel面板
窗口是不变的,而窗口里要实现的东西都在面板上实现,这就是Panel存在的意义
代码:
package com.zou.listen01;
import java.awt.*;
//Panel 可以看成一个空间,但是不能单独存在。
public class TextPanel {
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(206, 49, 118));
//panel设置坐标,相对于frame。
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(0, 255, 0));
//frame添加一个面板panel
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exut(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
}
运行图:
3、布局管理器(layout)
frame add(button1,布局名称layout.方向)
1.流式布局(按钮位置是连续的)
流式布局是一种等比例缩放布局方式,在CSS代码中使用百分比来设置宽度,也称百分比自适应的布局。 流式布局实现方法是将CSS固定像素宽度换算为百分比宽度。
package com.zou.listen01;
import java.awt.*;
public class TextFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame("流式布局");
frame.setBounds(200,200,300,300);
//组件--按钮 button
Button button1 = new Button("开始");
Button button2 = new Button("结束");
Button button3 = new Button("制作人员");
//设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
//frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
效果图
2.东西南北中
在东西南北中都设置了按钮布局
代码:
package com.zou.listen01;
import java.awt.*;
public class TextBorderlayout {
public static void main(String[] args) {
Frame frame = new Frame("东西南北中布局");
frame.setBounds(400,400,400,400);
frame.setBackground(new Color(0, 130, 255));
frame.setVisible(true);
Button button1=new Button("东");
Button button2=new Button("西");
Button button3=new Button("南");
Button button4=new Button("北");
Button button5=new Button("中");
button1.setBackground(Color.red);
button2.setBackground(Color.red);
button3.setBackground(Color.red);
button4.setBackground(Color.red);
button5.setBackground(Color.red);
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.SOUTH);
frame.add(button4,BorderLayout.NORTH);
frame.add(button5,BorderLayout.CENTER);
}
}
运行图
3.表格布局Grid
代码:
package com.zou.listen01;
import java.awt.*;
public class TextGridlayout {
public static void main(String[] args) {
Frame frame = new Frame("表格布局");
frame.setBounds(400,400,400,400);
frame.setBackground(Color.GRAY);
frame.setVisible(true);
Button button1=new Button("1");
Button button2=new Button("2");
Button button3=new Button("3");
Button button4=new Button("4");
Button button5=new Button("5");
Button button6=new Button("6");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();//java函数 自动选择最佳位置
frame.setVisible(true);
}
}
运行图
总结
1.Frame是一个顶级窗口
2.Panel无法单独显示,必须添加到某个容器中
3.布局管理器
- 流式布局 FlowLayout
- 东西南北中布局 Borderlayout
- 表格布局 Gridlalayout
4.大小、定位、背景颜色、可见性