GUI编程
该笔记由b站 狂神说视频获取
告诉大家该怎么学?
- 这是什么?
- 它怎么玩?
- 该如何去在我们平时运用?
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
简介
Gui的核心技术:Swing AWT
-
因为界面不美观。
-
需要jre环境!
为什么要学习?
- 可以写出自己心中想要的一些小工具
- 工作时候,也可能需要维护到swing界面,概率极小!
- 了解MVC架构,了解监听!
AWT
AWT 介绍
- 包含很多类和接口!GUI!
- 元素:窗口,按钮,文本框
- java.awt

第一个Frame窗口
package com.tian.test1;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame
Frame frame = new Frame("我的第一个java图形界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(54, 117, 151));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口关闭不掉,停止程序运行就行了
尝试封装
package com.tian.test1;
import java.awt.*;
//GUI的第一个界面
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.green);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.red);
}
}
class MyFrame extends Frame{
static int id=0;
public MyFrame(int x,int y,int w,int d,Color color){
super("myframe"+(++id));
setBounds(x, y, w, d);
setVisible(true);
setBackground(color);
}
}
Panel面板讲解
- 解决关闭事件
package com.tian.test1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//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(40,161,35));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(193,15,60));
frame.add(panel);
frame.setVisible(true);
//监听时间,监听窗口关闭事件,System.exit(0)
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
效果图

3种布局管理器
- 流式布局
package com.tian.test1;
import java.awt.*;
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());
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 东西南北中
package com.tian.test1;
import java.awt.*;
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(200,200);
frame.setVisible(true);
}
}
- 表格布局
package com.tian.test1;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
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");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.pack();//java函数!
frame.setVisible(true);
}
}
课堂练习讲解及总结
效果图

package com.tian.test1;
import java.awt.*;
public class TestDemo {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setVisible(true);
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(new Color(39, 82, 118));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2, 1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2, 2));
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
for(int i=0;i<4;i++){
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}
总结
总结:
1.Frame是一个顶级窗口
2.Panel无法单独显示,必须添加到某个容器中。
3.布局管理器
- 流式
- 东西南北中
- 表格
- 大小。定位。背景颜色,可见性,监听!
事件监听
package com.tian.test1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button();
//因为,addActionListener需要一个ActionListener,所以我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
//关闭窗体事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
输入框事件监听
package com.tian.test1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextField1 {
public static void main(String[] args) {
//启动
new MyFrame1();
}
}
class MyFrame1 extends Frame{
public MyFrame1(){
TextField textField = new TextField();
add(textField);
MyListener02 myListener02 = new MyListener02();
//监听这个文本框输入的文字,按下enter就会触发
textField.addActionListener(myListener02);
//设置编码
textField.setEchoChar('*');
pack();
setVisible(true);
}
}
class MyListener02 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();//获得一些资源,返回的一个对象
System.out.println(field.getText());//获得输入框的文本
field.setText("");//null ""
}
}
- 效果文本框输入什么(都是*),控制台打印什么,并清空输入框的内容
简易计算器
oop原则:组合 大于 继承!
oop七大原则 参考博客
class A extends B{
}
class A{
public B b;
}
package com.tian.test1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
public Calculator(){
//3个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);//字符数
TextField num3 = new TextField(20);//字符数
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
private TextField num1,num2,num3;
public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.将这个值+法运算后,放到第三个框
num3.setText(""+(n1+n2));
//3.清除前两个框
num1.setText("");
num2.setText("");
}
}
效果图

画笔paint
package com.tian.test1;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
// g.setColor(Color.red);
g.drawOval(100,100,100,100);
// g.fillOval(100,100,100,100);//实心的圆
// g.setColor(Color.green);
g.fillRect(150,200,200,200);
//养成习惯,画笔用完,将他还原到最初的颜色
}
}
鼠标监听
- 目标:实现鼠标画画

窗口监听
package com.tian.test1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.blue);
setBounds(100,100,200,200);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
});
}
}
键盘监听
package com.tian.test1;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘下的见是哪一个,当前的码
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
System.out.println("上");
}
}
});
}
}
Swing之JFrame窗体
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
public void init(){
JFrame jf = new JFrame("第一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
jf.setBackground(Color.cyan);
//设置文字JLabel
JLabel label = new JLabel("欢迎来到狂神说java系列");
jf.add(label);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
弹窗
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame放东西 容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");//创建
button.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new TanChuangDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class TanChuangDemo extends JDialog{
public TanChuangDemo(){
this.setVisible(true);
this.setBounds(100,100,500,500);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);默认已经有了
Container container = this.getContentPane();
container.setLayout(null);
Label label = new Label("秦老师带你学java");
label.setBounds(10,10,100,100);
container.add(label);
}
}
Icon,ImageIcon
图标
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,frame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){
}
public IconDemo(int width,int height){
this.height=height;
this.width=width;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//图标放在标签,也可以放在按钮上!
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container container=getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片的地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("bz.PNG");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
Jpanel
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
public class JpanelDemo extends JFrame {
public JpanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后面两个参数的意思 间距
JPanel panel = new JPanel(new GridLayout(1,3));
panel.add(new JButton("1"));
panel.add(new JButton("1"));
panel.add(new JButton("1"));
container.add(panel);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JpanelDemo();
}
}
文本域JScroll
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学习java");
//scroller面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100 ,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
按钮
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame{
public JButtonDemo01(){
Container container = this.getContentPane();
URL url = JButtonDemo01.class.getResource("bz.PNG");
Icon icon = new ImageIcon(url);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
container.add(button);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame{
public JButtonDemo02(){
Container container = this.getContentPane();
//单选框
JRadioButton button01 = new JRadioButton("button01");
JRadioButton button02 = new JRadioButton("button02");
JRadioButton button03 = new JRadioButton("button03");
//单选框只能选择一个,分组 一个组只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(button01);
group.add(button02);
group.add(button03);
container.add(button01,BorderLayout.CENTER);
container.add(button02,BorderLayout.NORTH);
container.add(button03,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
多选
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo03 extends JFrame{
public JButtonDemo03(){
Container container = this.getContentPane();
//多选框
JCheckBox check01 = new JCheckBox("check01");
JCheckBox check02 = new JCheckBox("check02");
container.add(check01,BorderLayout.NORTH);
container.add(check02,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
列表
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
public class Demo06 extends JFrame {
public Demo06(){
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo06();
}
}
列表框
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class Demo07 extends JFrame {
public Demo07(){
Container container = this.getContentPane();
//生成列表中的内容
//String[] contents={"1","2","3"};
//列表中需要放入内容
Vector contents = new Vector();
JList jList = new JList(contents);
contents.add("zhangsan");
contents.add("lisi");
contents.add("wangwu");
container.add(jList);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo07();
}
}
应用场景
- 下拉:选择地区,或者一些单个选项
- 列表:展示信息,一般是动态扩容!
文本框
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
public class Demo08 extends JFrame {
public Demo08(){
Container container = this.getContentPane();
JTextField hello = new JTextField("hello");
JTextField hello1 = new JTextField("world",20);
container.add(hello,BorderLayout.SOUTH);
container.add(hello1,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo08();
}
}
密码框
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
public class Demo09 extends JFrame {
public Demo09(){
Container container = this.getContentPane();
JPasswordField pwd = new JPasswordField();
pwd.setEchoChar('*');
container.add(pwd,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo09();
}
}
文本域
package com.tian.Test2;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学习java");
//scroller面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100 ,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
1447

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



