前言

  • 课程的第七篇,下一篇开始进阶相关的内容,故改命名方式为Java进阶one。
  • 以防有新读者所以我简述一下第一篇的前言。
  • 这个Java急速学习课程适合有一定编程基础的人。它通过实际的代码例子来教Java,减少了理论讲解,使学习过程更直观,帮助你更快地掌握Java的核心技能。课程内容经过优化,力求简洁明了。同时,课程鼓励大家交流心得,提出更好的解决方案,以此来不断改进课程内容。

包含的知识

  1. 集合(Collections)
  • ArrayList:使用泛型定义了一个ArrayList对象。
  • 添加元素:list.add(“weiguang1”)。
  • 获取元素:list.get(0)。
  • 遍历元素:使用for循环遍历集合。
  • 删除元素:根据索引删除list.remove(2),根据元素删除list.remove(“java3”)。
  • 修改元素:list.set(0, “weiguang4”)。
  1. 窗口组件(Swing)
  • JFrame:创建主窗口。
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE):设置关闭操作。
  • setSize(400, 300):设置窗口大小。
  • setLocationRelativeTo(null):窗口居中显示。
  • JPanel:创建面板,用于放置其他组件。
  • setLayout(new GridBagLayout()):设置布局管理器。
  • JLabel:创建标签,用于显示文本。
  • setFont(new Font(“楷体”, Font.BOLD, 24)):设置字体。
  • setForeground(new Color(64, 64, 64)):设置前景色。
  • JTextField:创建文本输入框。
  • JPasswordField:创建密码输入框。
  • JButton:创建按钮。
  • setBackground(new Color(0, 128, 255)):设置背景色。
  • setForeground(Color.WHITE):设置前景色。
  • setFont(new Font(“楷体”, Font.BOLD, 14)):设置字体。
  1. Lambda表达式
  • 定义:使用Lambda表达式简化匿名内部类。
  • Animal a = new Animal() { … }:传统匿名内部类。
  • Run p1 = () -> { System.out.println(“跑跑跑~”); }:Lambda表达式。
  • 函数式接口:定义了一个只有一个抽象方法的接口Run,并使用@FunctionalInterface注解。
  1. 字符串处理
  • 创建字符串:
  • 直接赋值:String s1 = “Hi,微光”;
  • 构造器:String s2 = new String();
  • 带参数的构造器:String s3 = new String(“Hello,微光”);
  • 从字符数组创建:String s4 = new String(chars);
  • 从字节数组创建:String s5 = new String(bytes);
  • 字符串方法:
    • length():获取字符串长度。
    • substring(int beginIndex, int endIndex):截取子字符串。
    • equals(String anotherString):比较字符串内容。
  1. 静态内部类
  • 定义:在外部类中定义静态内部类Animal。
  • 特点:
    • 可以直接访问外部类的静态成员。
    • 不能直接访问外部类的实例成员。
    • 创建对象:外部类名.内部类名 对象名 = new 外部类名.内部类名();
  1. 方法引用
  • 静态方法引用:类名::静态方法
  • 实例方法引用:对象名::实例方法
  • 特定类型方法引用:类型名称::方法名
  • 构造器引用:类名::new
  1. 其他
  • 颜色:使用Color类设置组件的颜色。
  • 布局管理器:使用GridBagLayout进行复杂的布局管理。

具体代码

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package basic_0;

import java.util.ArrayList;//ArrayList
import javax.swing.*;//窗口组件
import java.awt.*;//窗口组件
import java.util.Scanner;

