Presentation is loading. Please wait.

Presentation is loading. Please wait.

问题 Jdbc连接数据库 下载数据库的驱动 Sql server 在微软的网站上可以下载 Sqlserver for jdbc driver

Similar presentations


Presentation on theme: "问题 Jdbc连接数据库 下载数据库的驱动 Sql server 在微软的网站上可以下载 Sqlserver for jdbc driver"— Presentation transcript:

1 问题 Jdbc连接数据库 下载数据库的驱动 Sql server 在微软的网站上可以下载 Sqlserver for jdbc driver
sqljdbc_ _enu.exe 安装后目录下有sqljdbc4.jar 将此jar包设置到classpath中

2 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)); }

3 第11章 Swing组件高级编程 顶层容器、中间容器和原子组件

4 Swing组件 组件和容器 顶层容器 中间层容器 原子组件

5 11.1 顶层容器 Swing提供了3个顶层容器类:JFrame、 JApplet、 JDialog
都是重量级组件,分别继承了AWT组件Frame、Applet和Dialog 每个顶层容器都有一个内容面板,通常直接或间接的容纳别的可视组件 可以有选择地为顶层容器添加菜单,菜单被放置在顶层容器上,但是在内容面板之外

6 JFrame的继承结构 Swing 组件 java.lang.Object └ java.awt.Component
└ java.awt.Container └ java.awt.Window └ java.awt.Frame └ javax.swing.JFrame 每个包含Swing 组件的主窗口都应用JFrame 来实现

7 java.lang.Object └ java.awt.Component └ java.awt.Container
JApplet的继承结构 java.lang.Object └ java.awt.Component └ java.awt.Container └ java.awt.Panel └ java.awt.Applet └ javax.swing.JApplet 每个包含Swing 组件的Applet 都应作为JApplet 的子类来实现

8 JDialog的继承结构 java.lang.Object └ java.awt.Component
└ java.awt.Container └ java.awt.Window └ java.awt.Dialog └ javax.swing.JDialog 要实现对话框,需要从JDialog派生一个类

9 如何获得一个顶层容器 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形式,有标题

10 ——如何获得一个顶层容器续 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, boolean modal) 建立一个属于Frame组件的对话框,可决定modal形式,有标题

11 JOptionPane类 Swing 组件 JDialog JOptionPane 必须实现对话框中的每一个组件
只要显示一段文字,或是做一些简单的选择的简单对话框 在javax.swing包内 通过使用这个类提供的一些静态方法show×××Dialog,就可以产生四种简单的对话框 它们的方法参数中绝大部分(除了输入对话框可以不指定父窗口)都需要提供一个父窗口组件ParentComponent,只有关闭这些简单的对话框后,才可以返回到其父窗口,也就是说,它们绝大部分都是模态的

12 11.2 中间层容器 中间层容器存在的目的仅仅是为了容纳别的组件,它分为两类 一般用途的 JPanel JScrollPane
JSplitPane JTabbedPane JToolBar 特殊用途的 JInternalFrame JRootPane(比较特殊,它由好几个部分构成,我们可以直接从顶层容器中获得一个JRootPane对象来直接使用,而不需要像别的中间容器那样,使用的时候需要新建一个对象)

13 1.中间层容器JRootPane类 中间层容器 JRootPane的层次结构 glassPane layeredPane 默认状态下是隐藏的
可以使用glassPane来截获所有要到达JRootPane别的部分的事件 layeredPane 分为很多层(layer),每层都有一个代表层深度的整数值(Z-order),深度值高的组件将覆盖在深度值低的组件上 contentPane 所在层的深度值是-30000 一般我们将所有组件添加到contentPane上 JmenuBar 是可选的,如果没有,contentPane就会充满整个顶层容器

14 2.中间层容器JPanel JPanel 是一种经常使用的轻量级中间容器 在默认状态下,除了背景色外它并不绘制任何东西
可以很容易的为它设置边框和绘制特性,我们可以把它设置为顶层容器contentPane。有效地利用JPanel可以使版面管理更为容易 可以使用布局管理器来规划它所容纳的组件的位置和大小 可以通过setLayout方法来改变其布局 也可以在创建一个JPanel对象时就为它确定某种布局方式。在默认状态下panel使用FlowLayout布局,将各组件布局在一行

