Java Swing入门与实践指南

4星 · 超过85%的资源 需积分: 0 21 下载量 27 浏览量 更新于2024-07-28 1 收藏 1.37MB PDF 举报
"Java Swing教程是一本介绍Java Swing使用和开发的入门教材,具有一定的深度,包含大量示例代码供读者参考学习。作者是David M. Geary,由李建森、蒋欣军、龚尧莞等人翻译,由机械工业出版社出版。书中还提供了运行环境和开发环境的下载链接,以便读者进行程序演示和开发。内容涵盖Swing的基础知识,如Swing历史、轻量组件与重量组件的对比、Swing组件体系结构,以及Swing的基本用法,包括小应用程序与应用程序的区别、如何混合使用Swing和AWT组件、Swing组件的模型-视图-控制器架构等。" Java Swing是Java Foundation Classes (JFC) 的一部分,主要用于构建桌面应用的用户界面。它提供了一系列的组件,如按钮、文本框、列表、表格等,这些组件比AWT(Abstract Window Toolkit)的组件更具有现代感和可定制性,同时Swing组件是完全由Java实现的,因此具有更好的跨平台兼容性。 1. **Swing的历史**:Swing是在Java 1.2版本中引入的,作为AWT的增强版,提供了丰富的用户界面元素和更灵活的设计。 2. **轻量组件与重量组件**:轻量组件(如JButton)基于Java对象,而重量组件(如AWT的Button)则依赖于操作系统的原生窗口系统。Swing组件主要是轻量级的,这使得它们在性能和移植性上优于AWT组件。 3. **Swing组件**:Swing不仅提供了AWT的替代组件,如JButton代替Button,还增加了许多新的组件,如JTable、JTree、JTabbedPane等。 4. **J组件**:所有Swing组件都以"J"开头,如JFrame、JLabel,这些组件通常用于构建GUI。 5. **Swing包概览**:主要包含javax.swing.* 包,其中包含各种组件、布局管理器、事件处理等。 6. **Swing与AWT**:虽然Swing组件可以与AWT组件混用,但Swing设计上更加面向对象,使用模型-视图-控制器(MVC)架构,并且支持事件模型,使得编程更加简洁。 7. **Swing组件的体系结构**:Swing组件基于MVC模式,模型负责数据,视图显示数据,控制器处理用户交互。UI代表(UI Delegate)允许自定义组件的外观和行为。 8. **Swing与线程**:由于Swing组件是单线程设计,所有对组件的操作必须在Event Dispatch Thread(EDT)上执行,以避免线程安全问题。SwingUtilities的invokeLater和invokeAndWait方法用于在EDT上调度任务。 通过本书,读者可以学习到如何创建Swing应用程序,如何设计用户界面,以及如何处理用户交互,进一步提升Java桌面应用开发技能。同时,提供的实例代码可以帮助读者更好地理解和实践Swing的使用。
2019-10-30 上传
Java 图形界面开发简介 .............. ................................ ................................ ..... 5 1. Swing1. Swing1. Swing 1. Swing1. Swing1. Swing1. Swing简介 ................................ ................................ ................................ ................................ ............... 5 2. Swing2. Swing2. Swing 2. Swing2. Swing2. Swing2. Swing组件 ................................ ................................ ................................ ................................ ............... 5 3. 3. 3. 布局管理器 布局管理器 ................................ ................................ ................................ ................................ ............. 8 4. 4. 4. 代码实例 代码实例 : 一个简单的窗口程序 一个简单的窗口程序 一个简单的窗口程序 一个简单的窗口程序 一个简单的窗口程序 ................................ ................................ ................................ ..... 9 1.1: FlowLayo1.1: FlowLayo1.1: FlowLayo1.1: FlowLayo1.1: FlowLayo 1.1: FlowLayo 1.1: FlowLayout (流式布局) (流式布局) (流式布局) (流式布局) ................................ ................................ ................................ ...................... 10 1. 概述 ................................ ................................ ................................ ................................ ....................... 10 2. 代码实例 ................................ ................................ ................................ ................................ .............. 11 1.2: GridLayout(网格布局) (网格布局) (网格布局) (网格布局) ................................ ................................ ................................ ..................... 13 1. 概述 ................................ ................................ ................................ ................................ ....................... 13 2. 代码演示 ................................ ................................ ................................ ................................ .............. 14 1.3: GridBagLayout1.3: GridBagLayout1.3: GridBagLayout1.3: GridBagLayout1.3: GridBagLayout 1.3: GridBagLayout 1.3: GridBagLayout1.3: GridBagLayout 1.3: GridBagLayout1.3: GridBagLayout 1.3: GridBagLayout1.3: GridBagLayout(网格袋布局) (网格袋布局) (网格袋布局) (网格袋布局) ................................ ................................ ................................ ........... 17 1. 布局 : GridBagLayout ................................ ................................ ................................ ...................... 17 2. 约束 : GridBagConstraints ................................ ................................ ................................ ............. 17 3. 属性 : GridBagConstraints 的属性 ................................ ................................ ............................. 18 4. 案例 : GridBagLayout使用实例 使用实例 ................................ ................................ ................................ ... 19 1.4: BoxLayout1.4: BoxLayout1.4: BoxLayout1.4: BoxLayout1.4: BoxLayout 1.4: BoxLayout1.4: BoxLayout1.4: BoxLayout 1.4: BoxLayout1.4: BoxLayout(箱式布局) (箱式布局) (箱式布局) (箱式布局) ................................ ................................ ................................ ........................ 25 1. 概述 ................................ ................................ ................................ ................................ ....................... 25 2. 代码实例 ................................ ................................ ................................ ................................ .............. 27 1.5: GroupLayout(分组布局) (分组布局) (分组布局) ................................ ................................ ................................ ................. 29 1. 概述 ................................ ................................ ................................ ................................ ....................... 29 2. 代码实例 ................................ ................................ ................................ ................................ .............. 31 1.6: CardLayout(卡片布局) (卡片布局) (卡片布局) ................................ ................................ ................................ .................... 36 1. 概述 ................................ ................................ ................................ ................................ ....................... 36 2. 代码实例 ................................ ................................ ................................ ................................ .............. 37 1.7: BorderLayout(边界布局) (边界布局) (边界布局) ................................ ................................ ................................ ................ 40 1. 概述 ................................ ................................ ................................ ................................ ....................... 40 2. 代码实例 ................................ ................................ ................................ ................................ .............. 41 1.8: SpringLayout(弹性布局) (弹性布局) (弹性布局) ................................ ................................ ................................ ................ 43 1. 概述 ................................ ................................ ................................ ................................ ....................... 43 2. 代码实例 ................................ ................................ ................................ ................................ .............. 47 1.9: null(绝对布局) (绝对布局) (绝对布局) ................................ ................................ ................................ ................................ .... 52 1. 概述 ................................ ................................ ................................ ................................ ....................... 52 2. 代码实例 ................................ ................................ ................................ ................................ .............. 53 2.1: JLabel(标签) (标签) (标签) ................................ ................................ ................................ ................................ ....... 56 1. 概述 ................................ ................................ ................................ ................................ ....................... 56 2. 代码实例 ................................ ................................ ................................ ................................ .............. 61 2.2: JButton(按钮) (按钮) (按钮) ................................ ................................ ................................ ................................ ..... 64 1. 概述 ................................ ................................ ................................ ................................ ....................... 64 2. 代码实例 : 默认按钮 默认按钮 ................................ ................................ ................................ ........................ 66 3. 代码实例 : 自定义图片按钮 自定义图片按钮 自定义图片按钮 自定义图片按钮 ................................ ................................ ................................ .......... 68 2.3: JRadioButton(单选按钮) (单选按钮) (单选按钮) ................................ ................................ ................................ ................ 71 2 1. 概述 ................................ ................................ ................................ ................................ ....................... 71 2. 代码实例 ................................ ................................ ................................ ................................ .............. 73 2.4: JCheckBox(复选框) (复选框) (复选框) ................................ ................................ ................................ ......................... 75 1. 概述 ................................ ................................ ................................ ................................ ....................... 75 2. 代码实例 ................................ ................................ ................................ ................................ .............. 77 2.5: JToggleButton(开关按钮) (开关按钮) (开关按钮) ................................ ................................ ................................ .............. 80 1. 概述 ................................ ................................ ................................ ................................ ....................... 80 2. 代码实例 : 默认 的开关按钮 的开关按钮 的开关按钮 ................................ ................................ ................................ .......... 82 3. 代码实例 : 自定义图片开关 自定义图片开关 自定义图片开关 自定义图片开关 ................................ ................................ ................................ .......... 84 2.6: JTextField(文本框) (文本框) (文本框) ................................ ................................ ................................ ........................... 87 1. 概述 ................................ ................................ ................................ ................................ ....................... 87 2. 实例代码 ................................ ................................ ................................ ................................ .............. 91 2.7:PasswordField(密码框) (密码框) (密码框) ................................ ................................ ................................ ................... 93 1. 概述 ................................ ................................ ................................ ................................ ....................... 93 2. 代码实例 ................................ ................................ ................................ ................................ .............. 96 2.8: JTextArea(文本区域) (文本区域) (文本区域) (文本区域) ................................ ................................ ................................ ....................... 98 1. 概述 ................................ ................................ ................................ ................................ ....................... 98 2. 代码实例 ................................ ................................ ................................ ................................ ............ 103 2.9: JComboBox(下拉列表框) (下拉列表框) (下拉列表框) ................................ ................................ ................................ ............ 105 1. 概述 ................................ ................................ ................................ ................................ ..................... 105 2. 代码实例 ................................ ................................ ................................ ................................ ............ 107 2.10: JList(列 表框) 表框) ................................ ................................ ................................ ................................ .. 110 1. 概述 ................................ ................................ ................................ ................................ ..................... 110 2. 代码实例 ................................ ................................ ................................ ................................ ............ 113 2.11: JProgressBar(进度条) (进度条) (进度条) ................................ ................................ ................................ ................ 117 1. 概述 ................................ ................................ ................................ ................................ ..................... 117 2. 代码实例 ................................ ................................ ................................ ................................ ............ 119 2.12: JSlider(滑块) (滑块) (滑块) ................................ ................................ ................................ ................................ .. 123 1. 概述 ................................ ................................ ................................ ................................ ..................... 123 2. 代码实例 : 默认刻度值 默认刻度值 默认刻度值 ................................ ................................ ................................ .................. 126 3. 代码实例 : 自定义标签刻度值 自定义标签刻度值 自定义标签刻度值 自定义标签刻度值 ................................ ................................ ................................ .... 128 3.1: JPanel(面板) (面板) ................................ ................................ ................................ ................................ ..... 132 1. 概述 ................................ ................................ ................................ ................................ ..................... 132 2. 代码实例 ................................ ................................ ................................ ................................ ............ 133 3.2: JScrollPane(滚动面板) (滚动面板) (滚动面板) (滚动面板) ................................ ................................ ................................ ................. 135 1. 概述 ................................ ................................ ................................ ................................ ..................... 135 2. 代码实例 ................................ ................................ ................................ ................................ ............ 138 3.2: JScrollPane(滚动面板) (滚动面板) (滚动面板) (滚动面板) ................................ ................................ ................................ ................. 140 1. 概述 ................................ ................................ ................................ ................................ ..................... 140 2. 代码实例 ................................ ................................ ................................ ................................ ............ 142 3.4: JTabbedPane(选项卡面板) (选项卡面板) (选项卡面板) (选项卡面板) ................................ ................................ ................................ ......... 145 1. 概述 ................................ ................................ ................................ ................................ ..................... 145 2. 代码实例 ................................ ................................ ................................ ................................ ............ 149 3.5: JLayeredPane(层级面板) (层级面板) (层级面板) ................................ ................................ ................................ ............