SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
北京圣思园科技有限公司
http://www.shengsiyuan.com
本讲内容:
关于Swing
Swing组件和容器
1. Swing:
• 是第二代GUI开发工具
• 它建立在AWT之上,但用新版本的组件替
代了旧版本的组件。
• 它提供了许多新的组件和相关的API
2. The Swing API 发布在:
– JDK 1.2
– JFC 1.1 (for use with JDK 1.1)
3. The Swing API 功能是强大的,灵活的和广
泛的。例如 JFC 1.1 有 15 public
packages 。
我们经常用的有两个包:
– javax.swing
– javax.swing.event
注:AWT components 在 java.awt package 中
1 概览
2 Swing Components 分类
3 顶层容器
4 JFrame
5 JDialog
6 JPanel
7 创建菜单
8 JComponent Class
SwingApplication 创建了四个常用的 Swing components:
• a frame, or main window (JFrame)
• a panel (JPanel)
• a button (JButton)
• a label (JLabel)
1. 概览:
容器与组件包含继承关系图表:
JFC
AWT
Component
s
Button
Frame
ScrollBar
…
Window
Font Color
Dialog Frame
Graphics
AWT
Event
Tool Kit
Your Application
Swing
AWT
Java 2D
Drag and Drop
Accessibility
JButton
JMenultem
JToggleButton
JCheckBoxMenultem
JMenu
JRadioButtonMnultem
JCheckBox
JRadioButton
JComboBox
JLabel
JPanel
JPopupMenu
JScrollBar
JComponent
AbstractButton
JList
JMenuBar
JScrollPane
JTextComponent
JEditorPane
JTextArea
JTextField
JTextPane
JPasswordField
JComponent
JColorChooser
JFileChooser
JInternalFrame
JProgressBar
JOptionPane
JRootPane
JSeparator
JLayeredPane
JToolTip
JToolBar
JTable
JTabbedPane
JSplitPane
JSlider
JViewport
JTree
JDesktopPane
JIternalFrame.JDesktopIcon
JPopupMenuSeparator
JToolBarSeparator
2 Swing Components 分类:
• 顶层容器
• 中间容器
• 原子组件
顶层容器
java 提供了三个顶层容器:JFrame, JDialog, 和
JApplet。
顶层容器
特点:
• 显示在屏幕上的每个组件都必须在一个包含继
承中。每一个包含继承都有一个顶层容器作为
它的根。
• 每一个顶层容器都有一个content pane ,它
包含了顶层容器中的所有组件
• 菜单在顶层容器中,但在content pane 之外
。
顶层容器
包含继承图如下:
顶层容器
1. 在Content Pane 中添加组件
有两种方式:
• topLevelContainer.getContentPane().add(yellowLabel,
BorderLayout.CENTER);
或:
• JPanel contentPane = new JPanel();
… …
topLevelContainer.setContentPane(contentPane);
顶层容器
2. 添加菜单条
• JMenuBar cyanMenuBar = new JMenuBar();
• frame.setJMenuBar(cyanMenuBar);
JFrame
是一个带有标题和可变大小边框的窗口。
任何一个图形界面应用程序都必须至少有一个
JFrame.
参见程序:
FrameDemo.java
TopLevelDemo.java
panel
• 为其它组件提供一个容器。便于组织和显
示组件。
• 还可以包含子panel
参见程序:MyPanel.java
创建菜单:
步骤:
1.创建一个 MenuBar 对象, 将其加入一个菜单容器,
例如: Frame .
2.创建 一个或多个Menu 对象,将其加入menu bar
对象.
3.创建 一个或多个MenuItem objects,对象,将其加
入相应的menu object.
参见程序:MenuTest.java
JComponent Class
除了顶层容器,Swing中所有组件(中间容器和
原子组件)都从JComponent类中继承。
例如: JPanel, JScrollPane, JButton, JTable( 以
J开头的组件类)。
Component
Container
JComponent
JComponent Class
JComponent Class API:
它从Component and Container 继承了许多方法,同时
也提供了一些新的方法。它为它的继承者提供了如下
常用功能:
• Customizing Component Appearance
• Setting Component State
• Handling Events
• Painting Components
• Dealing with the Containment Hierarchy
• Laying Out Components
• Getting Size and Position Information
• Specifying Absolute Size and Position
• BorderWindow.java
• FlowWindow.java
• GridWindow.java
• MultiListener.java
Color类
 Color类将颜色按照sRGB标准格式进行封装,该格
式中红、绿、蓝三原色的取值范围都是0~255。
 Color类定义了多个构造方法,常用的有:
public Color(int r, int g, int b)
public Color(int r, int g, int b, int a) // a -- 透明度参数
Color c = new Color(200, 170, 90);
Color d = new Color(200, 170, 90, 120);
 在GUI设计中使用Color类