15 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) 设置边框

16 ——JPanel示例:Converter.java Converter.java 用到如下类,都放在JPanelDemo包中
ConversionPanel.java ConverterRangeModel.java DecimalField.java FollowerRangeModel.java FormattedDocument.java Unit.java

17 中间层容器JScrollPane JScrollPane容器
由九个部分组成,包括一个中心显示地带、四个角和四条边

18 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对象,当组件内容大于显示区域时,自动产生滚动条

19 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) 获得指定角上的组件

20 ——ScrollDemo2.java Swing 组件

21 中间层容器JSplitPane JSplitPane
可以把两个组件显示在两个显示区域内,且随着区域间分隔线的拖动,区域内组件的大小也随之发生变动 它允许设置水平分割或者垂直分割;也允许设置动态拖曳功能(拖动分界线时两边组件是否会随着拖曳动态改变大小还是在拖曳结束后才改动) 我们通常先把组件放到Scroll Pane中,再把Scroll Pane放到Split Pane中。这样在每部分窗口中,都可以拖动滚动条看到组件的全部内容

22 中间层容器(续) ——JSplitPane常用API
名称 说明 static int HORIZONTAL_SPLIT 水平分割常数 static int VERTICAL_SPLIT 垂直分割常数 JSplitPane() 创建一个JSplitPane,以水平方向排列,两边各是一个button,没有动态拖曳功能 JSplitPane(int newOrientation) 建立一个指定分割方向的JSplitPane,没有动态拖曳功能,参数为两个分割常数之一 JSplitPane(int newOrientation, Boolean newContinuousLayout) 指定分割方向,并可指定是否有动态拖曳功能 Boolean newContinuousLayout, Component newLeftComponent, Component newRightComponent) 指定分割方向、是否具有动态拖曳功能,和两侧组件

23 中间层容器(续) ——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型的象素值

24 中间层容器(续) ——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); }

25 中间层容器(续) ——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)); }

26 中间层容器(续) ——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(); }

27 中间层容器(续) ——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);

28 中间层容器(续) ——JSplitPaneDemo运行结果
程序说明 将一个JList组件放在一个JScrollPane容器中 将一个绘有图片的JLabel放在另一个JScrollPane容器中 将两个JScrollPane容器放到一个JSplitPane容器中 该类实现了ListSelectionListener接口,对列表选择事件可以做出反应,使JLable显示出不同的图片

29 中间层容器JTabbedPane JTabbedPane
如果一个窗口的功能有几项,我们可以给每项设置一个标签,每个标签下面包含为完成此功能专用的若干组件 用户要使用哪项功能,只用点击相应的标签,就可以进入相应的页面

30 中间层容器(续) ——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) 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) 删除指定位置的标签页

31 中间层容器(续) ——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) 设置指定序号标签页是否可选

32 中间层容器(续) ——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");

33 中间层容器(续) ——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;

34 中间层容器(续) ——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); }

35 中间层容器(续) ——TabbedPaneDemo.java运行结果
说明 在构造函数中创建四个JPanel类型的对象 并将其依次添加到一个JTabbedPane类的对象容器中 最后再将该JTabbedPane类的对象容器放在一个JFrame对象的内容面板中

36 中间层容器JToolBar JToolBar
一般我们在设计界面时,会将所有功能分类放置在菜单中(JMenu),但当功能相当多时,可能会使成用户为一个简单的操作反复地寻找菜单中相关的功能 可以把一些常用的功能以工具栏的方式呈现在菜单下,这就是JToolBar容器类的好处 我们可以将JToolBar设计为水平或垂直方向的,也可以以鼠标拉动的方式来改变

37 中间层容器(续) ——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() 设置或读取工具栏是否可以游离,成为一个独立的窗口

38 中间层容器(续) ——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); } });

39 中间层容器(续) ——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); }

