本文主要是介绍GUI编程 (狂神学习笔记)2021-10-8,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
GUI 编程
告诉大家该怎么学?
- 这是什么?
- 它怎么玩
- 该如何去在我们平时运用?
- class-可阅读的
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听时间
- 鼠标
- 键盘事件
- 外挂
- 破解工具
1、简介
Gui的核心技术: Swing AWT
- 因为界面不美观
- 需要jre环境
为什么我们要学习?
- 可以写出自己心中想要的小工具
- 工作时候,也可能需要维护到swing界面,概率极小!
- 了解MVC架构,了解监听!
2、AWT
2.1 AWT介绍
- 包含了很多类和接口! GUI!
- 元素:窗口,按钮,文本框
- java.awt
2.2 组件和容器
1.2.1 Frame
package com.tree.lesson01;
import java.awt.*;
/*** GUI 的第一个界面*/
public class TestFrame {public static void main(String[] args) {Frame frame = new Frame("我的java程序");//设置窗口大小frame.setSize(400,400);// 设置窗口位置frame.setLocation(200,200);//设置背景颜色frame.setBackground(Color.gray);//设置可见性frame.setVisible(true);//设置窗口拉伸frame.setResizable(false);}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GhtqF6b2-1633706763750)(GUI编程.assets/2325334-20210317161930105-314930371.png)]
/*** 创建多个窗口*/
public class TestFrame2 {public static void main(String[] args) {MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.red);MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.yellow);MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.blue);MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.green);}
}
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);}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rAg1aiIO-1633706763754)(GUI编程.assets/2325334-20210317161947465-1768301913.png)]
1.2.2. Panel(面板)
public class TestPanel {public static void main(String[] args) {Frame frame = new Frame();Panel panel = new Panel();//对窗口设置布局、可见性frame.setLayout(null);frame.setVisible(true);//窗口的坐标frame.setBounds(100,100,400,400);frame.setBackground(Color.red);//相对于frame的面板坐标panel.setBounds(50,50,300,300);panel.setBackground(Color.yellow);//将面板添加到窗口frame.add(panel);frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wq5jgLHK-1633706763764)(GUI编程.assets/2325334-20210317162003068-2097307029.png)]
2.3 布局管理器
- 流式布局
public class TestFlowLayout { //流式布局public static void main(String[] args) {Frame frame = new Frame();frame.setVisible(true);Button button1 = new Button("button1");Button button2 = new Button("button2");Button button3 = new Button("button3");Button button4 = new Button("button4");frame.setLayout(new FlowLayout(FlowLayout.LEFT));frame.setSize(300,200);frame.add(button1);frame.add(button2);frame.add(button3);frame.add(button4);}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XwQRgazw-1633706763769)(GUI编程.assets/2325334-20210317162013759-127782931.png)]
- 东西南北中
public class TestBorderLayout {public static void main(String[] args) {Frame frame = new Frame();frame.setVisible(true);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.setSize(300,200);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);}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U3A4UHWT-1633706763773)(GUI编程.assets/2325334-20210317162025494-1657290476.png)]
- 表格布局
public class TestGridLayout {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");Button button4 = new Button("button4");Button button5 = new Button("button5");Button button6 = new Button("button6");//设置表格布局 3*2frame.setLayout(new GridLayout(3,2));frame.add(button1);frame.add(button2);frame.add(button3);frame.add(button4);frame.add(button5);frame.add(button6);frame.setVisible(true);//将表格自动填充于窗口frame.pack();}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L8ZoyI4B-1633706763777)(GUI编程.assets/2325334-20210317162037528-294596330.png)]
课堂练习
/*** 坐标定位 布局法*/
public class TestHomeWork {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");Button button4 = new Button("button4");Button button5 = new Button("button5");Button button6 = new Button("button6");Button button7 = new Button("button7");Button button8 = new Button("button8");Button button9 = new Button("button9");Button button10 = new Button("button10");button1.setBounds(0,0,100,200);frame.add(button1);button2.setBounds(100,0,400,100);frame.add(button2);button3.setBounds(100,100,400,100);frame.add(button3);button4.setBounds(500,0,100,200);frame.add(<
这篇关于GUI编程 (狂神学习笔记)2021-10-8的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!