9.6 Swing组件

Download Report

Transcript 9.6 Swing组件

问题
Jdbc连接数据库
 下载数据库的驱动
 Sql server 在微软的网站上可以下载
 Sqlserver for jdbc driver
 sqljdbc_2.0.1803.100_enu.exe
 安装后目录下有sqljdbc4.jar
 将此jar包设置到classpath中

Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
String
connectionUrl=“jdbc:sqlserver://localhost:1433;database=dbname;user
=user;password=user”;
Connection ConnObj=DriverManager.getConnection(connectionUrl);
Statement stat=conn.createStatement();
String command=“select *from databasename”;
ResultSet rs=stat.executeQuery(command);
While (rs.next())
{
System.out.println(rs.getString(1));
}
第11章 Swing组件高级编程
顶层容器、中间容器和原子组件
Swing组件

组件和容器
– 顶层容器
– 中间层容器
– 原子组件
11.1 顶层容器

Swing提供了3个顶层容器类:JFrame、 JApplet、 JDialog
– 都是重量级组件,分别继承了AWT组件Frame、Applet和Dialog
– 每个顶层容器都有一个内容面板,通常直接或间接的容纳别的可视
组件
– 可以有选择地为顶层容器添加菜单,菜单被放置在顶层容器上,但
是在内容面板之外
JFrame的继承结构
Swing
组
件
java.lang.Object
└ java.awt.Component
└ java.awt.Container
└ java.awt.Window
└ java.awt.Frame
└ javax.swing.JFrame
每个包含Swing 组件的主窗口都应用JFrame 来实现
JApplet的继承结构
java.lang.Object
└ java.awt.Component
└ java.awt.Container
└ java.awt.Panel
└ java.awt.Applet
└ javax.swing.JApplet
每个包含Swing 组件的Applet 都应作为JApplet 的子类来实现
JDialog的继承结构
java.lang.Object
└ java.awt.Component
└ java.awt.Container
└ java.awt.Window
└ java.awt.Dialog
└ javax.swing.JDialog
要实现对话框,需要从JDialog派生一个类
如何获得一个顶层容器

JApplet类的顶层容器由浏览器提供,通常我们不需要自己产生一个
JApplet类的对象。JFrame和JDialog通过构造方法进行创建
名称
描述
JFrame()
建立一个新的JFrame,默认值是不可见的(Invisible)
JFrame(String title)
建立一个具有标题title的JFrame,默认值是不可见的
(Invisilble)
JApplet()
建立一个JApplet
JDialog()
建立一个non-moal对话框,无标题
JDialog(Dialog owner)
建立一个属于Dialog组件的对话框,为non-modal形式,
无标题
JDialog(Dialog owner, boolean
modal )
建立一个属于Dialog组件的对话框,可决定modal形式,
无标题
JDialog(Dialog owner, String
title)
建立一个属于Dialog组件的对话框,为non-modal形式,
有标题
——如何获得一个顶层容器续
JDialog(Dialog owner, String
title, boolean modal)
建立一个属于Dialog组件的对话框,可决定modal形式,
有标题
JDialog(Frame owner)
建立一个属于Frame组件的对话框,为non-modal形式,
无标题
JDialog(Frame owner, boolean
modal)
建立一个属于Frame组件的对话框,可决定modal形式,
无标题
JDialog(Frame owner, String title)
建立一个属于Frame组件的对话框,为non-modal形式,
有标题
JDialog(Frame owner, String title, 建立一个属于Frame组件的对话框,可决定modal形式,
boolean modal)
有标题
JOptionPane类

JDialog
– 必须实现对话框中的每一个组件
Swing
组
件

JOptionPane
– 只要显示一段文字,或是做一些简单的选择的简单对
话框
– 在javax.swing包内
– 通过使用这个类提供的一些静态方法
show×××Dialog,就可以产生四种简单的对话框

它们的方法参数中绝大部分(除了输入对话框可以不指
定父窗口)都需要提供一个父窗口组件ParentComponent,
只有关闭这些简单的对话框后,才可以返回到其父窗
口,也就是说,它们绝大部分都是模态的
11.2 中间层容器

中间层容器存在的目的仅仅是为了容纳别的组件,它分
为两类
– 一般用途的
 JPanel
 JScrollPane
 JSplitPane
 JTabbedPane
 JToolBar
– 特殊用途的
 JInternalFrame
 JRootPane(比较特殊,它由好几个部分构成,我
们可以直接从顶层容器中获得一个JRootPane对象
来直接使用,而不需要像别的中间容器那样,使
用的时候需要新建一个对象)
1.中间层容器JRootPane类

JRootPane的层次结构
– glassPane

中
间
层
容
器

默认状态下是隐藏的
可以使用glassPane来截获所有要到
达JRootPane别的部分的事件
– layeredPane


分为很多层(layer),每层都有一
个代表层深度的整数值(Z-order),
深度值高的组件将覆盖在深度值低
的组件上
contentPane
• 所在层的深度值是-30000
• 一般我们将所有组件添加到
contentPane上

JmenuBar
– 所在层的深度值是-30000
– 是可选的,如果没有,contentPane
就会充满整个顶层容器
2.中间层容器JPanel

JPanel
– 是一种经常使用的轻量级中间容器
– 在默认状态下,除了背景色外它并不绘制任何东西
– 可以很容易的为它设置边框和绘制特性,我们可以把它设置为
顶层容器contentPane。有效地利用JPanel可以使版面管理更为容
易
– 可以使用布局管理器来规划它所容纳的组件的位置和大小

可以通过setLayout方法来改变其布局

也可以在创建一个JPanel对象时就为它确定某种布局方式。
在默认状态下panel使用FlowLayout布局,将各组件布局在
一行
JPanel类常用API
名称
说明
JPanel()
创建一个JPanel,默认布局是FlowLayout
JPanel(LayoutManager layout)
创建一个指定布局的JPanel
void add(Component comp)
添加组件
void add(Component comp, int index)
把组件添加到特定位置上
int getComponentCount()
获得这个panel里所有组件的数量
Component getComponent(int index)
获得指定序号的某个组件
Component getComponentAt(int x, int y)
获得指定位置的某个组件
void remove(Component)
移除某个组件
void removeAll()
移除所有组件
void setLayout(LayoutManager layout)
设置布局
LayoutManager getLayout()
得到现有布局
void setBorder(Border border)
设置边框
——JPanel示例:Converter.java

