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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
| /**
* 抽象组件 - GUI组件
*/
public abstract class GUIComponent {
protected String name;
protected int x, y, width, height;
protected boolean visible;
public GUIComponent(String name, int x, int y, int width, int height) {
this.name = name;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.visible = true;
}
// 基本操作
public abstract void render();
public abstract void handleClick(int mouseX, int mouseY);
// 组合相关操作
public void add(GUIComponent component) {
throw new UnsupportedOperationException("不支持添加子组件");
}
public void remove(GUIComponent component) {
throw new UnsupportedOperationException("不支持删除子组件");
}
// 通用方法
public void setVisible(boolean visible) {
this.visible = visible;
System.out.println(name + (visible ? " 显示" : " 隐藏"));
}
public void move(int newX, int newY) {
this.x = newX;
this.y = newY;
System.out.println(name + " 移动到 (" + x + ", " + y + ")");
}
public void resize(int newWidth, int newHeight) {
this.width = newWidth;
this.height = newHeight;
System.out.println(name + " 调整大小为 " + width + "×" + height);
}
public boolean isPointInside(int pointX, int pointY) {
return pointX >= x && pointX <= x + width && pointY >= y && pointY <= y + height;
}
// getter方法
public String getName() { return name; }
public boolean isVisible() { return visible; }
protected String getIndent(int depth) {
StringBuilder indent = new StringBuilder();
for (int i = 0; i < depth; i++) {
indent.append(" ");
}
return indent.toString();
}
}
/**
* 叶子节点 - 按钮
*/
public class Button extends GUIComponent {
private String text;
private String color;
public Button(String name, int x, int y, int width, int height, String text, String color) {
super(name, x, y, width, height);
this.text = text;
this.color = color;
}
@Override
public void render() {
if (visible) {
System.out.println("🔘 渲染按钮: " + name + " | 位置(" + x + "," + y + ") | 大小" + width + "×" + height +
" | 文本: \"" + text + "\" | 颜色: " + color);
}
}
@Override
public void handleClick(int mouseX, int mouseY) {
if (visible && isPointInside(mouseX, mouseY)) {
System.out.println("🖱️ 按钮 \"" + text + "\" 被点击!");
onClick();
}
}
public void onClick() {
System.out.println(" ✨ 执行按钮动作: " + text);
}
public void setText(String text) {
this.text = text;
System.out.println("🔘 按钮 " + name + " 文本更新为: \"" + text + "\"");
}
public String getText() { return text; }
}
/**
* 叶子节点 - 文本框
*/
public class TextBox extends GUIComponent {
private String content;
private boolean editable;
public TextBox(String name, int x, int y, int width, int height, boolean editable) {
super(name, x, y, width, height);
this.content = "";
this.editable = editable;
}
@Override
public void render() {
if (visible) {
System.out.println("📝 渲染文本框: " + name + " | 位置(" + x + "," + y + ") | 大小" + width + "×" + height +
" | 内容: \"" + content + "\" | " + (editable ? "可编辑" : "只读"));
}
}
@Override
public void handleClick(int mouseX, int mouseY) {
if (visible && isPointInside(mouseX, mouseY)) {
if (editable) {
System.out.println("📝 文本框 " + name + " 获得焦点,可以输入文本");
} else {
System.out.println("📝 只读文本框 " + name + " 被点击");
}
}
}
public void setContent(String content) {
if (editable) {
this.content = content;
System.out.println("📝 文本框 " + name + " 内容更新为: \"" + content + "\"");
} else {
System.out.println("❌ 文本框 " + name + " 为只读,无法修改内容");
}
}
public String getContent() { return content; }
}
/**
* 复合节点 - 面板(容器)
*/
public class Panel extends GUIComponent {
private List<GUIComponent> children;
private String backgroundColor;
public Panel(String name, int x, int y, int width, int height, String backgroundColor) {
super(name, x, y, width, height);
this.children = new ArrayList<>();
this.backgroundColor = backgroundColor;
}
@Override
public void add(GUIComponent component) {
children.add(component);
System.out.println("📋 组件 " + component.getName() + " 添加到面板 " + name);
}
@Override
public void remove(GUIComponent component) {
children.remove(component);
System.out.println("📋 组件 " + component.getName() + " 从面板 " + name + " 移除");
}
@Override
public void render() {
if (visible) {
System.out.println("📋 渲染面板: " + name + " | 位置(" + x + "," + y + ") | 大小" + width + "×" + height +
" | 背景: " + backgroundColor + " | 子组件数: " + children.size());
// 递归渲染所有子组件
for (GUIComponent child : children) {
child.render();
}
}
}
@Override
public void handleClick(int mouseX, int mouseY) {
if (visible && isPointInside(mouseX, mouseY)) {
System.out.println("📋 面板 " + name + " 被点击");
// 将点击事件传递给子组件(从上到下查找)
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).handleClick(mouseX, mouseY);
}
}
}
public List<GUIComponent> getChildren() {
return new ArrayList<>(children);
}
public void setBackgroundColor(String backgroundColor) {
this.backgroundColor = backgroundColor;
System.out.println("📋 面板 " + name + " 背景色更改为: " + backgroundColor);
}
// 查找子组件
public GUIComponent findComponent(String name) {
if (this.name.equals(name)) {
return this;
}
for (GUIComponent child : children) {
if (child.getName().equals(name)) {
return child;
}
// 如果是面板,递归查找
if (child instanceof Panel) {
GUIComponent found = ((Panel) child).findComponent(name);
if (found != null) {
return found;
}
}
}
return null;
}
// 设置所有子组件的可见性
@Override
public void setVisible(boolean visible) {
super.setVisible(visible);
for (GUIComponent child : children) {
child.setVisible(visible);
}
}
}
/**
* 复合节点 - 窗口
*/
public class Window extends Panel {
private String title;
private boolean resizable;
public Window(String name, int x, int y, int width, int height, String title) {
super(name, x, y, width, height, "white");
this.title = title;
this.resizable = true;
}
@Override
public void render() {
if (visible) {
System.out.println("🪟 渲染窗口: " + title + " | 位置(" + x + "," + y + ") | 大小" + width + "×" + height);
System.out.println(" 窗口标题栏: " + title);
System.out.println(" 窗口内容区域:");
// 渲染子组件(缩进显示)
for (GUIComponent child : getChildren()) {
child.render();
}
}
}
public void setTitle(String title) {
this.title = title;
System.out.println("🪟 窗口标题更新为: " + title);
}
public String getTitle() { return title; }
}
// GUI组件组合模式演示
public class GUICompositeDemo {
public static void main(String[] args) {
System.out.println("=== GUI组件组合模式演示 ===");
// 创建主窗口
Window mainWindow = new Window("mainWindow", 100, 100, 800, 600, "用户管理系统");
// 创建顶部工具栏面板
Panel toolbarPanel = new Panel("toolbar", 0, 0, 800, 50, "lightgray");
Button newButton = new Button("newBtn", 10, 10, 80, 30, "新建", "blue");
Button saveButton = new Button("saveBtn", 100, 10, 80, 30, "保存", "green");
Button deleteButton = new Button("deleteBtn", 190, 10, 80, 30, "删除", "red");
toolbarPanel.add(newButton);
toolbarPanel.add(saveButton);
toolbarPanel.add(deleteButton);
// 创建左侧导航面板
Panel navPanel = new Panel("navigation", 0, 50, 200, 550, "lightblue");
Button usersButton = new Button("usersBtn", 10, 60, 180, 40, "用户管理", "gray");
Button ordersButton = new Button("ordersBtn", 10, 110, 180, 40, "订单管理", "gray");
Button reportsButton = new Button("reportsBtn", 10, 160, 180, 40, "报表统计", "gray");
navPanel.add(usersButton);
navPanel.add(ordersButton);
navPanel.add(reportsButton);
// 创建主内容面板
Panel contentPanel = new Panel("content", 200, 50, 600, 550, "white");
// 用户信息表单面板
Panel userFormPanel = new Panel("userForm", 20, 20, 560, 300, "lightgray");
TextBox nameTextBox = new TextBox("nameInput", 100, 30, 200, 25, true);
TextBox emailTextBox = new TextBox("emailInput", 100, 70, 200, 25, true);
TextBox phoneTextBox = new TextBox("phoneInput", 100, 110, 200, 25, true);
nameTextBox.setContent("张三");
emailTextBox.setContent("zhangsan@example.com");
phoneTextBox.setContent("138****1234");
userFormPanel.add(nameTextBox);
userFormPanel.add(emailTextBox);
userFormPanel.add(phoneTextBox);
// 表单操作按钮
Panel formButtonPanel = new Panel("formButtons", 20, 350, 560, 60, "lightgray");
Button submitButton = new Button("submitBtn", 200, 15, 80, 30, "提交", "green");
Button cancelButton = new Button("cancelBtn", 290, 15, 80, 30, "取消", "gray");
formButtonPanel.add(submitButton);
formButtonPanel.add(cancelButton);
contentPanel.add(userFormPanel);
contentPanel.add(formButtonPanel);
// 将所有面板添加到主窗口
mainWindow.add(toolbarPanel);
mainWindow.add(navPanel);
mainWindow.add(contentPanel);
System.out.println("\n=== 渲染整个GUI界面 ===");
mainWindow.render();
System.out.println("\n=== 模拟鼠标点击事件 ===");
System.out.println("点击保存按钮:");
mainWindow.handleClick(140, 25); // 点击保存按钮
System.out.println("\n点击用户管理按钮:");
mainWindow.handleClick(100, 80); // 点击用户管理按钮
System.out.println("\n点击姓名输入框:");
mainWindow.handleClick(200, 55); // 点击姓名输入框
System.out.println("\n点击提交按钮:");
mainWindow.handleClick(240, 365); // 点击提交按钮
System.out.println("\n=== 组件查找功能 ===");
GUIComponent foundComponent = mainWindow.findComponent("emailInput");
if (foundComponent instanceof TextBox) {
TextBox emailBox = (TextBox) foundComponent;
System.out.println("找到邮箱输入框,当前内容: \"" + emailBox.getContent() + "\"");
emailBox.setContent("newemail@example.com");
}
System.out.println("\n=== 动态修改界面 ===");
System.out.println("隐藏删除按钮:");
deleteButton.setVisible(false);
System.out.println("\n更改窗口标题:");
mainWindow.setTitle("用户管理系统 v2.0");
System.out.println("\n移动提交按钮:");
submitButton.move(150, 15);
System.out.println("\n=== 批量操作 ===");
System.out.println("隐藏整个表单面板(包括所有子组件):");
userFormPanel.setVisible(false);
System.out.println("\n重新显示表单面板:");
userFormPanel.setVisible(true);
System.out.println("\n观察:无论是单个组件还是容器,都可以用统一的方式处理!");
}
}
|