二、GUI编程基础
本文章由本人整理笔记。来自 狂神说JAVA链接在此
1、简介
Gui的核心技术: Swing AWT
因为界面不美观。
需要 jre环境 !
为什么我们要学习?1.可以写出自己心中想要的一些小工具
2.工作时候,也可能需要维护到swing界面,概率极小!
3.了解MVC架构,了解监听!

2.Frame窗口
package _JFrame;
import java.awt.*;
public class _Frame {
public static void main(String[] args) {
// ctrl + alt + v 自动填充
Frame f = new Frame("我的第一个GUI程序");
//在内存里。看不见。 需要设置可见性
f.setVisible(true);
//设置窗口大小
f.setSize(400, 400);
//设置背景颜色
f.setBackground(Color.orange);
//弹出的初始位置
// 定位 location
f.setLocation(550, 200);
//设置大小固定
f.setResizable(false); //默认为true 可以改变 , 固定大小 为 false 不可改变
}
}

3.Panel面板
package _JFrame;
//面板相当于区域模块
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class _Panel {
public static void main(String[] args) {
Frame frame = new Frame("面板测试");
frame.setBounds(200,200,300,300);//x,y,w,h
frame.setVisible(true); //可见性
frame.setBackground(new Color(128, 185, 241));
//frame.setBackground(Color.orange);
//布局的概念
Panel panel = new Panel();
//把默认的布局管理设为空,什么都没有。 手动设置布局
frame.setLayout(null);
//panel设置坐标,相对于frame
panel.setBounds(50,50,200,200);
//panel 设置颜色
panel.setBackground(new Color(92, 238, 106));
//在frame里面 加入 panel 面板
frame.add(panel);
//监听事件,监听窗口关闭事件
//适配器模式 WindowAdapter implements WindowListener
frame.addWindowListener(new WindowAdapter() {
@Override // 窗口点击关闭的时候,要做的事情
public void windowClosing(WindowEvent e) {
//程序结束
System.exit(0);
}
});
}
}

4.三种布局管理器
(1)、流式布局
组件 --> 按钮:botton
Botton botton = new Botton(value);
最后也要把按钮加进frame里面
frame.add(button);
package _JFrame.Bujv;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//布局管理器:
// 流式布局,FlowLayout
// 东西南北中,
// 表格布局
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame("按钮测试");
frame.setBounds(100, 100, 300, 300);
frame.setVisible(true);
frame.setBackground(Color.green);
//frame.setLayout(new FlowLayout()); //设置流式布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //靠左
//frame.setLayout((new FlowLayout(FlowLayout.CENTER))); //居中
//frame.setLayout((new FlowLayout(FlowLayout.RIGHT))); //靠右
//组件 -- 按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
frame.add(button1); //把按钮添加进frame
frame.add(button2); //把按钮添加进frame
frame.add(button3); //把按钮添加进frame
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
(2)、东南西北中
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HVgriYJW-1649904211730)(assets/1637661210768.png)]](https://i-blog.csdnimg.cn/blog_migrate/3024a981407ef1a4c9e47d460acfa524.png)
package _JFrame.Bujv;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setBounds(100,100,400,400);
frame.setBackground(Color.pink);
Button east = new Button("east");
Button west = new Button("west");
Button soust = new Button("soust");
Button north = new Button("north");
Button center = new Button("center");
frame.add(east,BorderLayout.EAST); //东
frame.add(west,BorderLayout.WEST); //西
frame.add(soust,BorderLayout.SOUTH);//南
frame.add(north,BorderLayout.NORTH);//北
frame.add(center,BorderLayout.CENTER);//中
frame.addWindowListener(new WindowAdapter() { //关闭窗口 (添加一个窗口监听器)
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

(3)、表格布局
package _JFrame.Bujv;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class _GridLayout {
public static void main(String[] args) {
Frame frame = new Frame("面板测试");
frame.setBounds(200, 200, 300, 300);
frame.setVisible(true);
frame.setBackground(new Color(128, 185, 241));
frame.setLayout(new GridLayout(2, 2));//2行2列
Button button = new Button("1");
Button button1 = new Button("2");
Button button2 = new Button("3");
Button button3 = new Button("4");
frame.add(button);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bQzOg3CK-1649904211731)(assets/1638238961692.png)]](https://i-blog.csdnimg.cn/blog_migrate/ef0d3ead490e43bd2536a9a767b6995d.png)
(4)、综合案例
实现效果:

分析一下: 需要 4个面板
1 , 2 面板 采用 南北 方向布局
按钮111,222,555,666 按钮 采用 东西 方向布局
3,4 面板 采用 表格布局
package _JFrame.Bujv;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class bottonTest {
public static void main(String[] args) {
Frame frame = new Frame("botton测试");
frame.setVisible(true);
frame.setBackground(Color.pink);
frame.setBounds(100, 100, 400, 400);
frame.setLayout(new GridLayout(2,1)); //分为两行一列的 上下两部分
// 可不必实例化 Button button3 = new Button("cc");
//设置面板的属性
Panel panel1 = new Panel(new BorderLayout()); //上
panel1.setBackground(Color.orange);
Panel panel2 = new Panel(new GridLayout(2, 1));//上中
panel2.setBackground(Color.green);
Panel panel3 = new Panel(new BorderLayout()); //下
panel3.setBackground(Color.yellow);
Panel panel4 = new Panel(new GridLayout(2, 2)); //下中
panel1.setBounds(0, 0, 400, 200);// 面板1的位置 (上)
panel3.setBounds(0, 0, 400, 200);// 面板3的位置 (下)
//把面板加入frame
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.add(panel4);
panel1.add(new Button("111"), BorderLayout.WEST); //添加到左上
panel1.add(new Button("222"), BorderLayout.EAST); //添加到右上
panel1.add(panel2); //把面板2 加进 面板1里
panel2.add(new Button("333")); //直接 匿名对象
panel2.add(new Button("444"));
panel3.add(new Button("555"), BorderLayout.WEST); //添加到 下部
panel3.add(new Button("666"), BorderLayout.EAST);
//下中
for (int i = 7; i <=10 ; i++) {
panel4.add(new Button("for"+i));
}
panel3.add(panel4);
frame.addWindowListener(new WindowAdapter() { //关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DqyZ2tpi-1649904211733)(assets/1638239864186.png)]](https://i-blog.csdnimg.cn/blog_migrate/74685b4b1239fae4fd330b95fd245eee.png)
(5)、dispose
JFrame jf = new JFrame();
jf.dispose();
1.使用dispose()方法关闭窗体会释放该窗体的占用的部分资源,不过呢不是全部的,如上面说的,只是屏幕资源。2.使用dispose()方法关闭的窗体可以使用pack 或 show 方法恢复,并且可以恢复到dispose前的状态(呵呵~感觉好神奇的,一开始都不相信)
关于上面的还有几点要说明的就是:1.如果dispose掉最后的窗体(程序启动就显示的那个),程序将终止
本文介绍了Java GUI编程的基础知识,包括Swing和AWT的核心技术,讲解了Frame窗口、Panel面板的使用,以及三种布局管理器(流式布局、东南西北中布局和表格布局)的应用。通过实例展示了如何创建和布局GUI组件,同时讨论了窗口关闭监听和dispose方法的使用。
628

被折叠的 条评论
为什么被折叠?