Converter.java 用到如下类,都放在JPanelDemo包中
– ConversionPanel.java
– ConverterRangeModel.java
– DecimalField.java
– FollowerRangeModel.java
– FormattedDocument.java
– Unit.java
中间层容器JScrollPane

JScrollPane容器
– 当容器内要容纳的内容大于容器大小的时候,我们希望容器
能够有一个滚动条,通过拖动滑块,就可以看到更多的内容。
JScrollPane就是能够实现这种功能的特殊容器
– 由九个部分组成,包括一个中心显示地带、四个角和四条边
JScrollPane常用API
名称
说明
static int HORIZONTAL_SCROLLBAR_ALWAYS
水平滚动条策略常数:总是显示
static int HORIZONTAL_SCROLLBAR_AS_NEEDED
水平滚动条策略常数:当显示内容水
平区域大于显示区域时才出现
static int HORIZONTAL_SCROLLBAR_NEVER
水平滚动条策略常数:总是不显示
static int VERTICAL_SCROLLBAR_ALWAYS
垂直滚动条策略常数:总是显示
static int VERTICAL_SCROLLBAR_AS_NEEDED
垂直滚动条策略常数:当显示内容垂
直区域大于显示区域时才出现
static int VERTICAL_SCROLLBAR_NEVER
垂直滚动条策略常数:总是不显示
JScrollPane()
建立一个空的JScrollPane对象
JScrollPane(Component view)
建立一个显示组件的JScrollPane对
象,当组件内容大于显示区域时,
自动产生滚动条
JScrollPane常用API
void setViewportView(Component)
设置JscrllPane要中心地带要显示的
组件
void setVerticalScrollBarPolicy(int)
int getVerticalScrollBarPolicy()
设置或者读取垂直滚动条策略常数
void setHorizontalScrollBarPolicy(int)
int getHorizontalScrollBarPolicy()
设置或者读取水平滚动条策略常数
void setViewportBorder(Border)
Border getViewportBorder()
设置或者读取中心显示地带的边框
void setWheelScrollingEnabled(Boolean)
Boolean isWheelScrollingEnabled()
设置或者读取是否随着鼠标滚轮滚动
出现或隐藏滚动条,默认状态下
为真
void setColumnHeaderView(Component)
void setRowHeaderView(Component)
设置显示在上面的边上的组件
设置显示在左面的边上的组件
void setCorner(String key,Component corner)
设置要显示在指定角上的组件,key的
值为图9_18中表明的字符串之一
Component getCorner(String key)
获得指定角上的组件
——ScrollDemo2.java
Swing
组
件
中间层容器JSplitPane

JSplitPane
– 可以把两个组件显示在两个显示区域内,且随着区域间分隔
线的拖动,区域内组件的大小也随之发生变动
– 它允许设置水平分割或者垂直分割;也允许设置动态拖曳功
能(拖动分界线时两边组件是否会随着拖曳动态改变大小还
是在拖曳结束后才改动)
– 我们通常先把组件放到Scroll Pane中,再把Scroll Pane放到
Split Pane中。这样在每部分窗口中,都可以拖动滚动条看到
组件的全部内容
中间层容器(续)
——JSplitPane常用API
名称
说明
static int HORIZONTAL_SPLIT
水平分割常数
static int VERTICAL_SPLIT
垂直分割常数
JSplitPane()
创建一个JSplitPane,以水平方向排列,
两边各是一个button,没有动态拖曳
功能
JSplitPane(int newOrientation)
建立一个指定分割方向的JSplitPane,没
有动态拖曳功能,参数为两个分割常
数之一
JSplitPane(int newOrientation,
Boolean newContinuousLayout)
指定分割方向,并可指定是否有动态拖曳
功能
JSplitPane(int newOrientation,
Boolean newContinuousLayout,
Component newLeftComponent,
Component newRightComponent)
指定分割方向、是否具有动态拖曳功能,
和两侧组件
中间层容器(续)
——JSplitPane常用API
JSplitPane(int newOrientation,
Component newLeftComponent,
Component newRightComponent)
指定分割方向和两侧组件,无自动拖曳功能
void setOrientation(int newOrientation)
int getOrientation()
设置或获得分割方向
void setDividerSize(int)
int getDividerSize()
设置或读取分隔线条的粗细
void setContinuousLayout(boolean nCL)
boolean isContinuousLayout()
设置或读取是否使用动态拖曳功能
void setOneTouchExpandable(Boolean oTE)
boolean is OneTouchExpandable()
设置或读取是否在分隔线上显示一个控键来完全
扩展和完全压缩单侧内容。
void remove(Component comp)
void add(Component comp)
删除或添加组件。只可以添加两个组件
void setDividerLocation(double)
void setDividerLocation(int)
int getDividerLocation()
设置或读取分隔线条的位置。设置参数可以是
double型的百分数,也可以是int型的象素
值
中间层容器(续)
——JSplitPaneDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class SplitPaneDemo implements ListSelectionListener {
private Vector imageNames;
private JLabel picture;
private JList list;
private JSplitPane splitPane;
public SplitPaneDemo() {
ResourceBundle imageResource;
try { //Read image names from a properties file
imageResource = ResourceBundle.getBundle("imagenames");
String imageNamesString = imageResource.getString("images");
imageNames = parseList(imageNamesString);
} catch (MissingResourceException e) {
System.exit(1);
}
中间层容器(续)
——JSplitPaneDemo.java
list = new JList(imageNames);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
JScrollPane listScrollPane = new JScrollPane(list);
ImageIcon firstImage = new
ImageIcon("./build/classes/"+(String)imageNames.firstElement());
picture = new JLabel(firstImage);
picture.setPreferredSize(new Dimension(firstImage.getIconWidth(),
firstImage.getIconHeight()));
JScrollPane pictureScrollPane = new JScrollPane(picture);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
listScrollPane, pictureScrollPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);
Dimension minimumSize = new Dimension(100, 50);
listScrollPane.setMinimumSize(minimumSize);
pictureScrollPane.setMinimumSize(minimumSize);
splitPane.setPreferredSize(new Dimension(400, 200));
}
中间层容器(续)
——JSplitPaneDemo.java
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
JList theList = (JList)e.getSource();
if (theList.isSelectionEmpty()) {
picture.setIcon(null);
} else {
int index = theList.getSelectedIndex();
ImageIcon newImage = new ImageIcon("./build/classes/"+
(String)imageNames.elementAt(index));
picture.setIcon(newImage);
picture.setPreferredSize(new Dimension(newImage.getIconWidth(),
newImage.getIconHeight() ));
picture.revalidate();
}
}
中间层容器(续)
——JSplitPaneDemo.java
protected static Vector parseList(String theStringList) {
Vector v = new Vector(10);
StringTokenizer tokenizer = new StringTokenizer(theStringList, " ");
while (tokenizer.hasMoreTokens()) {
String image = tokenizer.nextToken();
v.addElement(image);
}
return v;
}
public static void main(String s[]) {
JFrame frame = new JFrame("SplitPaneDemo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
SplitPaneDemo splitPaneDemo = new SplitPaneDemo();
frame.getContentPane().add(splitPaneDemo.getSplitPane());
frame.pack();
frame.setVisible(true);
}
}
中间层容器(续)
——JSplitPaneDemo运行结果