40 中间层容器(续) ——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"); displayResult("Action for second button");

41 中间层容器(续) ——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);

42 中间层容器(续) ——ToolBarDemo运行结果
说明 创建了三个按钮,并为每个按钮添加了事件监听器 将三个按钮放在一个JToolBar容器中 将JToolBar容器放在顶层容器的内容面板中 将一个包含JtextArea组件的JScrollPane容器也放在顶层容器的内容面板中

43 中间层容器JInternalFram JInternalFrame
JInternalFrame的使用跟JFrame几乎一样,可以最大化、最小化、关闭窗口、加入菜单 唯一不同的是JInternalFrame是轻量级组件,因此它只能是中间容器,必须依附于顶层容器上 通常我们会将internal frame加入JDesktopPane类的对象来方便管理,JDesktopPane继承自JLayeredPane,用来建立虚拟桌面。它可以显示并管理众多internal frame之间的层次关系

44 中间层容器(续) ——JInternalFrame常用API
名称 说明 JInternalFrame() JInternalFrame(String title) JInternalFrame(String title, boolean resizable) JInternalFrame(String title, boolean resizable, boolean closable) boolean closable, boolean maximizable) JInternalFrame(String, boolean resizable, boolean closable, boolean maximizable,boolean iconifiable) 创建一个子窗口对象,依次设置标题、是否可改变大小、可关闭、可最大最小化、是否有图标。默认状态下都是不可以。 void setContentPane(Container c) Container getContentPane() 设置或获得internal frame的内容面板,通常包括除菜单以外的所有可视组件

45 中间层容器(续) ——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标题栏中的图标

46 中间层容器(续) ——InternalFrameDemo.java MyInternalFrame.java

47 11.3 原子组件 Swing原子组件有很多种, 与顶层容器和中间容器相比,原子组件用法都比较简单 可将其分为三类 显示不可编辑信息
JLabel、JProgressBar、JToolTip 有控制功能、可以用来输入信息 JButton、JCheckBox、JRadioButton、JComboBox、JList、JMenu、JSlider、JSpinner、JTextComponent 能提供格式化的信息并允许用户选择 JColorChooser、JFileChooser、JTable、JTree

48 第一类原子组件 JLabel JProgressBar JToolTip提示信息 该组件上可以显示文字和图像,并能指定两者的位置
在一些软件运行时,会出现一个进度条告知目前进度如何。通过使用该组件我们可以轻松地为软件加上一个进度条 JToolTip提示信息 通常不必直接处理JToolTip类 通常使用setToolTipText()方法为组件设置提示信息 有的组件例如JTabbedPane由多个部分组成,需要鼠标在不同部分停留时显示不同的提示信息,这时可以在其addTab()方法中设置提示信息参数,也可以通过setTooltipTextAt方法进行设置

49 原子组件(续) ——例11_9 随时间增加,进度条输出进度变化情况,同时标签上显示出当前进度。鼠标移到两组件时,显示提示信息
原子组件(续) ——例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); //设置最大值