Button b = new Button(“Test”);
Color c = new Color(200, 170, 90);
b.setBackground(c)

Más contenido relacionado

Destacado

Hive User Meeting August 2009 Facebook
Hive User Meeting August 2009 FacebookHive User Meeting August 2009 Facebook
Hive User Meeting August 2009 Facebookragho
 
Analytical Queries with Hive: SQL Windowing and Table Functions
Analytical Queries with Hive: SQL Windowing and Table FunctionsAnalytical Queries with Hive: SQL Windowing and Table Functions
Analytical Queries with Hive: SQL Windowing and Table FunctionsDataWorks Summit
 
Hadoop Real Life Use Case & MapReduce Details
Hadoop Real Life Use Case & MapReduce DetailsHadoop Real Life Use Case & MapReduce Details
Hadoop Real Life Use Case & MapReduce DetailsAnju Singh
 
Facebooks Petabyte Scale Data Warehouse using Hive and Hadoop
Facebooks Petabyte Scale Data Warehouse using Hive and HadoopFacebooks Petabyte Scale Data Warehouse using Hive and Hadoop
Facebooks Petabyte Scale Data Warehouse using Hive and Hadooproyans
 
Hadoop MapReduce Fundamentals
Hadoop MapReduce FundamentalsHadoop MapReduce Fundamentals
Hadoop MapReduce FundamentalsLynn Langit
 
Hive Quick Start Tutorial
Hive Quick Start TutorialHive Quick Start Tutorial
Hive Quick Start TutorialCarl Steinbach
 
Hadoop project design and a usecase
Hadoop project design and  a usecaseHadoop project design and  a usecase
Hadoop project design and a usecasesudhakara st
 

Destacado (7)

Hive User Meeting August 2009 Facebook
Hive User Meeting August 2009 FacebookHive User Meeting August 2009 Facebook
Hive User Meeting August 2009 Facebook
 
Analytical Queries with Hive: SQL Windowing and Table Functions
Analytical Queries with Hive: SQL Windowing and Table FunctionsAnalytical Queries with Hive: SQL Windowing and Table Functions
Analytical Queries with Hive: SQL Windowing and Table Functions
 
Hadoop Real Life Use Case & MapReduce Details
Hadoop Real Life Use Case & MapReduce DetailsHadoop Real Life Use Case & MapReduce Details
Hadoop Real Life Use Case & MapReduce Details
 
Facebooks Petabyte Scale Data Warehouse using Hive and Hadoop
Facebooks Petabyte Scale Data Warehouse using Hive and HadoopFacebooks Petabyte Scale Data Warehouse using Hive and Hadoop
Facebooks Petabyte Scale Data Warehouse using Hive and Hadoop
 
Hadoop MapReduce Fundamentals
Hadoop MapReduce FundamentalsHadoop MapReduce Fundamentals
Hadoop MapReduce Fundamentals
 
Hive Quick Start Tutorial
Hive Quick Start TutorialHive Quick Start Tutorial
Hive Quick Start Tutorial
 
Hadoop project design and a usecase
Hadoop project design and  a usecaseHadoop project design and  a usecase
Hadoop project design and a usecase
 

Similar a Swing

Ryu Learning Guide
Ryu Learning GuideRyu Learning Guide
Ryu Learning Guide呈 李
 
2. java introduction
2. java introduction2. java introduction
2. java introductionnetdbncku
 
Workflow Overview
Workflow OverviewWorkflow Overview
Workflow OverviewKevin Cao
 
Java 1(Java概述)
Java 1(Java概述)Java 1(Java概述)
Java 1(Java概述)xzdbd
 
Kubernetes device plugins
Kubernetes device pluginsKubernetes device plugins
Kubernetes device pluginsssuser75c76a2
 
Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)Will Huang
 
Sdn 之旅 open suse_asia_summit_20151206
Sdn 之旅 open suse_asia_summit_20151206Sdn 之旅 open suse_asia_summit_20151206
Sdn 之旅 open suse_asia_summit_20151206Fei Ji Siao
 
啟動 Laravel 與環境設定
啟動 Laravel 與環境設定啟動 Laravel 與環境設定
啟動 Laravel 與環境設定Shengyou Fan
 
Foundation of software development 1
Foundation of software development 1Foundation of software development 1
Foundation of software development 1netdbncku
 
UniArgus video box (UniArgus nvb)軟體產品簡介與一次性軟體使用授權
UniArgus video box (UniArgus nvb)軟體產品簡介與一次性軟體使用授權UniArgus video box (UniArgus nvb)軟體產品簡介與一次性軟體使用授權
UniArgus video box (UniArgus nvb)軟體產品簡介與一次性軟體使用授權HermesDDS
 