程序说明
–
–
–
–
将一个JList组件放在一个JScrollPane容器中
将一个绘有图片的JLabel放在另一个JScrollPane容器中
将两个JScrollPane容器放到一个JSplitPane容器中
该类实现了ListSelectionListener接口,对列表选择事件可以
做出反应,使JLable显示出不同的图片
中间层容器JTabbedPane

JTabbedPane
– 如果一个窗口的功能有几项,我们可以给每项设置一个标签,
每个标签下面包含为完成此功能专用的若干组件
– 用户要使用哪项功能,只用点击相应的标签,就可以进入相
应的页面
中间层容器(续)
——JTabbedPane常用API
名称
说明
JTabbedPane()
创建一个tabbed pane。标签条位置在顶
部
JtabbedPane(int tabPlacement)
创建一个tabbed pane,并设置其标签位
置。参数为从SwingConstants接口中
继承来的TOP、BOTTOM、LEFT、RIGHT
之一。
void addTab(String title, Icon icon,
Component comp, String tip)
void addTab(String title, Icon icon,
Component comp)
void addTab(String, Component)
增加一个标签页,第一个String参数指
定显示在标签上的文本,可选的Icon
参数制定标签图标,Component参数
指定选择此标签页时要显示的组件,
最后一个可选的String参数是提示信
息
void insertTab(String title, Icon icon,
Component comp, String tip,
int index)
在指定位置index插入一个标签页,第一
个标签页的index是0,其余参数意义
同addTab方法
removeTabAt(int index)
删除指定位置的标签页
中间层容器(续)
——JTabbedPane常用API
int indexOfTabComponent comp)
int indexofTab(String title)
int indexofTab(Icon icon)
返回有指定组件、标题或图标的标
签页序号
void setSelectedIndex(int)
void setSelectedComponent(
Component comp)
选择指定序号或组件的标签页。选
择的效果是将显示此标签页所
含的所有内容
void setComponentAt(int index,
Component comp)
Component getComponentAt(int
index)
设置或读取指定序号标签页上的组
件
void setEnabledAt(int index,
Boolean enabled)
设置指定序号标签页是否可选
中间层容器(续)
——TabbedPaneDemo.java
Swing
组
件
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
ImageIcon icon = new ImageIcon("images/middle.gif");
JTabbedPane tabbedPane = new JTabbedPane();
Component panel1 = makeTextPanel("Blah");
tabbedPane.addTab("One", icon, panel1, "Does nothing");
tabbedPane.setSelectedIndex(0);
Component panel2 = makeTextPanel("Blah blah");
tabbedPane.addTab("Two", icon, panel2, "Does twice as much
nothing");
中间层容器(续)
——TabbedPaneDemo.java
Component panel3 = makeTextPanel("Blah blah blah");
tabbedPane.addTab("Three", icon, panel3, "Still does nothing");
Component panel4 = makeTextPanel("Blah blah blah blah");
tabbedPane.addTab("Four", icon, panel4, "Does nothing at all");
//Add the tabbed pane to this panel.
setLayout(new GridLayout(1, 1));
add(tabbedPane);
}
protected Component makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
中间层容器(续)
——TabbedPaneDemo.java
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPaneDemo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.getContentPane().add(new TabbedPaneDemo(),
BorderLayout.CENTER);
frame.setSize(400, 125);
frame.setVisible(true);
}
}
中间层容器(续)
——TabbedPaneDemo.java运行结果

说明
– 在构造函数中创建四个JPanel类型的对象
– 并将其依次添加到一个JTabbedPane类的对象容器中
– 最后再将该JTabbedPane类的对象容器放在一个JFrame对象的内容面
板中
中间层容器JToolBar

JToolBar
– 一般我们在设计界面时,会将所有功能分类放置在菜单中
(JMenu),但当功能相当多时,可能会使成用户为一个简单的操
作反复地寻找菜单中相关的功能
– 可以把一些常用的功能以工具栏的方式呈现在菜单下,这就是
JToolBar容器类的好处
– 我们可以将JToolBar设计为水平或垂直方向的,也可以以鼠标拉动
的方式来改变
中间层容器(续)
——JToolBar常用API
名称
JToolBar()
JToolBar(int orientation)
JToolBar(String name)
JToolBar(String name,int
orientation)
说明
创建一个工具栏,可以指定其朝向
orientation,为
SwingConstants中的
HORIZONTAL或VERTICLE,也可
以指定游离工具栏显示的名称
name。
void add(Component)
为工具栏添加一个组件
void addSeparator()
在末尾增加一个分隔线
void setFloatabled(Boolean
floatabled)
Boolean isFloatable()
设置或读取工具栏是否可以游离,
成为一个独立的窗口
中间层容器(续)
——ToolBarDemo.java
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
public class ToolBarDemo extends JFrame {
protected JTextArea textArea;
protected String newline = "\n";
public ToolBarDemo() {
//Do frame stuff.
super("ToolBarDemo");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
中间层容器(续)
——ToolBarDemo.java
JToolBar toolBar = new JToolBar(); //Create the toolbar.
addButtons(toolBar);
//Create the text area used for output.
textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
//Lay out the content pane.
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.setPreferredSize(new Dimension(400, 100));
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
setContentPane(contentPane);
}
中间层容器(续)
——ToolBarDemo.java
protected void addButtons(JToolBar toolBar) {
JButton button = null;
//first button
button = new JButton(new ImageIcon("./build/classes/left.gif"));
button.setToolTipText("This is the left button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayResult("Action for first button");
}
});
toolBar.add(button);
//second button
button = new JButton(new ImageIcon("./build/classes/middle.gif"));
button.setToolTipText("This is the middle button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayResult("Action for second button");
}
});
toolBar.add(button);
中间层容器(续)
——ToolBarDemo.java
//third button
button = new JButton(new ImageIcon("./build/classes/right.gif"));
button.setToolTipText("This is the right button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayResult("Action for third button");
}
});
toolBar.add(button);
}
protected void displayResult(String actionDescription) {
textArea.append(actionDescription + newline);
}
public static void main(String[] args) {
ToolBarDemo frame = new ToolBarDemo();
frame.pack();
frame.setVisible(true);
}
}
中间层容器(续)
——ToolBarDemo运行结果