50 原子组件(续) ——例11_9 Swing 组件 pb.setValue(value); //初值
原子组件(续) ——例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<= ;i++) { if(i% ==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+"%");

51 原子组件(续) ——例11_9运行结果 说明 Ex11_9类实现ChangeListener接口,实现了stateChanged方法
原子组件(续) ——例11_9运行结果 说明 Ex11_9类实现ChangeListener接口,实现了stateChanged方法 main方法中随着程序的运行,不断改变进度条的当前值,触发stateChanged方法,在JLable组件上显示当前进度信息 通过Component类的setTooltipText(String s)方法为组件添加提示信息

52 第二类原子组件:按钮类 按钮类 AbstractButton抽象类是众多按钮类的基类,继承它的类包括 JButton
JToggleButton——表示有两个选择状态的按钮 CheckBox(多选按钮) JRadioButton(单选按钮) JMenuItem——在菜单中使用 JCheckBoxMenuItem(多选按钮) JRadioButtonMenuItem(单选按钮) JMenu(一般的菜单项)

53 原子组件(续) ——第二类原子组件:按钮类
原子组件(续) ——第二类原子组件:按钮类 JComponent AbstractButton JToggleButton JRadioButton JCheckBox JButton

54 原子组件(续) ——几个例子 ButtonDemo.java CheckBoxDemo.java RadioButtonDemo.java

55 第二类原子组件:列表框 列表框JList 除了JCheckBox以外,JList也可以选择一到多个选项
有几种各有特色的构造方法(选项是否可添加、删除) 也提供了很多API可以用来设置各选项的显示方式(在一列还是若干列)、选择模式(一次可选几项及是否可连续)等 因为JList通常含有很多选项,所以经常把它放在一个JScrollPane对象里面 JList的事件处理一般可分为两类 取得用户选取的项目,事件监听器是ListSelectionListener 对鼠标事件作出响应,其事件监听器是MouseListener

56 原子组件(续) ——ListDemo.java
说明 先创建一个LIstModel对象,再将ListModel对象作为参数创建一个JList对象 列表框中的选择发生变化时将触发ListSelectionListener事件监听器 为fire、hire两个按钮添加ActionListener事件监听器,在其ActionPerformed方法中修改ListModel对象的属性,以改变列表内容

57 原子组件(续) ——第二类原子组件:复选框
原子组件(续) ——第二类原子组件:复选框 复选框JComboBox 允许用户在许多选项中选其一,可以有两种非常不一样的格式 默认状态是不可编辑的模式,其特色是包括一个按钮和一个下拉列表,用户只能在下拉列表提供的内容中选其一 另一种是可编辑的模式,其特色是多了一个文本区域,用户可以在此文本区域内填入列表中不包括的内容 复选框只需要很少的屏幕区域,而一组JRadioBox、JCheckBox、JList占据的空间比较大

58 原子组件(续) ——ComboBoxDemo.java
Swing 组件 说明 以一个字符串数组为参数创建了一个JComboBox对象 为该对象添加ActionListener事件监听器,当选择不同动物名称时,显示不同的动物图片

59 原子组件(续) ——第二类原子组件:连续数值选择
原子组件(续) ——第二类原子组件:连续数值选择 Swing 组件 连续数值选择 JSlider 空间大 好像电器用品上机械式的控制杆,我们可以设置它的最小、最大、初始刻度,还可以设置它的方向,还可以为其标上刻度或文本 在JSlider上移动滑动杆,会产生ChangeEvent事件 JSpinner 空间小 类似于可编辑的JComboBox,是种复合组件,由三个部分组成:向上按钮、向下按钮和一个文本编辑区 可以通过按钮来选择待选项,也可以直接在文本编辑区内输入 和JComboBox不同的是,它的待选项不会显示出来

60 原子组件(续) ——第二类原子组件:文本组件
原子组件(续) ——第二类原子组件:文本组件 Swing 组件 文本组件 允许用户在里边编辑文本,能满足复杂的文本需求 都继承自JTextComponent抽象类,可分为三类 JTextField、JPasswordField、JFormattedTextField 它们只能显示和编辑一行文本,像按钮一样,它们可以产生ActionEvent事件,因此通常用来接受少量用户输入信息并在输入结束进行一些事件处理 JTextArea 它可以显示和编辑多行文本,但是这些文本只能是单一风格的,因此通常用来让用户输入任意长度的无格式文本或显示无格式的帮助信息 JEditorPane、JTextPane 可以显示和编辑多行多种式样的文本,嵌入图像或别的组件

61 原子组件(续) ——例11_10 Swing 组件 在第一个窗口输入用户名和密码,要求输入密码使用JPasswordField组件,密码正确后,才能进入第二个窗口进行“+、-、×、÷”计算。运算符号使用单选按钮JRadioButton,操作数使用JcomboBox和Jspinner组件

62 原子组件(续) ——第三类原子组件 Swing 组件 JColorChooser颜色选择对话框 可以让用户选择所需要的颜色
原子组件(续) ——第三类原子组件 Swing 组件 JColorChooser颜色选择对话框 可以让用户选择所需要的颜色 通常使用这个类的静态方法showDialog()来输出标准的颜色选择对话框,其返回值就是选择的颜色 也可以通过静态方法createDialog()方式输出个性化的颜色选择对话框,例如为其添加菜单、定义其事件处理程序,这个方法的返回值就是一个对话框

63 原子组件(续) ——第三类原子组件 Swing 组件 JFileChooser文件选择对话框 让用户选择一个已有的文件或者新建一个文件
原子组件(续) ——第三类原子组件 Swing 组件 JFileChooser文件选择对话框 让用户选择一个已有的文件或者新建一个文件 可以使用JFileChooser的showDialog、showOpenDialog或showSaveDialog()方法来打开文件对话框,但是它仅仅会返回用户选择的按钮(确认还是取消)和文件名(如果确认的话),接下来的要实现的例如存盘或者打开的功能还需要程序员自己编写 这个类提供了专门的方法用于设置可选择的文件类型,还可以指定每类文件使用的类型图标

64 原子组件(续) ——第三类原子组件 Swing 组件 JTable表格
原子组件(续) ——第三类原子组件 Swing 组件 JTable表格 和其相关的还有一些接口和类,包括TableModel、AbstractTableModel、DefaultTableModel、SelectionModel、TableColumnModel 可为表格设计显示外观(是否有滚动条、调整某一列宽其它列宽变化情形)、显示模式(根据数据类型的不同有不同的排列显示方式、为某一字段添加复选框JComboBox)、选择模式(单选、多选、连续选、任意选) 在JTable的事件大致均针对表格内容的异操作处理,我们称为TableModelEvent事件。通过addTableModelListener方法为表格添加此种事件监听器

65 原子组件(续) ——第三类原子组件 Swing 组件 JTree 用来产生树状结构来直观地表现层次关系,有根节点、树枝节点、树叶节点
原子组件(续) ——第三类原子组件 Swing 组件 JTree 用来产生树状结构来直观地表现层次关系,有根节点、树枝节点、树叶节点 构造方法有多种,参数可以是一个Hashtable,也可以是TreeNode或TreeModel对象 可以使用JComponent提供的putClientProperty方法来设置JTree外观,也可以使用TreeCellRenderer来个性化各类节点的显示样式

66 很多Swing组件都要使用一些相同的特性,包括 Action对象 边框 设置外观和感觉 应用线程 定时器

67 11.4.1 Action对象 其它Swing特性 Action的用途
Action接口是对ActionListener接口的一个有用的扩展,它的继承关系如下 public interface Action extends ActionListener 在很多既有菜单又有工具栏的应用程序中,我们可以看到某条菜单和工具栏的功能是相同的,按照前面所讲的方法,我们需要为每个组件一一添加事件监听器,并在其处理程序中写入相同的代码,程序就会显得比较繁琐。在这种场合,可以考虑使用Action对象实现此功能 还可以通过它对不同组件的显示文字、图标、快捷键、提示文字、是否可用等属性进行统一的设置

68 11.4.1 Action对象(续) ——创建Action对象
其它Swing特性 创建Action对象 通常我们首先需要创建一个继承抽象类AbstractAction类的子类,然后再实例化这个子类 AbstractAction抽象类提供了Action接口的默认实现,而且还提供了一些获取和设置Action域属性的方法。我们在继承它创建自己需要的子类的时候,只需要通过这些方法 设置需要的属性值 定义actionPerformed方法

69 11.4.1 Action对象(续) ——使用Action对象
其它Swing特性 使用Action对象 可通过GUI组件的setAction方法把Action关联到此GUI组件。每个具有addActionListener方法的组件也都具有setAction方法 但setAction方法并不是addActionLilstener方法的替换:Action是一个事件监听器,如果除此之外还需要添加别的监听器,则应该使用addActionListener方法 对给定的GUI组件,你可以调用setAction不止一次,但组件和上一个Action对象之间的关联会被删除

70 11.4.1 Action对象(续) ——使用Action对象的效果
其它Swing特性 通过setAction方法把Action对象关联到某GUI组件后,会有如下效果 此组件的属性会被升级到符合这个Action对象的属性,例如如果设置了Action对象的文字和图标值,那么组件的文字和图标也会被重新设置成同样的值。 这个Action对象会被注册为此组件的一个事件监听器对象 如果改变了Action对象的属性或方法,则所有和它关联的组件的使能状态也会发生相应改变

71 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.*;

72 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);