Keyboard Extension sharing
Keyboard Extension sharingKeyboard Extension sharing
Keyboard Extension sharingGrady Zhuo
 
自动化运维管理
自动化运维管理自动化运维管理
自动化运维管理frankwsj
 
Continuous Delivery: automated testing, continuous integration and continuous...
Continuous Delivery: automated testing, continuous integration and continuous...Continuous Delivery: automated testing, continuous integration and continuous...
Continuous Delivery: automated testing, continuous integration and continuous...Jimmy Lai
 
Uc gui中文手册
Uc gui中文手册Uc gui中文手册
Uc gui中文手册호연 최
 
Continuous integration
Continuous integrationContinuous integration
Continuous integrationnetdbncku
 
探奇部落格文章彙整
探奇部落格文章彙整探奇部落格文章彙整
探奇部落格文章彙整信仁 邱
 
Java2新觀念教本投影片ch1
Java2新觀念教本投影片ch1Java2新觀念教本投影片ch1
Java2新觀念教本投影片ch15045033
 

Similar a Swing (20)

Swagger
SwaggerSwagger
Swagger
 
Ryu Learning Guide
Ryu Learning GuideRyu Learning Guide
Ryu Learning Guide
 
2. java introduction
2. java introduction2. java introduction
2. java introduction
 
Workflow Overview
Workflow OverviewWorkflow Overview
Workflow Overview
 
Java 1(Java概述)
Java 1(Java概述)Java 1(Java概述)
Java 1(Java概述)
 
Kubernetes device plugins
Kubernetes device pluginsKubernetes device plugins
Kubernetes device plugins
 
Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)
 
Sdn 之旅 open suse_asia_summit_20151206
Sdn 之旅 open suse_asia_summit_20151206Sdn 之旅 open suse_asia_summit_20151206
Sdn 之旅 open suse_asia_summit_20151206
 
啟動 Laravel 與環境設定
啟動 Laravel 與環境設定啟動 Laravel 與環境設定
啟動 Laravel 與環境設定
 
Foundation of software development 1
Foundation of software development 1Foundation of software development 1
Foundation of software development 1
 
UniArgus video box (UniArgus nvb)軟體產品簡介與一次性軟體使用授權
UniArgus video box (UniArgus nvb)軟體產品簡介與一次性軟體使用授權UniArgus video box (UniArgus nvb)軟體產品簡介與一次性軟體使用授權
UniArgus video box (UniArgus nvb)軟體產品簡介與一次性軟體使用授權
 
Keyboard Extension sharing
Keyboard Extension sharingKeyboard Extension sharing
Keyboard Extension sharing
 
beidakejian
beidakejianbeidakejian
beidakejian
 
自动化运维管理
自动化运维管理自动化运维管理
自动化运维管理
 
Continuous Delivery: automated testing, continuous integration and continuous...
Continuous Delivery: automated testing, continuous integration and continuous...Continuous Delivery: automated testing, continuous integration and continuous...
Continuous Delivery: automated testing, continuous integration and continuous...
 
Uc gui中文手册
Uc gui中文手册Uc gui中文手册
Uc gui中文手册
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
Jni文档
Jni文档Jni文档
Jni文档
 
探奇部落格文章彙整
探奇部落格文章彙整探奇部落格文章彙整
探奇部落格文章彙整
 
Java2新觀念教本投影片ch1
Java2新觀念教本投影片ch1Java2新觀念教本投影片ch1
Java2新觀念教本投影片ch1
 

Más de Medivh2011 (19)

北京圣思园深入Java虚拟机 class loader
北京圣思园深入Java虚拟机 class loader北京圣思园深入Java虚拟机 class loader
北京圣思园深入Java虚拟机 class loader
 
Unit test lesson 3
Unit test lesson 3Unit test lesson 3
Unit test lesson 3
 
Unit test lesson 1
Unit test lesson 1Unit test lesson 1
Unit test lesson 1
 
Unit test lesson 2
Unit test lesson 2Unit test lesson 2
Unit test lesson 2
 
Reflection
ReflectionReflection
Reflection
 
Network
NetworkNetwork
Network
 
Multithread
MultithreadMultithread
Multithread
 
Jdk5
Jdk5Jdk5
Jdk5
 
Io 3
Io 3Io 3
Io 3
 
Io 2
Io 2Io 2
Io 2
 
Io 1
Io 1Io 1
Io 1
 
Inner class
Inner classInner class
Inner class
 
Event
EventEvent
Event
 
El
ElEl
El
 
Collection
CollectionCollection
Collection
 
Clone
CloneClone
Clone
 
Class loader
Class loaderClass loader
Class loader
 
Awt
AwtAwt
Awt
 
Ant
AntAnt
Ant
 

Swing