说明
–
–
–
–
创建了三个按钮,并为每个按钮添加了事件监听器
将三个按钮放在一个JToolBar容器中
将JToolBar容器放在顶层容器的内容面板中
将一个包含JtextArea组件的JScrollPane容器也放在顶层容器的内容面
板中
中间层容器JInternalFram

JInternalFrame
– 如果要实现在一个主窗口中打开很多个文档,每个文档各自占用一个
新的窗口,就需要使用JInternalFrame容器类
– JInternalFrame的使用跟JFrame几乎一样,可以最大化、最小化、关闭
窗口、加入菜单
– 唯一不同的是JInternalFrame是轻量级组件,因此它只能是中间容器,
必须依附于顶层容器上
– 通常我们会将internal frame加入JDesktopPane类的对象来方便管理,
JDesktopPane继承自JLayeredPane,用来建立虚拟桌面。它可以显示
并管理众多internal frame之间的层次关系
中间层容器(续)
——JInternalFrame常用API
名称
JInternalFrame()
JInternalFrame(String title)
JInternalFrame(String title, boolean resizable)
JInternalFrame(String title, boolean resizable,
boolean closable)
JInternalFrame(String title, boolean resizable,
boolean closable, boolean maximizable)
JInternalFrame(String, boolean resizable,
boolean closable,
boolean maximizable,boolean iconifiable)
void setContentPane(Container c)
Container getContentPane()
说明
创建一个子窗口对象,依次设置
标题、是否可改变大小、可关闭、
可最大最小化、是否有图标。默
认状态下都是不可以。
设置或获得internal frame的内
容面板,通常包括除菜单以外的
所有可视组件
中间层容器(续)
——JInternalFrame常用API
void setJMenuBar(JMenuBar m)
JMenuBar getJMenuBar()
设置或获得internal frame的
菜单
void setVisible(boolean b)
此方法从Component类继承。设
置是否可见。应该在将
internal frame加到其父窗口
之前调用。
void setLocation(int x, int y)
设置相对于父窗口的位置。指
定左上角坐标
void setBounds(int x, int y, int width, int
height)
设置位置和大小
Void
addInternalFrameListener(InternalFrameListener
l)
添加事件监听器,相当于
windowlistener
void setSelected(boolean b)
boolean isSelected()
设置或获得当前是否被激活。
void setFramedIcon(Icon icon)
Icon getFrameIcon()
设置或获得显示在internal
frame标题栏中的图标
中间层容器(续)
——InternalFrameDemo.java
MyInternalFrame.java
11.3 原子组件

Swing原子组件有很多种, 与顶层容器和中间容器相比,原子组件用
法都比较简单

可将其分为三类
– 显示不可编辑信息

JLabel、JProgressBar、JToolTip
– 有控制功能、可以用来输入信息

JButton、JCheckBox、JRadioButton、JComboBox、JList、JMenu、
JSlider、JSpinner、JTextComponent
– 能提供格式化的信息并允许用户选择

JColorChooser、JFileChooser、JTable、JTree
第一类原子组件

JLabel
– 该组件上可以显示文字和图像,并能指定两者的位置

JProgressBar
– 在一些软件运行时,会出现一个进度条告知目前进度如何。通过使
用该组件我们可以轻松地为软件加上一个进度条

JToolTip提示信息
– 通常不必直接处理JToolTip类
– 通常使用setToolTipText()方法为组件设置提示信息
– 有的组件例如JTabbedPane由多个部分组成,需要鼠标在不同部分停
留时显示不同的提示信息,这时可以在其addTab()方法中设置提示信
息参数,也可以通过setTooltipTextAt方法进行设置
原子组件(续)
——例11_9