73 11.4.1 Action对象(续) ——ActionDemo.java
其它Swing特性 //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); }

74 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

75 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

76 11.4.1 Action对象(续) ——ActionDemo.java
其它Swing特性 //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

77 11.4.1 Action对象(续) ——ActionDemo.java
其它Swing特性 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;

78 11.4.1 Action对象(续) ——ActionDemo.java
其它Swing特性 protected void displayResult(String actionDescription, ActionEvent e) { String s = ("Action event detected by: " + actionDescription + newline + " Event source: " + e.getSource() + newline); textArea.append(s); } public static void main(String[] args) { ActionDemo frame = new ActionDemo(); frame.pack(); frame.setVisible(true);

79 11.4.1 Action对象(续) ——ActionDemo.java
其它Swing特性 说明 创建了三个Action对象,在每个对象中定义了文本、图片等属性以及actionPerformed方法 分别为三组按钮、菜单分别添加三个Action对象,使组内的按钮、菜单具有相同的功能 创建了三个复选框菜单选项,在其事件监听器ItemListener()中改变Action对象的enabled属性,因此可以同时改变按钮、菜单的使能状态

80 11.4.2 边框 其它Swing特性 边框(Border) 每个继承自JComponent的Swing组件都可以有若干个边框 边框可以用来
绘制线条 为组件提供标题 为组件提供周边的空白区域 我们使用组件的setBorder方法为组件添加边框。这个方法需要提供一个Border类型的对象 通常可以使用BorderFactory类提供的很多静态方法产生一个常用的Border对象 如果不能够满足要求,可以直接使用javax.swing.border包里的API来定义自己的边框

81 11.4.2 边框(续) ——有关border的常用API
其它Swing特性 名称 说明 BorderFactory类里的静态方法,返回类型都是Border 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() 创建一种边框,给人感觉组件比周围高

82 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) 创建一个不光滑的边框,四个整数参数指定了边框在四个边上占据的象素数,颜色参数指定了边框的颜色,图标参数指定了填充边框的图标