public class BasicZc8 {
public static void main(String[] args) {
// API,public static
// ArrayList集合
// 创建ArrayList对象,代表一个集合容器
ArrayList<String> list = new ArrayList<>(); // 泛型定义集合。
// 添加数据
list.add("weiguang1");
list.add("weiguang2");
list.add("weiguang3");
System.out.println(list); // [weiguang1, weiguang2, weiguang3]
// 查看数据
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.get(2));

System.out.println("-------------------------------");
// 遍历集合。
for (int i = 0; i < list.size(); i++) {
// i 0 1 2
String s = list.get(i);
System.out.println(s);
}
// 删除数据
System.out.println(list.remove(2)); // 根据索引删除
System.out.println(list);
System.out.println(list.remove("java3")); // 根据元素删除
System.out.println(list);
// 修改数据
list.set(0, "weiguang4");
System.out.println(list);

System.out.println("-------------------------------");
// 通过窗口组件创建登路功能
login();
System.out.println("窗口已运行~~~");

System.out.println("-------------------------------");
// Lambda表达式,用来简化的
// Lambda不能简化全部的匿名内部类,只能简化函数式接口的匿名内部类
Animal a = new Animal() {
@Override
public void cry() { System.out.println("歪比巴伯~~~");}
};
a.cry();
Run p1 = () -> { System.out.println("跑跑跑~"); };
p1.Run();

System.out.println("-------------------------------");
// 创建字符串对象,封装要处理的字符串数据,调用String处理字符串
// 推荐方式: 直接“”就可以创建字符串对象,封装字符串数据。
String s1 = "Hi,微光";
System.out.println(s1);
System.out.println(s1.length()); // 处理字符串的方法

// 不推荐方式:通过构造器初始化对象。
String s2 = new String();
System.out.println(s2); // ""空字符串

String s3 = new String("Hello,微光");
System.out.println(s3);

char[] chars = {'h','e','l','l','o',',','微','光'};
String s4 = new String(chars);
System.out.println(s4);

byte[] bytes = {97, 98, 99, 65, 66, 67};
String s5 = new String(bytes);
System.out.println(s5);

System.out.println("========================================");
// 只有“”给出的字符串对象放在字符串常量池,相同内容只放一个。
String t1 = "abc";
String t2 = "abc";
System.out.println(t1 == t2);

String t3 = new String("abc");
String t4 = new String("abc");
System.out.println(t3 == t4);

System.out.println("========================================");
// 调用字符串的方法,处理字符串数据。
// 简易版的登录:
String okLoginName = "admin";

System.out.println("请您输入您的登录名称:");
Scanner sc = new Scanner(System.in);
String loginName = sc.next();

// 字符串对象的内容比较,千万不要用==,==默认比较地址,字符串对象的内容一样时地址不一定一样
// 判断你字符串内容,建议大家用 String提供的equals方法,只关心内容一样,就返回true,不关心地址。
if(okLoginName.equals(loginName)){
System.out.println("恭喜您,登录成功!");
}else{
System.out.println("登录失败!");
}

System.out.println("========================================");
// 18663656520 ==> 186****6520
System.out.println("请您用手机号码登录:");
String phone = sc.next(); // 18663656520

System.out.println("系统显示以下手机号码进入:");
String newPhone = phone.substring(0, 3) + "****" + phone.substring(7);
System.out.println(newPhone);
}

// 登录界面
public static void login() {
JFrame frame = new JFrame("公司项目登录界面");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null); // 居中显示

// 设置背景颜色
frame.getContentPane().setBackground(new Color(245, 245, 245)); // 灰白色背景

// 创建面板
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10); // 内边距
gbc.fill = GridBagConstraints.HORIZONTAL;

// 标题标签
JLabel titleLabel = new JLabel("欢迎登录");
titleLabel.setFont(new Font("楷体", Font.BOLD, 24));
titleLabel.setForeground(new Color(64, 64, 64)); // 深灰色文字
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
panel.add(titleLabel, gbc);

// 用户名输入框
gbc.gridy++;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.LINE_START;
panel.add(new JLabel("用户名:"), gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.HORIZONTAL;
JTextField usernameField = new JTextField(20);
panel.add(usernameField, gbc);

// 密码输入框
gbc.gridy++;
gbc.gridx = 0;
gbc.fill = GridBagConstraints.NONE;
panel.add(new JLabel("密码:"), gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.HORIZONTAL;
JPasswordField passwordField = new JPasswordField(20);
panel.add(passwordField, gbc);

// 登录按钮
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
JButton loginButton = new JButton("登录");
loginButton.setBackground(new Color(0, 128, 255)); // 蓝色按钮
loginButton.setForeground(Color.WHITE);
loginButton.setFont(new Font("楷体", Font.BOLD, 14));
panel.add(loginButton, gbc);

frame.add(panel);
frame.setVisible(true);
}

// 静态内部类
// 创建对象:外部类名.内部类名 对象名 = new 外部类名.内部类名();
// 1、静态内部类可以直接访问外部类的静态成员
// 2、静态内部类不可以直接访问外部类的实例成员
abstract static class Animal{
public abstract void cry();
}

// 函数式接口:只有一个抽象方法的接口
@FunctionalInterface // 函数式接口注解
interface Run{
void Run();
}
// 静态方法引用: 类名:: 静态方法
// 实例方法引用:对象名::实例方法
// 特定类型方法引用:类型名称::方法名
// 构造器引用: 类名::new
}