图形化界面

AWT

Component 组件基类

Container继承体系 容器

组件,容器,布局

image-20221128153654791

设置组件

image-20221128154241804

对容器根的操作

image-20221128154338204

一个简单的界面

1
2
3
4
5
6
7
8
9
10
import java.awt.*;

public class Main {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setLocation(100, 100);
frame.setSize(500,500); //设置窗口大小
frame.setVisible(true);//窗口可见
}
}

组件和容器

Panel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.awt.*;

public class Main {
public static void main(String[] args) {
Frame frame = new Frame("title");

//创建一个容器
Panel p = new Panel();

//添加组件
p.add(new TextField("这是一个组件"));
p.add(new Button("这是一个按钮组件")); //需要设置编码

frame.add(p);
frame.setBounds(200,200,500,500);
frame.setVisible(true);
}
}

ScrollPane

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.awt.*;

public class Main {
public static void main(String[] args) {
Frame frame = new Frame("title");

//创建一个容器
ScrollPane scrollPane = new ScrollPane();

scrollPane.add(new TextField("这是一个文本")); //只能看到一个文本框
scrollPane.add(new Button("这是一个按钮"));

frame.add(scrollPane);
frame.setBounds(100,100,500,300);
frame.setVisible(true);
}
}

LayoutManager 接口 布局管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.awt.*;

public class Main {
public static void main(String[] args) {
Frame frame = new Frame("title");


for (int i = 0; i < 100; i++) {
frame.add(new Button());
}

frame.setBounds(100,100,500,300);
frame.setVisible(true);
frame.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));//创建一个布局管理器对象
}
}

BoderLayout布局

边框布局,没有指定区域默认center

image-20221128165822416

image-20221128170344878

区域不使用会被覆盖

GridLayout 网格布局

简易的计算器

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
import java.awt.*;

public class Main {
public static void main(String[] args) {
Frame frame = new Frame("计算器");//windows 默认布局

//添加一个容器
Panel panel = new Panel();
panel.add(new TextField(30));
frame.add(panel, BorderLayout.NORTH); //fram 是windows 默认布局 默认BoderLayout 所以不用设置


//创建一个容器布局设置为 网格布局 panel1 3行 5 列 横纵向间距
Panel panel1 = new Panel();
panel1.setLayout(new GridLayout(3,5,4,4));
for (int i = 0; i < 10; i++) {
panel1.add(new Button(i+""));
}
panel1.add(new Button("+"));
panel1.add(new Button("-") );
panel1.add(new Button("*"));
panel1.add(new Button("/"));
panel1.add(new Button("."));
frame.add(panel1);

//智能大小
frame.pack();
frame.setVisible(true);

}
}

image-20221128172553835

GridBagLayout

CardLayout 卡片布局

image-20221128173119295

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
public static void main(String[] args) {
Frame frame = new Frame();
//card 布局
Panel panel = new Panel();
CardLayout cardLayout = new CardLayout();
panel.setLayout(cardLayout);
String[] name = new String[]{"第一张", "第二张", "第三张", "第四张","第五张" };
for (int i = 0; i < 5; i++) {
panel.add(name[i] ,new Button("第"+(i+1)+"张"));
}
frame.add(panel, BorderLayout.CENTER);

//按钮
Panel panel1 = new Panel();
Button b1 = new Button("上一张");
Button b2 = new Button("下一张");
Button b3 = new Button("第一张");
Button b4 = new Button("最后一张");
Button b5 = new Button("第三张");


panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
panel1.add(b4);
panel1.add(b5);

ActionListener actionListener = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand(); //获取组件名称

switch (action){
case "上一张":
cardLayout.previous(panel);
break;
case "下一张":
cardLayout.next(panel);
break;
case "第一张":
cardLayout.first(panel);
break;
case "最后一张":
cardLayout.last(panel);
break;
case "第三张":
cardLayout.show(panel, "第三张"); //查找对应名称
break;

}
}
};