83 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中。

84 11.4.1 Action对象(续) ——BorderDemo.java
其它Swing特性 说明 创建了四个JPanel组件,放在一个JTabbedPane中间容器中 在每个JPanel组件中,又创建了若干个JPanel组件,并为其 通过setBorder方法制定不同的边框 通过一个JLabel组件显示出对该种边框的描述

85 设置外观和感觉 其它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的操作系统下使用

86 11.4.3 设置外观和感觉(续) ——为组件进行设置 其它Swing特性
设置外观和感觉(续) ——为组件进行设置 其它Swing特性 我们需要在产生任何可视组件以前就设置好它们的外观和感觉,要设置某种外观和感觉,必须使用UIManager类所提供的setLookAndFeel()静态方法,这个方法的参数为上面提到的类名字符串之一。通常我们只会做两种选择 java提供的跨平台的外观和感觉。可以利用UIManager类提供的getCrossPlatformLookAndFeelClassName()静态方法获得类名 程序所处系统的外观和感觉。可以利用UIManager类提供的getSystemLookAndFeel()静态方法获得目前操作平台的Look and Feel类名称字符串

87 11.4.3 设置外观和感觉(续) ——为顶层容器进行设置
设置外观和感觉(续) ——为顶层容器进行设置 其它Swing特性 前述设置不能影响顶层容器JFrame和JDialog,因为它们是重量级组件,均是依赖于操作系统(Operating System dependent)的,当使用的操作系统不同时,所显示的顶层容器就会有所不同。针对这两个顶层容器,有一个静态方法专门为其设置外观感觉 static void setDefaultLookAndFeelDecorated(boolean) 参数是true,就会选用默认的外观感觉 参数是false,就会选用操作平台的外观感觉

