《Java核心技术+卷1:基础知识(原书第9版)》
第 1 章 Java 程序设计概述······················ 1 1.1 Java 程序设计平台···························· 1 1.2 Java“白皮书”的关键术语 ············· 2 1.2.1 简单性·········································· 2 1.2.2 面向对象······································ 3 1.2.3 网络技能······································ 3 1.2.4 健壮性·········································· 3 1.2.5 安全性·········································· 4 1.2.6 体系结构中立 ······························ 4 1.2.7 可移植性······································ 5 1.2.8 解释型········································· · 5 1.2.9 高性能·········································· 5 1.2.10 多线程········································ 6 1.2.11 动态性········································ 6 1.3 Java applet 与 Internet ······················· 6 1.4 Java 发展简史···································· 7 1.5 关于 Java 的常见误解····················· 10 第 2 章 Java 程序设计环境···················· 13 2.1 安装 Java 开发工具箱····················· 13 2.1.1 下载 JDK···································· 13 2.1.2 设置执行路径 ···························· 14 2.1.3 安装库源文件和文档················· 16 2.1.4 安装本书中的示例 ···················· 17 2.1.5 导航 Java 目录··························· 17 2.2 选择开发环境·································· 18 2.3 使用命令行工具······························ 18 2.4 使用集成开发环境·························· 20 2.5 运行图形化应用程序 ······················ 23 2.6 建立并运行 applet ··························· 25 第 3 章 Java 的基本程序设计结构 ······· 29 3.1 一个简单的 Java 应用程序 ············· 29 3.2 注释 ················································· 32 3.3 数据类型·········································· 33 3.3.1 整型············································ 33 3.3.2 浮点类型···································· 34 3.3.3 char 类型···································· 35 3.3.4 boolean 类型 ······························ 36 3.4 变量 ················································· 37 3.4.1 变量初始化································ 37 3.4.2 常量············································ 38 3.5 运算符·············································· 39 3.5.1 自增运算符与自减运算符········· 40 3.5.2 关系运算符与 boolean 运算符 ··· 40 3.5.3 位运算符···································· 41 3.5.4 数学函数与常量 ························ 42 3.5.5 数值类型之间的转换················· 43 3.5.6 强制类型转换 ···························· 43 3.5.7 括号与运算符级别 ···················· 44 3.5.8 枚举类型···································· 45 3.6 字符串·············································· 45 3.6.1 子串············································· 46 3.6.2 拼接············································ 46 3.6.3 不可变字符串 ···························· 46 3.6.4 检测字符串是否相等················· 47 3.6.5 空串与 Null 串··························· 48 3.6.6 代码点与代码单元 ···················· 49 3.6.7 字符串 API································· 49 3.6.8 阅读联机 API 文档···················· 51 3.6.9 构建字符串································ 53 3.7 输入输出·········································· 54 3.7.1 读取输入···································· 54 3.7.2 格式化输出································ 56 3.7.3 文件输入与输出 ························ 60 3.8 控制流程·········································· 61 3.8.1 块作用域···································· 62 3.8.2 条件语句···································· 62 3.8.3 循环············································ 65 3.8.4 确定循环 ·································· 68 3.8.5 多重选择:switch 语句············· 71 3.8.6 中断控制流程语句 ···················· 74 3.9 大数值·············································· 76 3.10 数组 ··············································· 78 3.10.1 for each 循环···························· 79 3.10.2 数组初始化以及匿名数组······· 80 3.10.3 数组拷贝·································· 80 3.10.4 命令行参数······························ 81 3.10.5 数组排序·································· 82 3.10.6 多维数组·································· 85 3.10.7 不规则数组······························ 87 第 4 章 对象与类 ····································· 91 4.1 面向对象程序设计概述 ·················· 91 4.1.1 类················································ 92 4.1.2 对象············································ 93 4.1.3 识别类········································ 93 4.1.4 类之间的关系 ··························· 94 4.2 使用预定义类·································· 95 4.2.1 对象与对象变量 ························ 95 4.2.2 Java 类库中的 GregorianCalendar 类 ································ 98 4.2.3 更改器方法与访问器方法······· 100 4.3 用户自定义类································ 106 4.3.1 Employee 类····························· 106 4.3.2 多个源文件的使用 ·················· 108 4.3.3 剖析 Employee 类···················· 109 4.3.4 从构造器开始 ·························· 110 4.3.5 隐式参数与显式参数················111 4.3.6 封装的优点······························ 112 4.3.7 基于类的访问权限 ·················· 114 4.3.8 私有方法·································· 114 4.3.9 final 实例域······························ 115 4.4 静态域与静态方法························ 115 4.4.1 静态域······································ 115 4.4.2 静态常量·································· 116 4.4.3 静态方法·································· 117 4.4.4 工厂方法·································· 118 4.4.5 main 方法································· 118 4.5 方法参数········································ 121 4.6 对象构造········································ 126 4.6.1 重载·········································· 126 4.6.2 默认域初始化 ·························· 126 4.6.3 无参数的构造器 ······················ 127 4.6.4 显式域初始化 ·························· 128 4.6.5 参数名······································ 129 4.6.6 调用另一个构造器 ·················· 129 4.6.7 初始化块·································· 130 4.6.8 对象析构与 finalize 方法········· 134 4.7 包 ··················································· 134 4.7.1 类的导入·································· 134 4.7.2 静态导入·································· 136 4.7.3 将类放入包中 ·························· 136 4.7.4 包作用域·································· 139 4.8 类路径············································ 140 4.9 文档注释········································ 143 4.9.1 注释的插入······························ 143 4.9.2 类注释······································ 144 4.9.3 方法注释·································· 144 4.9.4 域注释······································ 145 4.9.5 通用注释·································· 145 4.9.6 包与概述注释 ·························· 146 4.9.7 注释的抽取 ··························· 146 4.10 类设计技巧·································· 147 第 5 章 继承············································ 150 5.1 类、超类和子类···························· 150 5.1.1 继承层次·································· 156 5.1.2 多态·········································· 157 5.1.3 动态绑定·································· 158 5.1.4 阻止继承:final 类和方法······· 160 5.1.5 强制类型转换 ·························· 161 5.1.6 抽象类······································ 163 5.1.7 受保护访问······························ 168 5.2 Object:所有类的超类 ················· 169 5.2.1 equals 方法······························· 169 5.2.2 相等测试与继承 ······················ 171 5.2.3 hashCode 方法 ························· 173 5.2.4 toString 方法···························· 175 5.3 泛型数组列表································ 181 5.3.1 访问数组列表元素 ·················· 183 5.3.2 类型化与原始数组列表 的兼容性·································· 186 5.4 对象包装器与自动装箱 ················ 187 5.5 参数数量可变的方法 ···················· 189 5.6 枚举类············································ 191 5.7 反射 ··············································· 192 5.7.1 Class 类···································· 193 5.7.2 捕获异常·································· 195 5.7.3 利用反射分析类的能力··········· 196 5.7.4 在运行时使用反射分析对象··· 201 5.7.5 使用反射编写泛型数组代码··· 206 5.7.6 调用任意方法 ·························· 209 5.8 继承设计的技巧···························· 212 第 6 章 接口与内部类··························· 215 6.1 接口 ··············································· 215 6.1.1 接口的特性······························ 220 6.1.2 接口与抽象类 ·························· 222 6.2 对象克隆········································ 222 6.3 接口与回调···································· 228 6.4 内部类············································ 231 6.4.1 使用内部类访问对象状态······· 232 6.4.2 内部类的特殊语法规则··········· 235 6.4.3 内部类是否有用、必要和 安全·········································· 236 6.4.4 局部内部类······························ 238 6.4.5 由外部方法访问 final 变量······ 239 6.4.6 匿名内部类······························ 241 6.4.7 静态内部类······························ 244 6.5 代理 ··············································· 247 第 7 章 图形程序设计··························· 253 7.1 Swing 概述 ···································· 253 7.2 创建框架········································ 257 7.3 框架定位········································ 259 7.3.1 框架属性·································· 261 7.3.2 确定合适的框架大小··············· 262 7.4 在组件中显示信息························ 265 7.5 处理 2D 图形································· 270 7.6 使用颜色········································ 277 7.7 文本使用特殊字体························ 280 7.8 显示图像········································ 287 第 8 章 事件处理 ··································· 291 8.1 事件处理基础································ 291 8.1.1 实例:处理按钮点击事件······· 293 8.1.2 建议使用内部类 ······················ 297 8.1.3 创建包含一个方法调用的 监听器······································ 299 8.1.4 实例:改变观感 ······················ 300 8.1.5 适配器类·································· 303 8.2 动作 ··············································· 306 8.3 鼠标事件········································ 312 8.4 AWT 事件继承层次······················· 318 第 9 章 Swing 用户界面组件··············· 322 9.1 Swing 和模型 – 视图 – 控制器 设计模式········································ 322 9.1.1 设计模式·································· 322 9.1.2 模型 – 视图 – 控制器模式······· 323 9.1.3 Swing 按钮的模型 – 视图 – 控制器分析······························ 326 9.2 布局管理概述································ 327 9.2.1 边框布局·································· 329 9.2.2 网格布局·································· 331 9.3 文本输入········································ 334 9.3.1 文本域······································ 334 9.3.2 标签和标签组件 ······················ 336 9.3.3 密码域······································ 337 9.3.4 文本区······································ 338 9.3.5 滚动窗格·································· 338 9.4 选择组件········································ 340 9.4.1 复选框······································ 340 9.4.2 单选按钮·································· 342 9.4.3 边框·········································· 345 9.4.4 组合框······································ 349 9.4.5 滑动条······································ 352 9.5 菜单 ··············································· 357 9.5.1 菜单创建·································· 357 9.5.2 菜单项中的图标 ······················ 359 9.5.3 复选框和单选按钮菜单项······· 360 9.5.4 弹出菜单·································· 361 9.5.5 快捷键和加速器 ······················ 362 9.5.6 启用和禁用菜单项 ·················· 364 9.5.7 工具栏······································ 368 9.5.8 工具提示·································· 369 9.6 复杂的布局管理···························· 370 9.6.1 网格组布局······························ 372 9.6.2 组布局······································ 380 9.6.3 不使用布局管理器 ·················· 388 9.6.4 定制布局管理器 ······················ 388 9.6.5 遍历顺序·································· 392 9.7 对话框············································ 393 9.7.1 选项对话框······························ 393 9.7.2 创建对话框······························ 402 9.7.3 数据交换·································· 406 9.7.4 文件对话框······························ 411 9.7.5 颜色选择器······························ 421 第 10 章 部署应用程序和 applet········· 426 10.1 JAR 文件······································ 426 10.1.1 清单文件································ 427 10.1.2 可运行 JAR 文件 ··················· 428 10.1.3 资源········································ 429 10.1.4 密封········································ 431 10.2 Java Web Start······························ 432 10.2.1 沙箱········································ 435 10.2.2 签名代码································ 436 10.2.3 JNLP API································ 438 10.3 applet············································ 445 10.3.1 一个简单的 applet·················· 445 10.3.2 applet 的 HTML 标记和属性··· 448 10.3.3 object 标记 ····························· 451 10.3.4 使用参数向 applet 传递信息 ··· 451 10.3.5 访问图像和音频文件············· 456 10.3.6 applet 上下文 ························· 457 10.4 应用程序首选项存储 ·················· 460 10.4.1 属性映射································ 460 10.4.2 Preferences API······················ 464 第 11 章 异常、断言、日志和调试 ··· 471 11.1 处理错误······································ 471 11.1.1 异常分类································· 473 11.1.2 声明已检查异常 ···················· 474 11.1.3 如何抛出异常 ························ 476 11.1.4 创建异常类 ···························· 477 11.2 捕获异常······································ 478 11.2.1 捕获多个异常 ························ 480 11.2.2 再次抛出异常与异常链········· 481 11.2.3 finally 子句····························· 482 11.2.4 带资源的 try 语句·················· 486 11.2.5 分析堆栈跟踪元素················· 487 11.3 使用异常机制的技巧 ·················· 490 11.4 使用断言······································ 492 11.4.1 启用和禁用断言 ···················· 493 11.4.2 使用断言完成参数检查········· 494 11.4.3 为文档假设使用断言············· 495 11.5 记录日志······································ 496 11.5.1 基本日志································ 496 11.5.2 高级日志································ 497 11.5.3 修改日志管理器配置············· 499 11.5.4 本地化···································· 500 11.5.5 处理器···································· 500 11.5.6 过滤器···································· 503 11.5.7 格式化器································ 504 11.5.8 日志记录说明 ························ 504 11.6 调试技巧······································ 512 11.7 GUI 程序排错技巧······················ 516 11.8 使用调试器·································· 523 第 12 章 泛型程序设计························· 527 12.1 为什么要使用泛型程序设计······· 527 12.2 定义简单泛型类·························· 529 12.3 泛型方法······································ 531 12.4 类型变量的限定·························· 532 12.5 泛型代码和虚拟机······················ 534 12.5.1 翻译泛型表达式 ···················· 535 12.5.2 翻译泛型方法 ························ 536 12.5.3 调用遗留代码 ························ 537 12.6 约束与局限性······························ 538 12.6.1 不能用基本类型实例化 类型参数································ 538 12.6.2 运行时类型查询只适用 于原始类型···························· 539 12.6.3 不能创建参数化类型的 数组········································ 539 12.6.4 Varargs 警告··························· 540 12.6.5 不能实例化类型变量············· 541 12.6.6 泛型类的静态上下文中 类型变量无效 ························ 542 12.6.7 不能抛出或捕获泛型类 的实例···································· 543 12.6.8 注意擦除后的冲突 ················ 545 12.7 泛型类型的继承规则 ·················· 546 12.8 通配符类型·································· 547 12.8.1 通配符的超类型限定············· 549 12.8.2 无限定通配符 ························ 551 12.8.3 通配符捕获···························· 551 12.9 反射和泛型·································· 553 12.9.1 使用 Class 参数进行 类型匹配································ 554 12.9.2 虚拟机中的泛型类型信息····· 555 第 13 章 集合·········································· 560 13.1 集合接口······································ 560 13.1.1 将集合的接口与实现分离····· 560 13.1.2 Java 类库中的集合接口和 迭代器接口···························· 562 13.2 具体的集合·································· 567 13.2.1 链表········································ 568 13.2.2 数组列表································ 576 13.2.3 散列集···································· 576 13.2.4 树集········································ 579 13.2.5 对象的比较···························· 580 13.2.6 队列与双端队列 ···················· 585 13.2.7 优先级队列···························· 586 13.2.8 映射表···································· 587 13.2.9 专用集与映射表类 ················ 591 13.3 集合框架······································ 595 13.3.1 视图与包装器 ························ 598 13.3.2 批操作···································· 604 13.3.3 集合与数组之间的转换········· 605 13.4 算法 ············································· 606 13.4.1 排序与混排···························· 607 13.4.2 二分查找································ 609 13.4.3 简单算法································ 610 13.4.4 编写自己的算法 ···················· 612 13.5 遗留的集合·································· 613 13.5.1 Hashtable 类··························· 613 13.5.2 枚举········································ 613 13.5.3 属性映射表···························· 614 13.5.4 栈············································ 615 13.5.5 位集········································ 615 第 14 章 多线程 ····································· 620 14.1 什么是线程·································· 620 14.2 中断线程······································ 630 14.3 线程状态······································ 633 14.3.1 新创建线程···························· 633 14.3.2 可运行线程···························· 633 14.3.3 被阻塞线程和等待线程········· 634 14.3.4 被终止的线程 ························ 634 14.4 线程属性······································ 636 14.4.1 线程优先级···························· 636 14.4.2 守护线程································ 637 14.4.3 未捕获异常处理器 ················ 637 14.5 同步 ············································· 638 14.5.1 竞争条件的一个例子············· 638 14.5.2 竞争条件详解 ························ 642 14.5.3 锁对象···································· 644 14.5.4 条件对象································ 647 14.5.5 synchronized 关键字·············· 651 14.5.6 同步阻塞································ 655 14.5.7 监视器概念···························· 656 14.5.8 Volatile 域······························· 657 14.5.9 final 变量································ 658 14.5.10 原子性·································· 658 14.5.11 死锁······································ 658 14.5.12 线程局部变量 ······················ 660 14.5.13 锁测试与超时 ······················ 661 14.5.14 读 / 写锁······························· 663 14.5.15 为什么弃用 stop 和 suspend 方法 ························ 663 14.6 阻塞队列······································ 665 14.7 线程安全的集合·························· 672 14.7.1 高效的映射表、集合和队列··· 672 14.7.2 写数组的拷贝 ························ 674 14.7.3 较早的线程安全集合 ··········· 674 14.8 Callable 与 Future ························ 675 14.9 执行器·········································· 679 14.9.1 线程池···································· 680 14.9.2 预定执行································ 683 14.9.3 控制任务组···························· 684 14.9.4 Fork-Join 框架 ······················· 686 14.10 同步器········································ 688 14.10.1 信号量·································· 689 14.10.2 倒计时门栓·························· 689 14.10.3 障栅······································ 689 14.10.4 交换器··································· 690 14.10.5 同步队列······························ 690 14.11 线程与 Swing····························· 690 14.11.1 运行耗时的任务··················· 692 14.11.2 使用 Swing 工作线程 ·········· 696 14.11.3 单一线程规则 ······················ 701 附录 Java 关键字·································· 703 · 5 1.2.9 高性能·········································· 5 1.2.10 多线程········································ 6 1.2.11 动态性········································ 6 1.3 Java applet 与 Internet ······················· 6 1.4 Java 发展简史···································· 7 1.5 关于 Java 的常见误解····················· 10 第 2 章 Java 程序设计环境···················· 13 2.1 安装 Java 开发工具箱····················· 13 2.1.1 下载 JDK···································· 13 2.1.2 设置执行路径 ···························· 14 2.1.3 安装库源文件和文档················· 16 2.1.4 安装本书中的示例 ···················· 17 2.1.5 导航 Java 目录··························· 17 2.2 选择开发环境·································· 18 2.3 使用命令行工具······························ 18 2.4 使用集成开发环境·························· 20 2.5 运行图形化应用程序 ······················ 23 2.6 建立并运行 applet ··························· 25 第 3 章 Java 的基本程序设计结构 ······· 29 3.1 一个简单的 Java 应用程序 ············· 29 3.2 注释 ················································· 32 3.3 数据类型·········································· 33 3.3.1 整型············································ 33 3.3.2 浮点类型···································· 34 3.3.3 char 类型···································· 35 3.3.4 boolean 类型 ······························ 36 3.4 变量 ················································· 37 3.4.1 变量初始化································ 37 3.4.2 常量············································ 38 3.5 运算符·············································· 39 3.5.1 自增运算符与自减运算符········· 40 3.5.2 关系运算符与 boolean 运算符 ··· 40 3.5.3 位运算符···································· 41 3.5.4 数学函数与常量 ························ 42 3.5.5 数值类型之间的转换················· 43 3.5.6 强制类型转换 ···························· 43 3.5.7 括号与运算符级别 ···················· 44 3.5.8 枚举类型···································· 45 3.6 字符串·············································· 45 3.6.1 子串············································· 46 3.6.2 拼接············································ 46 3.6.3 不可变字符串 ···························· 46 3.6.4 检测字符串是否相等················· 47 3.6.5 空串与 Null 串··························· 48 3.6.6 代码点与代码单元 ···················· 49 3.6.7 字符串 API································· 49 3.6.8 阅读联机 API 文档···················· 51 3.6.9 构建字符串································ 53 3.7 输入输出·········································· 54 3.7.1 读取输入···································· 54 3.7.2 格式化输出································ 56 3.7.3 文件输入与输出 ························ 60 3.8 控制流程·········································· 61 3.8.1 块作用域···································· 62 3.8.2 条件语句···································· 62 3.8.3 循环············································ 65 3.8.4 确定循环 ·································· 68 3.8.5 多重选择:switch 语句············· 71 3.8.6 中断控制流程语句 ···················· 74 3.9 大数值·············································· 76 3.10 数组 ··············································· 78 3.10.1 for each 循环···························· 79 3.10.2 数组初始化以及匿名数组······· 80 3.10.3 数组拷贝·································· 80 3.10.4 命令行参数······························ 81 3.10.5 数组排序·································· 82 3.10.6 多维数组·································· 85 3.10.7 不规则数组······························ 87 第 4 章 对象与类 ····································· 91 4.1 面向对象程序设计概述 ·················· 91 4.1.1 类················································ 92 4.1.2 对象············································ 93 4.1.3 识别类········································ 93 4.1.4 类之间的关系 ··························· 94 4.2 使用预定义类·································· 95 4.2.1 对象与对象变量 ························ 95 4.2.2 Java 类库中的 GregorianCalendar 类 ································ 98 4.2.3 更改器方法与访问器方法······· 100 4.3 用户自定义类································ 106 4.3.1 Employee 类····························· 106 4.3.2 多个源文件的使用 ·················· 108 4.3.3 剖析 Employee 类···················· 109 4.3.4 从构造器开始 ·························· 110 4.3.5 隐式参数与显式参数················111 4.3.6 封装的优点······························ 112 4.3.7 基于类的访问权限 ·················· 114 4.3.8 私有方法·································· 114 4.3.9 final 实例域······························ 115 4.4 静态域与静态方法························ 115 4.4.1 静态域······································ 115 4.4.2 静态常量·································· 116 4.4.3 静态方法·································· 117 4.4.4 工厂方法·································· 118 4.4.5 main 方法································· 118 4.5 方法参数········································ 121 4.6 对象构造········································ 126 4.6.1 重载·········································· 126 4.6.2 默认域初始化 ·························· 126 4.6.3 无参数的构造器 ······················ 127 4.6.4 显式域初始化 ·························· 128 4.6.5 参数名······································ 129 4.6.6 调用另一个构造器 ·················· 129 4.6.7 初始化块·································· 130 4.6.8 对象析构与 finalize 方法········· 134 4.7 包 ··················································· 134 4.7.1 类的导入·································· 134 4.7.2 静态导入·································· 136 4.7.3 将类放入包中 ·························· 136 4.7.4 包作用域·································· 139 4.8 类路径············································ 140 4.9 文档注释········································ 143 4.9.1 注释的插入······························ 143 4.9.2 类注释······································ 144 4.9.3 方法注释·································· 144 4.9.4 域注释······································ 145 4.9.5 通用注释·································· 145 4.9.6 包与概述注释 ·························· 146 4.9.7 注释的抽取 ··························· 146 4.10 类设计技巧·································· 147 第 5 章 继承············································ 150 5.1 类、超类和子类···························· 150 5.1.1 继承层次·································· 156 5.1.2 多态·········································· 157 5.1.3 动态绑定·································· 158 5.1.4 阻止继承:final 类和方法······· 160 5.1.5 强制类型转换 ·························· 161 5.1.6 抽象类······································ 163 5.1.7 受保护访问······························ 168 5.2 Object:所有类的超类 ················· 169 5.2.1 equals 方法······························· 169 5.2.2 相等测试与继承 ······················ 171 5.2.3 hashCode 方法 ························· 173 5.2.4 toString 方法···························· 175 5.3 泛型数组列表································ 181 5.3.1 访问数组列表元素 ·················· 183 5.3.2 类型化与原始数组列表 的兼容性·································· 186 5.4 对象包装器与自动装箱 ················ 187 5.5 参数数量可变的方法 ···················· 189 5.6 枚举类············································ 191 5.7 反射 ··············································· 192 5.7.1 Class 类···································· 193 5.7.2 捕获异常·································· 195 5.7.3 利用反射分析类的能力··········· 196 5.7.4 在运行时使用反射分析对象··· 201 5.7.5 使用反射编写泛型数组代码··· 206 5.7.6 调用任意方法 ·························· 209 5.8 继承设计的技巧···························· 212 第 6 章 接口与内部类··························· 215 6.1 接口 ··············································· 215 6.1.1 接口的特性······························ 220 6.1.2 接口与抽象类 ·························· 222 6.2 对象克隆········································ 222 6.3 接口与回调···································· 228 6.4 内部类············································ 231 6.4.1 使用内部类访问对象状态······· 232 6.4.2 内部类的特殊语法规则··········· 235 6.4.3 内部类是否有用、必要和 安全·········································· 236 6.4.4 局部内部类······························ 238 6.4.5 由外部方法访问 final 变量······ 239 6.4.6 匿名内部类······························ 241 6.4.7 静态内部类······························ 244 6.5 代理 ··············································· 247 第 7 章 图形程序设计··························· 253 7.1 Swing 概述 ···································· 253 7.2 创建框架········································ 257 7.3 框架定位········································ 259 7.3.1 框架属性·································· 261 7.3.2 确定合适的框架大小··············· 262 7.4 在组件中显示信息························ 265 7.5 处理 2D 图形································· 270 7.6 使用颜色········································ 277 7.7 文本使用特殊字体························ 280 7.8 显示图像········································ 287 第 8 章 事件处理 ··································· 291 8.1 事件处理基础································ 291 8.1.1 实例:处理按钮点击事件······· 293 8.1.2 建议使用内部类 ······················ 297 8.1.3 创建包含一个方法调用的 监听器······································ 299 8.1.4 实例:改变观感 ······················ 300 8.1.5 适配器类·································· 303 8.2 动作 ··············································· 306 8.3 鼠标事件········································ 312 8.4 AWT 事件继承层次······················· 318 第 9 章 Swing 用户界面组件··············· 322 9.1 Swing 和模型 – 视图 – 控制器 设计模式········································ 322 9.1.1 设计模式·································· 322 9.1.2 模型 – 视图 – 控制器模式······· 323 9.1.3 Swing 按钮的模型 – 视图 – 控制器分析······························ 326 9.2 布局管理概述································ 327 9.2.1 边框布局·································· 329 9.2.2 网格布局·································· 331 9.3 文本输入········································ 334 9.3.1 文本域······································ 334 9.3.2 标签和标签组件 ······················ 336 9.3.3 密码域······································ 337 9.3.4 文本区······································ 338 9.3.5 滚动窗格·································· 338 9.4 选择组件········································ 340 9.4.1 复选框······································ 340 9.4.2 单选按钮·································· 342 9.4.3 边框·········································· 345 9.4.4 组合框······································ 349 9.4.5 滑动条······································ 352 9.5 菜单 ··············································· 357 9.5.1 菜单创建·································· 357 9.5.2 菜单项中的图标 ······················ 359 9.5.3 复选框和单选按钮菜单项······· 360 9.5.4 弹出菜单·································· 361 9.5.5 快捷键和加速器 ······················ 362 9.5.6 启用和禁用菜单项 ·················· 364 9.5.7 工具栏······································ 368 9.5.8 工具提示·································· 369 9.6 复杂的布局管理···························· 370 9.6.1 网格组布局······························ 372 9.6.2 组布局······································ 380 9.6.3 不使用布局管理器 ·················· 388 9.6.4 定制布局管理器 ······················ 388 9.6.5 遍历顺序·································· 392 9.7 对话框············································ 393 9.7.1 选项对话框······························ 393 9.7.2 创建对话框······························ 402 9.7.3 数据交换·································· 406 9.7.4 文件对话框······························ 411 9.7.5 颜色选择器······························ 421 第 10 章 部署应用程序和 applet········· 426 10.1 JAR 文件······································ 426 10.1.1 清单文件································ 427 10.1.2 可运行 JAR 文件 ··················· 428 10.1.3 资源········································ 429 10.1.4 密封········································ 431 10.2 Java Web Start······························ 432 10.2.1 沙箱········································ 435 10.2.2 签名代码································ 436 10.2.3 JNLP API································ 438 10.3 applet············································ 445 10.3.1 一个简单的 applet·················· 445 10.3.2 applet 的 HTML 标记和属性··· 448 10.3.3 object 标记 ····························· 451 10.3.4 使用参数向 applet 传递信息 ··· 451 10.3.5 访问图像和音频文件············· 456 10.3.6 applet 上下文 ························· 457 10.4 应用程序首选项存储 ·················· 460 10.4.1 属性映射································ 460 10.4.2 Preferences API······················ 464 第 11 章 异常、断言、日志和调试 ··· 471 11.1 处理错误······································ 471 11.1.1 异常分类································· 473 11.1.2 声明已检查异常 ···················· 474 11.1.3 如何抛出异常 ························ 476 11.1.4 创建异常类 ···························· 477 11.2 捕获异常······································ 478 11.2.1 捕获多个异常 ························ 480 11.2.2 再次抛出异常与异常链········· 481 11.2.3 finally 子句····························· 482 11.2.4 带资源的 try 语句·················· 486 11.2.5 分析堆栈跟踪元素················· 487 11.3 使用异常机制的技巧 ·················· 490 11.4 使用断言······································ 492 11.4.1 启用和禁用断言 ···················· 493 11.4.2 使用断言完成参数检查········· 494 11.4.3 为文档假设使用断言············· 495 11.5 记录日志······································ 496 11.5.1 基本日志································ 496 11.5.2 高级日志································ 497 11.5.3 修改日志管理器配置············· 499 11.5.4 本地化···································· 500 11.5.5 处理器···································· 500 11.5.6 过滤器···································· 503 11.5.7 格式化器································ 504 11.5.8 日志记录说明 ························ 504 11.6 调试技巧······································ 512 11.7 GUI 程序排错技巧······················ 516 11.8 使用调试器·································· 523 第 12 章 泛型程序设计························· 527 12.1 为什么要使用泛型程序设计······· 527 12.2 定义简单泛型类·························· 529 12.3 泛型方法······································ 531 12.4 类型变量的限定·························· 532 12.5 泛型代码和虚拟机······················ 534 12.5.1 翻译泛型表达式 ···················· 535 12.5.2 翻译泛型方法 ························ 536 12.5.3 调用遗留代码 ························ 537 12.6 约束与局限性······························ 538 12.6.1 不能用基本类型实例化 类型参数································ 538 12.6.2 运行时类型查询只适用 于原始类型···························· 539 12.6.3 不能创建参数化类型的 数组········································ 539 12.6.4 Varargs 警告··························· 540 12.6.5 不能实例化类型变量············· 541 12.6.6 泛型类的静态上下文中 类型变量无效 ························ 542 12.6.7 不能抛出或捕获泛型类 的实例···································· 543 12.6.8 注意擦除后的冲突 ················ 545 12.7 泛型类型的继承规则 ·················· 546 12.8 通配符类型·································· 547 12.8.1 通配符的超类型限定············· 549 12.8.2 无限定通配符 ························ 551 12.8.3 通配符捕获···························· 551 12.9 反射和泛型·································· 553 12.9.1 使用 Class 参数进行 类型匹配································ 554 12.9.2 虚拟机中的泛型类型信息····· 555 第 13 章 集合·········································· 560 13.1 集合接口······································ 560 13.1.1 将集合的接口与实现分离····· 560 13.1.2 Java 类库中的集合接口和 迭代器接口···························· 562 13.2 具体的集合·································· 567 13.2.1 链表········································ 568 13.2.2 数组列表································ 576 13.2.3 散列集···································· 576 13.2.4 树集········································ 579 13.2.5 对象的比较···························· 580 13.2.6 队列与双端队列 ···················· 585 13.2.7 优先级队列···························· 586 13.2.8 映射表···································· 587 13.2.9 专用集与映射表类 ················ 591 13.3 集合框架······································ 595 13.3.1 视图与包装器 ························ 598 13.3.2 批操作···································· 604 13.3.3 集合与数组之间的转换········· 605 13.4 算法 ············································· 606 13.4.1 排序与混排···························· 607 13.4.2 二分查找································ 609 13.4.3 简单算法································ 610 13.4.4 编写自己的算法 ···················· 612 13.5 遗留的集合·································· 613 13.5.1 Hashtable 类··························· 613 13.5.2 枚举········································ 613 13.5.3 属性映射表···························· 614 13.5.4 栈············································ 615 13.5.5 位集········································ 615 第 14 章 多线程 ····································· 620 14.1 什么是线程·································· 620 14.2 中断线程······································ 630 14.3 线程状态······································ 633 14.3.1 新创建线程···························· 633 14.3.2 可运行线程···························· 633 14.3.3 被阻塞线程和等待线程········· 634 14.3.4 被终止的线程 ························ 634 14.4 线程属性······································ 636 14.4.1 线程优先级···························· 636 14.4.2 守护线程································ 637 14.4.3 未捕获异常处理器 ················ 637 14.5 同步 ············································· 638 14.5.1 竞争条件的一个例子············· 638 14.5.2 竞争条件详解 ························ 642 14.5.3 锁对象···································· 644 14.5.4 条件对象································ 647 14.5.5 synchronized 关键字·············· 651 14.5.6 同步阻塞································ 655 14.5.7 监视器概念···························· 656 14.5.8 Volatile 域······························· 657 14.5.9 final 变量································ 658 14.5.10 原子性·································· 658 14.5.11 死锁······································ 658 14.5.12 线程局部变量 ······················ 660 14.5.13 锁测试与超时 ······················ 661 14.5.14 读 / 写锁······························· 663 14.5.15 为什么弃用 stop 和 suspend 方法 ························ 663 14.6 阻塞队列······································ 665 14.7 线程安全的集合·························· 672 14.7.1 高效的映射表、集合和队列··· 672 14.7.2 写数组的拷贝 ························ 674 14.7.3 较早的线程安全集合 ··········· 674 14.8 Callable 与 Future ························ 675 14.9 执行器·········································· 679 14.9.1 线程池···································· 680 14.9.2 预定执行································ 683 14.9.3 控制任务组···························· 684 14.9.4 Fork-Join 框架 ······················· 686 14.10 同步器········································ 688 14.10.1 信号量·································· 689 14.10.2 倒计时门栓·························· 689 14.10.3 障栅······································ 689 14.10.4 交换器··································· 690 14.10.5 同步队列······························ 690 14.11 线程与 Swing····························· 690 14.11.1 运行耗时的任务··················· 692 14.11.2 使用 Swing 工作线程 ·········· 696 14.11.3 单一线程规则 ······················ 701 附录 Java 关键字·································· 703
暂无评论