b1.addActionListener(actionListener);
b2.addActionListener(actionListener);
b3.addActionListener(actionListener);
b4.addActionListener(actionListener);
b5.addActionListener(actionListener);
frame.add(panel1,BorderLayout.SOUTH);

frame.pack();
frame.setVisible(true);
}
}

image-20221128183246605

BoxLayout 布局

image-20221128183445384

image-20221128184054555

Box 变量 = Box.createVerticalBox(); 直接添加一个水平容器

间距

image-20221128184316335

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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
public static void main(String[] args) {
Frame frame = new Frame();

//第一个容器
Box box1 = Box.createHorizontalBox();
box1.add(new Button("水平1"));
box1.add(Box.createHorizontalGlue()); //创建一个间隔 水平向两边拉伸
box1.add(new Button("水平2"));
box1.add(Box.createHorizontalStrut(30)); //创建一个固定的水平拉伸
box1.add(new Button("水平3"));


//第二个容器 垂直
Box box2 = Box.createVerticalBox();
box2.add(new Button("垂直1"));
box2.add(Box.createVerticalGlue());
box2.add(new Button("垂直2"));
box2.add(Box.createVerticalStrut(30));
box2.add(new Button("垂直3"));

frame.add(box1, BorderLayout.NORTH);
frame.add(box2);

frame.pack();
frame.setVisible(true);
}
}

基本组件

image-20221128190609022

image-20221128200347389

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {

//底部
TextField textField = new TextField(40);
Button b1 = new Button("确认");

//右边
List coloer = new List(4, true);

//左边
TextArea textArea = new TextArea(10, 30);
Choice choice = new Choice();

CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox checkbox = new Checkbox("男", checkboxGroup, true );
Checkbox checkbox1 = new Checkbox("女", checkboxGroup, true );
Checkbox checkbox2 = new Checkbox("是否已婚");

//组装界面
public void init(){
Frame frame = new Frame();

//底box
Box box3 = Box.createHorizontalBox();
box3.add(textField);
box3.add(b1);
frame.add(box3, BorderLayout.SOUTH);

Box box1_2 = Box.createHorizontalBox();
Box box1 = Box.createVerticalBox();
box1.add(textArea);
Box box1_south = Box.createHorizontalBox();
box1_south.add(Box.createHorizontalStrut(20));
choice.add("红色");
choice.add("绿色");
choice.add("黑色");
box1_south.add(choice);
box1_south.add(checkbox);
box1_south.add(checkbox1);
box1_south.add(checkbox2);
box1.add(box1_south);

box1_2.add(box1);
coloer.add("红色");
coloer.add("黑色");
box1_2.add(coloer);
frame.add(box1_2);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
Main main = new Main();
main.init();
}
}

DIalog

image-20221128203335311

image-20221128203424414

image-20221128204852928

image-20221128204859981

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
43
44
45
46
47
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
Frame frame = new Frame();

Button button1 = new Button("打开文件");
Button button2 = new Button("保存文件");

FileDialog fileDialog1 = new FileDialog(frame, "打开文件" ,FileDialog.LOAD);
FileDialog fileDialog2 = new FileDialog(frame, "关闭文件", FileDialog.SAVE);



//组装界面
public void init(){
frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.SOUTH);

frame.pack();
frame.setVisible(true);

button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileDialog1.setVisible(true);
System.out.println("文件路径" + fileDialog1.getDirectory());
System.out.println("文件名"+ fileDialog1.getFile());
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileDialog2.setVisible(true);
System.out.println("文件路径" + fileDialog2.getDirectory());
System.out.println("文件名"+ fileDialog2.getFile());
}
});
}

public static void main(String[] args) {
Main main = new Main();
main.init();
}
}

事件处理

关系:

(外部动作)事件源、事件、事件监听器

image-20221128210358461

流程

image-20221128210554378

事件

image-20221128211322280

image-20221128211410506

事件监听器

image-20221128211449430

菜单组件

继承关系:

image-20221128212726059

绘图

概念

image-20221128213321499

image-20221128213608077

Graphics对象