88 设置外观和感觉(续) 其它Swing特性 Java 外观和感觉 CDE/Motif外观和感觉 Windows外观和感觉

89 应用线程 其它Swing特性 一个Swing组件被创建出来后,应该只有事件处理的线程可以访问它。别的线程最好应该不去调用关于该组件的方法。否则有可能造成死锁 在某些情况下应该使用线程 GUI界面初始化中有一个很耗时的任务要完成(比如复杂的计算、网络堵塞、磁盘I/O),我们可以把这个任务放到主线程之外进行,这样界面就会出现得比较快 点击一个按钮进入一段事件处理程序中,而这段程序本身很费时间,如果这段程序不结束,GUI就无法响应别的事件,解决办法就是把这段事件处理程序放到另外一个线程中,例如一个后台线程 定期重复执行某项操作 等待从其它程序传来的消息

90 11.4.4 应用线程(续) ——例11_11 其它Swing特性
应用线程(续) ——例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);

91 11.4.4 应用线程(续) ——例11_11 其它Swing特性
应用线程(续) ——例11_11 其它Swing特性 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); public void update(Graphics g) {.drawLine(0,0,x,y);} public static void main(String[] args) { new Ex11_11(); }

92 11.4.4 应用线程(续) ——例11_11运行结果 其它Swing特性 说明
应用线程(续) ——例11_11运行结果 其它Swing特性 说明 为标签添加MouseAdapter鼠标事件监听器,对鼠标点击事件做出响应 通过MouseEvent事件对象可获得鼠标点击的坐标,调用repaint方法后,该方法又会自动调用update方法重绘GUI 为按钮添加ActionListener事件监听器,鼠标点击后进入死循环,不能继续对点击标签事件做出响应了

93 11.4.4 应用线程(续) ——例11_12 其它Swing特性 多线程的绘图程序:修改上例,使点击按钮后虽然进入无限循环,仍可以画线
应用线程(续) ——例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(); //重绘组件 } });

94 11.4.4 应用线程(续) ——例11_12 其它Swing特性
应用线程(续) ——例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);}

95 11.4.4 应用线程(续) ——例11_12运行结果 其它Swing特性
应用线程(续) ——例11_12运行结果 其它Swing特性 在对按钮的ActionListener事件监听器的事件处理方法actionPerformed方法中,将无限循环放到了另外一个线程中运行,这样就不会影响对标签的鼠标点击事件做出响应了

96 11.4.5 定时器 其它Swing特性 如果需要让程序按照一定时间顺序依次完成某项任务,就需要使用Timer组件
其完整的类名是javax.swing.Timer Timer组件会根据程序员设定的间隔时间,周期性的触发ActionEvent事件 这个类提供了一系列API用于对定时器的各种操作,例如启动、关闭、设置间隔时间、设置是否只使用一次等

97 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 获得定时器是否在运行

98 11.4.5 定时器(续) ——AnimatorApplicationTimer.java
其它Swing特性 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);

99 11.4.5 定时器(续) ——AnimatorApplicationTimer.java
其它Swing特性 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);

100 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(); } public void stopAnimation() { //Stop the animating thread. if (timer.isRunning()) { timer.stop();

101 11.4.5 定时器(续) ——AnimatorApplicationTimer.java
其它Swing特性 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();

102 11.4.5 定时器(续) ——AnimatorApplicationTimer运行结果
其它Swing特性 说明 为窗口通过addWindowListener方法添加了事件监听器,窗口最小化时停止Timer,窗口恢复时重新启动Timer 为标签通过addMouthListener方法添加了事件监听器,在标签上点击时可以交替暂停和启动Timer Timer组件会根据程序员设定的间隔时间,周期性的触发ActionEvent事件,在ActionEvent中让标签上显示的信息递增


Download ppt "问题 Jdbc连接数据库 下载数据库的驱动 Sql server 在微软的网站上可以下载 Sqlserver for jdbc driver"

Similar presentations


Ads by Google