GUI编程


GUI编程

1、简介

GUI的核心技术:Swing AWT

  1. 界面不美观
  2. 需要JRE环境

为什么要学习?

  1. 可以写出自己心中想要的小工具
  2. 工作时,可能要维护Swing界面
  3. 了解MVC架构,了解监听

2、AWT

2.1、AWT介绍

  1. 包含了很多的类和接口

GUI:图形用户界面编程

  1. 元素:窗口,按钮,文本框
  2. java.awt

2.2、组件和容器

1、Frame
1
2
3
4
5
6
7
8
9
public static void main(String[] args) {
Frame frame = new Frame("我的第一个java图形界面窗口");
//设置可见性
frame.setVisible(true);
frame.setSize(400, 400);
frame.setBackground(new Color(123, 45, 243));
frame.setLocation(200, 200);
frame.setResizable(false);
}

问题:窗口关闭不掉

2、Panel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.setBounds(50,50,400,400);
panel.setBackground(new Color(193,15,60));
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

解决了窗口关闭问题

3、布局管理器
  • 流式布局
1
2
3
4
5
6
7
8
9
10
11
12
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);
}
  • 东西南北中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) {
Frame testBorderLayout = new Frame("TestBorderLayout");
Button east = new Button("East");
Button west = new Button("West");
Button center = new Button("Center");
Button north = new Button("North");
Button south = new Button("South");
testBorderLayout.add(east,BorderLayout.EAST);
testBorderLayout.add(west,BorderLayout.WEST);
testBorderLayout.add(north,BorderLayout.NORTH);
testBorderLayout.add(south,BorderLayout.SOUTH);
testBorderLayout.add(center,BorderLayout.CENTER);
testBorderLayout.setSize(500,500);
testBorderLayout.setVisible(true);
}
  • 表格布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button b1 = new Button("b1");
Button b2 = new Button("b2");
Button b3 = new Button("b3");
Button b4 = new Button("b4");
Button b5 = new Button("b5");
Button b6 = new Button("b6");
frame.setLayout(new GridLayout(3,2));
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(b4);
frame.add(b5);
frame.add(b6);
frame.pack();
frame.setVisible(true);
}
4、事件监听

当某个事情发生的时候,该干什么

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.dogegg.lesson2;

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();

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("aa");
}
}
5、输入框TextField监听
6、画笔
7、鼠标监听
8、窗口监听 WindowListener
9、键盘监听

3、Swing

1、窗口
2、弹窗
3、标签

label

4、面板

JPanel

JScrollPanel

5、按钮
  • 单选按钮
  • 复选按钮
6、列表
7、文本框
  • 文本框
  • 密码框
  • 文本域

文章作者: ZDogEgg
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 ZDogEgg !