随时间增加,进度条输出进度变化情况,同时标签上显示出当前进度。鼠
标移到两组件时,显示提示信息
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
public class Ex11_9 implements ChangeListener {
JLabel label;
JProgressBar pb;
public Ex11_9() {
int value=0;
JFrame f=new JFrame("第一类原子组件演示");
Container contentPane=f.getContentPane ();
label=new JLabel("",JLabel.CENTER);
label.setToolTipText ("显示进度信息");
pb=new JProgressBar();
pb.setOrientation(JProgressBar.HORIZONTAL); //设置进度条方向
pb.setMinimum(0);
//设置最小值
pb.setMaximum(100);
//设置最大值
原子组件(续)
——例11_9
Swing
pb.setValue(value);
//初值
pb.setStringPainted(true);
//设置进度条上显示进度
pb.addChangeListener(this);
//增加时间监听器
pb.setToolTipText ("进度条");
//设置提示信息
contentPane.add(pb,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
f.setSize(400,60);
f.setVisible(true);
for(int i=1;i<=500000000;i++) {
if(i%5000000==0)
pb.setValue(++value); //改变进度条的值,触发ChangeEvent
}
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String[] args) { new Ex11_8(); }
public void stateChanged(ChangeEvent e) {
int value=pb.getValue();
label.setText ("目前已完成进度:"+value+"%");
}
组
件
}
原子组件(续)
——例11_9运行结果

说明
– Ex11_9类实现ChangeListener接口,实现了stateChanged方法
– main方法中随着程序的运行,不断改变进度条的当前值,触发
stateChanged方法,在JLable组件上显示当前进度信息
– 通过Component类的setTooltipText(String s)方法为组件添加提示信息
第二类原子组件:按钮类

按钮类
– AbstractButton抽象类是众多按钮类的基类,
继承它的类包括
JButton
 JToggleButton——表示有两个选择状态的按钮

– CheckBox(多选按钮)
– JRadioButton(单选按钮)

JMenuItem——在菜单中使用
– JCheckBoxMenuItem(多选按钮)
– JRadioButtonMenuItem(单选按钮)
– JMenu(一般的菜单项)
原子组件(续)
——第二类原子组件:按钮类
JComponent
JComponent
AbstractButton
AbstractButton
JButton
JToggleButton
JButton
JToggleButton
JCheckBox
JCheckBox
JRadioButton
JRadioButton
原子组件(续)
——几个例子
ButtonDemo.java
CheckBoxDemo.java
RadioButtonDemo.java
第二类原子组件:列表框

列表框JList
– 除了JCheckBox以外,JList也可以选择一到多个选项
– 有几种各有特色的构造方法(选项是否可添加、删除)
– 也提供了很多API可以用来设置各选项的显示方式(在一
列还是若干列)、选择模式(一次可选几项及是否可连续)
等
– 因为JList通常含有很多选项,所以经常把它放在一个
JScrollPane对象里面
– JList的事件处理一般可分为两类
 取得用户选取的项目,事件监听器是
ListSelectionListener
 对鼠标事件作出响应,其事件监听器是MouseListener
原子组件(续)
——ListDemo.java

说明
– 先创建一个LIstModel对象,再将ListModel对象作为参数创建一个
JList对象
– 列表框中的选择发生变化时将触发ListSelectionListener事件监听器
– 为fire、hire两个按钮添加ActionListener事件监听器,在其
ActionPerformed方法中修改ListModel对象的属性,以改变列表内容
原子组件(续)
——第二类原子组件:复选框

复选框JComboBox
– 允许用户在许多选项中选其一,可以有两种非常不一
样的格式

默认状态是不可编辑的模式,其特色是包括一个按钮和
一个下拉列表,用户只能在下拉列表提供的内容中选其
一

另一种是可编辑的模式,其特色是多了一个文本区域,
用户可以在此文本区域内填入列表中不包括的内容
– 复选框只需要很少的屏幕区域,而一组JRadioBox、
JCheckBox、JList占据的空间比较大
原子组件(续)
——ComboBoxDemo.java

Swing
组
件
说明
– 以一个字符串数组为参数创建了一个JComboBox对象
– 为该对象添加ActionListener事件监听器,当选择不同动物
名称时,显示不同的动物图片
原子组件(续)
——第二类原子组件:连续数值选择

连续数值选择
– JSlider

Swing
组
件


空间大
好像电器用品上机械式的控制杆,我们可以设置
它的最小、最大、初始刻度,还可以设置它的方
向,还可以为其标上刻度或文本
在JSlider上移动滑动杆,会产生ChangeEvent事件
– JSpinner




空间小
类似于可编辑的JComboBox,是种复合组件,由
三个部分组成:向上按钮、向下按钮和一个文本
编辑区
可以通过按钮来选择待选项,也可以直接在文本
编辑区内输入
和JComboBox不同的是,它的待选项不会显示出
来
原子组件(续)
——第二类原子组件:文本组件

文本组件
– 允许用户在里边编辑文本,能满足复杂的文本需求
– 都继承自JTextComponent抽象类,可分为三类
Swing
组
件

JTextField、JPasswordField、JFormattedTextField
– 它们只能显示和编辑一行文本,像按钮一样,它们可以产生
ActionEvent事件,因此通常用来接受少量用户输入信息并在输入结
束进行一些事件处理

JTextArea
– 它可以显示和编辑多行文本,但是这些文本只能是单一风格的,因
此通常用来让用户输入任意长度的无格式文本或显示无格式的帮助
信息

JEditorPane、JTextPane
– 可以显示和编辑多行多种式样的文本,嵌入图像或别的组件
原子组件(续)
——例11_10

Swing
组
件
在第一个窗口输入用户名和密码,要求输入密码使用
JPasswordField组件,密码正确后,才能进入第二个窗口进行
“+、-、×、÷”计算。运算符号使用单选按钮JRadioButton,
操作数使用JcomboBox和Jspinner组件
原子组件(续)
——第三类原子组件

Swing
组
件
JColorChooser颜色选择对话框
– 可以让用户选择所需要的颜色
– 通常使用这个类的静态方法showDialog()来输出标准的颜色
选择对话框,其返回值就是选择的颜色
– 也可以通过静态方法createDialog()方式输出个性化的颜色选
择对话框,例如为其添加菜单、定义其事件处理程序,这
个方法的返回值就是一个对话框
原子组件(续)
——第三类原子组件

Swing
组
件
JFileChooser文件选择对话框
– 让用户选择一个已有的文件或者新建一个文件
– 可以使用JFileChooser的showDialog、showOpenDialog或
showSaveDialog()方法来打开文件对话框,但是它仅仅会返
回用户选择的按钮(确认还是取消)和文件名(如果确认
的话),接下来的要实现的例如存盘或者打开的功能还需
要程序员自己编写
– 这个类提供了专门的方法用于设置可选择的文件类型,还
可以指定每类文件使用的类型图标
原子组件(续)
——第三类原子组件

JTable表格
– 和其相关的还有一些接口和类,包括TableModel、
Swing
组
件
AbstractTableModel、DefaultTableModel、SelectionModel、
TableColumnModel
– 可为表格设计显示外观(是否有滚动条、调整某一列宽其它
列宽变化情形)、显示模式(根据数据类型的不同有不同的
排列显示方式、为某一字段添加复选框JComboBox)、选择
模式(单选、多选、连续选、任意选)
– 在JTable的事件大致均针对表格内容的异操作处理,我们称为
TableModelEvent事件。通过addTableModelListener方法为表格
添加此种事件监听器
原子组件(续)
——第三类原子组件

Swing
组
件
JTree
– 用来产生树状结构来直观地表现层次关系,有根节点、树
枝节点、树叶节点
– 构造方法有多种,参数可以是一个Hashtable,也可以是
TreeNode或TreeModel对象
– 可以使用JComponent提供的putClientProperty方法来设置
JTree外观,也可以使用TreeCellRenderer来个性化各类节点
的显示样式
11.4 其它Swing特性
很多Swing组件都要使用一些相同的特性,包括
–Action对象
–边框
–设置外观和感觉
–应用线程
–定时器
11.4.1 Action对象

其
它
Swing
特
性
Action的用途
– Action接口是对ActionListener接口的一个有用的扩展,它的继承关系
如下
public interface Action extends ActionListener
– 在很多既有菜单又有工具栏的应用程序中,我们可以看到某条菜单
和工具栏的功能是相同的,按照前面所讲的方法,我们需要为每个
组件一一添加事件监听器,并在其处理程序中写入相同的代码,程
序就会显得比较繁琐。在这种场合,可以考虑使用Action对象实现此
功能
– 还可以通过它对不同组件的显示文字、图标、快捷键、提示文字、
是否可用等属性进行统一的设置
11.4.1 Action对象(续)
——创建Action对象

创建Action对象
其
它
– 通常我们首先需要创建一个继承抽象类AbstractAction类的
Swing
– AbstractAction抽象类提供了Action接口的默认实现,而且还
特
性
子类,然后再实例化这个子类
提供了一些获取和设置Action域属性的方法。我们在继承它
创建自己需要的子类的时候,只需要通过这些方法

设置需要的属性值

定义actionPerformed方法
11.4.1 Action对象(续)
——使用Action对象

其
它
Swing
特
性
使用Action对象
– 可通过GUI组件的setAction方法把Action关联到此GUI组件。每个具
有addActionListener方法的组件也都具有setAction方法
– 但setAction方法并不是addActionLilstener方法的替换:Action是一个
事件监听器,如果除此之外还需要添加别的监听器,则应该使用
addActionListener方法
– 对给定的GUI组件,你可以调用setAction不止一次,但组件和上一个
Action对象之间的关联会被删除
11.4.1 Action对象(续)
——使用Action对象的效果

其
它
Swing
特
性
通过setAction方法把Action对象关联到某GUI组件后,会
有如下效果
– 此组件的属性会被升级到符合这个Action对象的属性,例如
如果设置了Action对象的文字和图标值,那么组件的文字和
图标也会被重新设置成同样的值。
– 这个Action对象会被注册为此组件的一个事件监听器对象
– 如果改变了Action对象的属性或方法,则所有和它关联的组
件的使能状态也会发生相应改变
11.4.1 Action对象(续)
——ActionDemo.java
其
它
Swing
特
性
/* Uses actions with a tool bar and a menu. */
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
11.4.1 Action对象(续)
——ActionDemo.java
其
它
Swing
特
性
public class ActionDemo extends JFrame {
protected JTextArea textArea;
protected String newline = "\n";
protected Action leftAction;
protected Action middleAction;
protected Action rightAction;
public ActionDemo() {
super("ActionDemo");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Create the toolbar and menu.
JToolBar toolBar = new JToolBar();
JMenu mainMenu = new JMenu("Menu");
createActionComponents(toolBar, mainMenu);
11.4.1 Action对象(续)
——ActionDemo.java
//Create the text area used for output.
textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
//Lay out the content pane.
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.setPreferredSize(new Dimension(400, 150));
contentPane.add(toolBar, BorderLayout.SOUTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
setContentPane(contentPane);
//Set up the menu bar.
JMenuBar mb = new JMenuBar();
mb.add(mainMenu);
mb.add(createAbleMenu());
setJMenuBar(mb);
其
它
Swing
特
性
}
11.4.1 Action对象(续)
——ActionDemo.java
其
它
Swing
特
性
protected void createActionComponents(JToolBar toolBar,
JMenu mainMenu) {
JButton button = null;
JMenuItem menuItem = null;
//first button and menu item
leftAction = new AbstractAction("Go left",
new ImageIcon("./build/classes/left.gif")) {
public void actionPerformed(ActionEvent e) {
displayResult("Action for first button/menu item", e);
}
};
button = toolBar.add(leftAction);
button.setText(""); //an icon-only button
button.setToolTipText("This is the left button");
menuItem = mainMenu.add(leftAction);
menuItem.setIcon(null); //arbitrarily chose not to use icon in menu
11.4.1 Action对象(续)
——ActionDemo.java
其
它
Swing
特
性
//second button and menu item
middleAction = new AbstractAction("Do something",
new ImageIcon("./build/classes/middle.gif")) {
public void actionPerformed(ActionEvent e) {
displayResult("Action for second button/menu item", e);
}
};
button = toolBar.add(middleAction);
button.setText("");
button.setToolTipText("This is the middle button");
menuItem = mainMenu.add(middleAction);
menuItem.setIcon(null); //arbitrarily chose not to use icon in menu
11.4.1 Action对象(续)
——ActionDemo.java
//third button and menu item
rightAction = new AbstractAction("Go right",
new ImageIcon("./build/classes/right.gif")) {
public void actionPerformed(ActionEvent e) {
displayResult("Action for third button/menu item", e);
}
};
button = toolBar.add(rightAction);
button.setText("");
button.setToolTipText("This is the right button");
menuItem = mainMenu.add(rightAction);
menuItem.setIcon(null); //arbitrarily chose not to use icon in menu
其
它
Swing
特
性
}
11.4.1 Action对象(续)
——ActionDemo.java
protected JMenu createAbleMenu() {
JMenu ableMenu = new JMenu("Action State");
JCheckBoxMenuItem cbmi = null;
cbmi = new JCheckBoxMenuItem("First action enabled");
cbmi.setSelected(true);
cbmi.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JCheckBoxMenuItem mi = (JCheckBoxMenuItem)(e.getSource());
boolean selected =
(e.getStateChange() == ItemEvent.SELECTED);
leftAction.setEnabled(selected);
}
});
ableMenu.add(cbmi);
//…这里省略了增加另外两个类似的菜单复选框的语句…
return ableMenu;
其
它
Swing
特
性
}
11.4.1 Action对象(续)
——ActionDemo.java
protected void displayResult(String actionDescription,
ActionEvent e) {
String s = ("Action event detected by: "
+ actionDescription
+ newline
+ " Event source: " + e.getSource()
+ newline);
textArea.append(s);
}
其
它
Swing
static void main(String[] args) {
特 public
ActionDemo frame = new ActionDemo();
性 frame.pack();
frame.setVisible(true);
}
}
11.4.1 Action对象(续)
——ActionDemo.java

Swing
其
它
特
性
说明
– 创建了三个Action对象,在每个对象中定义了文本、图片等属性以及
actionPerformed方法
– 分别为三组按钮、菜单分别添加三个Action对象,使组内的按钮、菜
单具有相同的功能
– 创建了三个复选框菜单选项,在其事件监听器ItemListener()中改变
Action对象的enabled属性,因此可以同时改变按钮、菜单的使能状
态
11.4.2 边框

其
它
Swing
特
性
边框(Border)
– 每个继承自JComponent的Swing组件都可以有若干个边框
– 边框可以用来



绘制线条
为组件提供标题
为组件提供周边的空白区域
– 我们使用组件的setBorder方法为组件添加边框。这个方法需
要提供一个Border类型的对象


通常可以使用BorderFactory类提供的很多静态方法产生一个常
用的Border对象
如果不能够满足要求,可以直接使用javax.swing.border包里的
API来定义自己的边框
11.4.2 边框(续)
——有关border的常用API
名称
说明
BorderFactory类里的静态方法,返回类型都是Border
其
它
Swing
特
性
createLineBorder(Color color)
createLineBorder(Color color, int
thickness)
创建指定颜色、线宽的线框
createEtcheBorder()
createEtchedBorder(Color hightlight, Color
shadow)
createEtchedBorder(int type)
createEtcheBorder(int type, Color
highlight, Color shadow)
创建突起或凹陷的边框。可选的
Color参数指定了使用的高亮和
阴影色。type参数为
EtchedBorder.LOWERED或
EtchedBorder.RAISED之一,如
果不含此参数,则为LOWERED。
createLoweredBevelBorder()
创建一种边框,给人感觉组件比周
围低
createRaiseBevelBorder()
创建一种边框,给人感觉组件比周
围高
11.4.2 边框(续)
——有关border的常用API
其
它
Swing
特
性
createBevelBorder(int type, Color highlight,
Color shadow)
createBevelBorder(int type,
Color highlightOuter, Color highlightInner,
Color shadowOuter, Color shadowInner)
创建为组件造成突起或下沉的效果
的边框,type为
BevelBorder.RAISED或
BevelBorder.LOWERED。颜色参数用
来指定外层和内层的高亮色和阴影
色
createEmptyBorder()
createEmptyBorder(int top, int left, int
bottom, int right)
创建一不显示的边框,无参数的边
框不占空间,有参数的指定了边框
在四个边上占据的象素数
MatterBorder createMatteBorder(int top, int
left, int bottom, int right, Color color)
MatterBorder createMatteBorder(int top, int
left, int bottom, int right, Icon tileIcon)
创建一个不光滑的边框,四个整数
参数指定了边框在四个边上占据的
象素数,颜色参数指定了边框的颜
色,图标参数指定了填充边框的图
标
11.4.2 边框(续)
——有关border的常用API
其
它
Swing
特
性
TitledBorder createTitledBorder(
Border border,String title,
int titleJustification,
int titlePosition,
Font titleFont, Color titleColor)
为已有的border增加标题title,并
指定标题位置、字体、颜色。某
些参数是可选的,具体用法见
API文档。
CompoundBorder createCompoundBorder(
Border outsideBorder, Border insideBorder)
创建一个双层边框
Swing组件用来设置边框的API
void setBorder(Border border)
Border getBorder()
设置或获得JComponent组件的边框
void setBorderPainted(Boolean)
Boolean isBorderPainted()
设置或获得是否显示边框,用在
AbstractButton、JMenuBar、
JPopupMenu、JProgressBar和
JToolBar中。
11.4.1 Action对象(续)
——BorderDemo.java

其
它
Swing
特
性
说明
– 创建了四个JPanel组件,放在一个JTabbedPane中间容器中
– 在每个JPanel组件中,又创建了若干个JPanel组件,并为其


通过setBorder方法制定不同的边框
通过一个JLabel组件显示出对该种边框的描述
11.4.3 设置外观和感觉

其
它
Swing
特
性
Swing有一个十分有趣的特点,就是所谓的可插入式的外
观和感觉(Pluggable Look & Feel),允许程序模拟各种
操作系统的外观和感觉
– JAVA Look and Feel


类名为“javax.swing.plaf.metal.MetalLookAndFeel”
程序默认的Look and Feel,这是java提供的跨平台的有金属质
感的外观和感觉。可以用在所有平台上
– Motif Look and Feel


类名为“com.sun.java.swing.plaf.motif.MotifLookAndFeel”
仿真UNIX环境的外观和感觉,也可以用在所有平台上
– Microsoft Windows


类名为“com.sun.java.swing.plaf.windows.WindowsLookAndFeel”
它只能在Microsoft Windows的操作系统下使用。
– Macintosh style


类名为“com.sun.java.swing.plaf.mac.MacLookAndFeel”
只能在Macintosh的操作系统下使用
11.4.3 设置外观和感觉(续)
——为组件进行设置

其
它
Swing
特
性
我们需要在产生任何可视组件以前就设置好它们的外观和感觉,
要设置某种外观和感觉,必须使用UIManager类所提供的
setLookAndFeel()静态方法,这个方法的参数为上面提到的类名
字符串之一。通常我们只会做两种选择
– java提供的跨平台的外观和感觉。可以利用UIManager类提供的
getCrossPlatformLookAndFeelClassName()静态方法获得类名
– 程序所处系统的外观和感觉。可以利用UIManager类提供的
getSystemLookAndFeel()静态方法获得目前操作平台的Look and
Feel类名称字符串
11.4.3 设置外观和感觉(续)
——为顶层容器进行设置

其
它
Swing
特
性
前述设置不能影响顶层容器JFrame和JDialog,因为它们是重量级
组件,均是依赖于操作系统(Operating System dependent)的,当
使用的操作系统不同时,所显示的顶层容器就会有所不同。针对
这两个顶层容器,有一个静态方法专门为其设置外观感觉
– static void setDefaultLookAndFeelDecorated(boolean)


参数是true,就会选用默认的外观感觉
参数是false,就会选用操作平台的外观感觉
11.4.3 设置外观和感觉(续)

其
它
Swing

Java 外观和感觉
CDE/Motif外观和感觉
特
性 Windows外观和感觉
11.4.4 应用线程

其
它
Swing
特
性

一个Swing组件被创建出来后,应该只有事件处理的线程可
以访问它。别的线程最好应该不去调用关于该组件的方法。
否则有可能造成死锁
在某些情况下应该使用线程
– GUI界面初始化中有一个很耗时的任务要完成(比如复杂的计
算、网络堵塞、磁盘I/O),我们可以把这个任务放到主线程之
外进行,这样界面就会出现得比较快
– 点击一个按钮进入一段事件处理程序中,而这段程序本身很费
时间,如果这段程序不结束,GUI就无法响应别的事件,解决
办法就是把这段事件处理程序放到另外一个线程中,例如一个
后台线程
– 定期重复执行某项操作
– 等待从其它程序传来的消息
11.4.4 应用线程(续)
——例11_11

其
它
Swing
特
性
单线程的绘图程序:有一个按钮和标签,标签可以在上面画线,
按钮点击后则进入无限循环(点击按钮可能造成死机)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ex11_11 extends JFrame {
int x,y;
public Ex11_11() {
this.setTitle ("线程演示");
Container c=this.getContentPane();
JLabel L=new JLabel("点击可以画线哟",JLabel.CENTER);
c.add(L,BorderLayout.CENTER);
JButton b=new JButton(“试试按我,别后悔啊");
b.setBorder (BorderFactory.createLineBorder (Color.red,5));
c.add(b,BorderLayout.SOUTH);
11.4.4 应用线程(续)
——例11_11
L.addMouseListener (new MouseAdapter(){
public void mousePressed(MouseEvent e){
x=e.getX(); //获得按点的横坐标
y=e.getY(); //获得按点的纵坐标
repaint(); //重绘组件
} });
b.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
while(true); //点击按钮后进入死循环
}
});
this.setSize (300,400);
this.show ();
//显示
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
其
它
Swing
特
性
}
public void update(Graphics g) {.drawLine(0,0,x,y);}
public static void main(String[] args) {
new Ex11_11(); }
}
11.4.4 应用线程(续)
——例11_11运行结果

其
它
Swing
特
性
说明
– 为标签添加MouseAdapter鼠标事件
监听器,对鼠标点击事件做出响应
– 通过MouseEvent事件对象可获得鼠
标点击的坐标,调用repaint方法后,
该方法又会自动调用update方法重绘
GUI
– 为按钮添加ActionListener事件监听
器,鼠标点击后进入死循环,不能
继续对点击标签事件做出响应了
11.4.4 应用线程(续)
——例11_12

其
它
Swing
特
性
多线程的绘图程序:修改上例,使点击按钮后虽然进入无限循环,仍可
以画线
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ex11_12 extends JFrame {
int x,y;
public Ex11_12() {
this.setTitle ("线程演示");
Container c=this.getContentPane();
JLabel L=new JLabel("点击可以画线哟",JLabel.CENTER);
c.add(L,BorderLayout.CENTER);
JButton b=new JButton("试试按我");
b.setBorder (BorderFactory.createLineBorder (Color.red,5));
c.add(b,BorderLayout.SOUTH);
L.addMouseListener (new MouseAdapter(){
public void mousePressed(MouseEvent e){
x=e.getX(); //获得按点的横坐标
y=e.getY(); //获得按点的纵坐标
repaint(); //重绘组件
} });
11.4.4 应用线程(续)
——例11_12
其
它
Swing
特
性
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new newThread().start();
}
});
this.setSize (300,400);
this.show ();
//显示
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void update(Graphics g) {.drawLine(0,0,x,y); }
public static void main(String[] args) { new Ex11_12(); }
}
class newThread extends Thread {
public void run() { while(true);}
}
11.4.4 应用线程(续)
——例11_12运行结果

在对按钮的ActionListener事件监听器
的事件处理方法actionPerformed方法
其
它
Swing
特
性
中,将无限循环放到了另外一个线程
中运行,这样就不会影响对标签的鼠
标点击事件做出响应了
11.4.5 定时器

其
它
Swing
特
性
如果需要让程序按照一定时间顺序依次完成某项任务,就需要使
用Timer组件
– 其完整的类名是javax.swing.Timer
– Timer组件会根据程序员设定的间隔时间,周期性的触发ActionEvent
事件
– 这个类提供了一系列API用于对定时器的各种操作,例如启动、关闭、
设置间隔时间、设置是否只使用一次等
11.4.5 定时器(续)
——Timer类常用API
其
它
Swing
特
性
名称
说明
Timer(int delay, ActionListener
listener)
创建一个指定间隔时间和事件监听器的定时器
void setDelay(int milliseconds)
int getDelay()
void setInitialDelay(int
milliseconds)
int getInitialDelay()
设置或获得间隔事件,单位是毫秒
设置或获得首次触发Timer事件的时间间隔,以后的
间隔是setDealy设置的数值。
void setRepeats(boolean flag)
boolean isRepeats()
设置或获得是否可以连续触发,如果是false,只触
发一次事件
void setCoalesce(boolean flag)
boolean isCoalesce()
设置或获得定时器是否将若干未处理的一系列事件
削减为一个,而不至于积压排队,默认状态是true
void start()
开启定时器
void restart()
取消当前定时器事件,并在initialDelay间隔后重
新开启定时器
void stop()
关闭定时器
boolean isRunning
获得定时器是否在运行
11.4.5 定时器(续)
——AnimatorApplicationTimer.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimatorApplicationTimer extends JFrame
implements ActionListener {
int frameNumber = -1;
Timer timer;
boolean frozen = false;
JLabel label;
AnimatorApplicationTimer(int fps, String windowTitle) {
super(windowTitle);
int delay = (fps > 0) ? (1000 / fps) : 100;
//Set up a timer that calls this object's action handler.
timer = new Timer(delay, this);
timer.setInitialDelay(0);
timer.setCoalesce(true);
其
它
Swing
特
性
11.4.5 定时器(续)
——AnimatorApplicationTimer.java
addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) { stopAnimation();}
public void windowDeiconified(WindowEvent e) {startAnimation();}
public void windowClosing(WindowEvent e) {System.exit(0);}
});
label = new JLabel("Frame ", JLabel.CENTER);
label.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (frozen) { frozen = false; startAnimation(); }
else { frozen = true; stopAnimation(); }
}
});
getContentPane().add(label, BorderLayout.CENTER);
其
它
Swing
特
性
}
11.4.5 定时器(续)
——AnimatorApplicationTimer.java
其
它
Swing
特
性
//Can be invoked by any thread (since timer is thread-safe).
public void startAnimation() {
if (frozen) {
//Do nothing. The user has requested that we
//stop changing the image.
} else {
//Start animating!
if (!timer.isRunning()) {
timer.start();
}
}
}
//Can be invoked by any thread (since timer is thread-safe).
public void stopAnimation() {
//Stop the animating thread.
if (timer.isRunning()) {
timer.stop();
}
}
11.4.5 定时器(续)
——AnimatorApplicationTimer.java
public void actionPerformed(ActionEvent e) {
//Advance the animation frame.
frameNumber++;
label.setText("Frame " + frameNumber);
}
public static void main(String args[]) {
AnimatorApplicationTimer animator = null;
int fps = 10;
//Get frames per second from the command line argument.
if (args.length > 0) {
try { fps = Integer.parseInt(args[0]);} catch (Exception e) {}
}
animator = new AnimatorApplicationTimer(fps, "Animator with Timer");
animator.pack();
animator.setVisible(true);
animator.startAnimation();
}
其
它
Swing
特
性
}
11.4.5 定时器(续)
——AnimatorApplicationTimer运行结果

其
它
Swing
特
性
说明
– 为窗口通过addWindowListener方法添加了事件监听器,窗
口最小化时停止Timer,窗口恢复时重新启动Timer
– 为标签通过addMouthListener方法添加了事件监听器,在标
签上点击时可以交替暂停和启动Timer
– Timer组件会根据程序员设定的间隔时间,周期性的触发
ActionEvent事件,在ActionEvent中让标签上显示的信息递
增