Java语言程序设计(下)课件_第1页
Java语言程序设计(下)课件_第2页
Java语言程序设计(下)课件_第3页
Java语言程序设计(下)课件_第4页
Java语言程序设计(下)课件_第5页
已阅读5页,还剩271页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java语言程序设计马 皓1第1页,共276页。第四章 Applet及其应用Applet概述Applet类Applet程序与HTML文件Applet的应用2第2页,共276页。Applet概述Java程序的两种基本形式Java Application(应用程序),可独立运行Java Applet(小程序),嵌入在浏览器中运行介绍Applet的结构特点、实现方法、工作原理掌握Applet的编辑、编译和运行方法3第3页,共276页。一个Applet小程序的例子import java.applet.Applet;import java.awt.Graphics;public class Exam4_

2、1 extends Applet String str;public void init() str = “Here is an Applet”;public void paint(Graphics g) g.drawString(str, 100, 100);4第4页,共276页。HTML文件超文本标记语言(HTML)WWW浏览器Applet小程序嵌入在/写入在HTML文件中从WWW服务器下载到本地WWW浏览器由WWW浏览器中的Java解释器来运行5第5页,共276页。HTML文件实现过程Applet小程序编写,编译,得到字节码文件javac Exam4_1.java嵌入到HTML文件中,保

3、存为Exam4_1.html6第6页,共276页。HTML文件浏览器打开Exam4_1.html文件7第7页,共276页。Applet的特点通常作为Applet类的子类,格式如下:public class 类名 extends Applet 嵌入在HTML文件中,利用WWW浏览器或Appletviewer来运行利用了WWW浏览器或Appletviewer所提供的图形用户界面功能8第8页,共276页。Applet的工作原理Applet源程序字节码文件嵌入到HTML文件WWW浏览器打开 该HTML文件9第9页,共276页。第四章 Applet及其应用Applet概述Applet类Applet程序与

4、HTML文件Applet的应用10第10页,共276页。Applet类的继承关系java.lang.Objectjava.awt.Componentjava.awt.Containerjava.awt.Paneljava.applet.Applet默认情况下,Applet类使用FlowLayout布局管理器11第11页,共276页。Applet类的主要方法init()方法完成初始化操作在Applet程序第一次加载时调用,仅执行一次start()方法启动Applet主线程运行重启时也被调用(reload或返回)paint()方法将结果输出/绘制到界面上被自动调用(启动后/窗口改变/repaint

5、()调用)12第12页,共276页。Applet类的主要方法stop()方法暂停Applet程序执行destroy()方法终止Applet程序执行,释放所占用的资源13第13页,共276页。Applet类的主要方法import java.applet.Applet;import java.awt.*;public class Exam extends Applet public void init( ) /初始化Applet程序public void start( ) /启动Applet线程public void paint(Graphics g) /绘制输出显示信息public void s

6、top( ) /暂停线程public void destroy( ) /释放系统资源,结束线程14第14页,共276页。第四章 Applet及其应用Applet概述Applet类Applet程序与HTML文件Applet的应用15第15页,共276页。HTML文件超文本标记语言(HTML)和Html文件开始和结束的标记和WWW浏览器窗口标题内容的标记和Html文件在浏览器窗口中显示内容的标记和嵌入到Html文件中Applet程序的标记16第16页,共276页。Applet程序的标记参数17第17页,共276页。Applet小程序import java.applet.Applet;import

7、java.awt.*;public class Exam4_3 extends Applet String str;int x, y, h;Font fnt;public void init() str = getParameter(“string”);h = Integer.parseInt(getParameter(“size”);x = Integer.parseInt(getParameter(“x1”);y = Integer.parseInt(getParameter(“y1”);fnt = new Font(“TimesRoman”, Font.BOLD, h);public v

8、oid paint(Graphics g) g.setColor(Color.red);g.setFont(fnt);g.drawString(str, x, y);18第18页,共276页。Applet小程序19第19页,共276页。第四章 Applet及其应用Applet概述Applet类Applet程序与HTML文件Applet的应用20第20页,共276页。绘制图形设置字体java.awt.Font类设置文本的字体(包括字型和字号)构造方法public Font(String name, int style int size)设置颜色java.awt.Color类控制颜色,Color类

9、已包含13个颜色常量构造方法public Color(int r, int g, int b)public Color(float r1, float g1, float b1)21第21页,共276页。绘制图形绘制文本绘制字符串public void drawString(String s, int x, int y)绘制字符public void drawString(char c, int offset, int number int x, int y)绘制字节public void drawString(byte b, int offset, int number int x, int

10、 y)22第22页,共276页。Applet小程序import java.applet.Applet;import java.awt.*;public class Exam4_4 extends Applet public void paint(Graphics g) Font font1, font2, font3;font1 = new Font(“Serif”, Font.BOLD, 20);font2 = new Font(“Monospaced”, Font.BOLD+Font.ITALIC, 24);font3 = new Font(“SansSerif”, Font.PLAIN,

11、 16);g.setFont(font1);g.drawString(“Serif 20 point BOLD”, 30, 20);g.setFont(font2);g.drawString(“Monospaced 24 point BOLD + ITALIC”, 30, 80);g.setFont(font3);g.drawString(“SansSerif 16 point PLAIN”, 30, 50);int size = font2.getSize();int style = font1.getStyle();String name = font2.getName();String

12、str = name + “ “ + style + “ “ + size;g.drawString(str, 30, 110);23第23页,共276页。Applet小程序24第24页,共276页。Applet小程序import java.applet.Applet;import java.awt.*;public class Exam4_6 extends Applet Font font1 = new Font(“TimesRoman”, Font.ITALIC, 25);Font font2 = new Font(“Braggadcoio”, Font.BOLD, 40);public

13、 void paint(Graphics g) String str = “I love Beijing!”;Color mycolor = new Color(192, 64, 200);g.setFont(font1);g.setColor(mycolor);g.drawString(str, 30, 40);Color darker = mycolor.darker();g.setColor(darker);g.drawString(str, 50, 80);Color brighter = mycolor.brighter();g.setColor(brighter);g.drawSt

14、ring(str, 70, 120);g.setFont(font2);g.setColor(Color.red);g.drawString(str, 30, 170);g.setColor(Color.white);g.drawString(str, 32, 169);25第25页,共276页。Applet小程序26第26页,共276页。绘制图形绘制几何图形画直线void drawLine(int x1, int y1, int x2, int y2)画矩形void drawRect(int x, int y, int width, int height)void fillRect(int

15、x, int y, int width, int height)void clearRect(int x, int y, int width, int height)void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)void draw3DRect(int x, int y, int width, int height, boolean b)void fill3DRect(int x, int y, int width, int height, boolean b)27第27页,

16、共276页。绘制图形绘制几何图形画圆弧和椭圆void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)void drawOval(int x, int y, int width, int height)void fillOval(int x, int y, int width, int height)28第28页,共276页。Applet小

17、程序import java.applet.Applet;import java.awt.*;public class Exam4_8 extends Applet public void paint(Graphics g) int x0 =10, y0=20, X=150, Y=80, L, c;int arc = 0, N=10;double xy=1.0*(X-Y)/N/2;g.setColor(Color.red);g.fillRect(x0, y0, X, X);for(int i=0; i =N; i+) L=(int)(X-2*i*xy); arc=Y*i/N; c=i*240/N

18、; g.setColor(new Color(c, c, c); g.drawRoundRect(int)(x0+i*xy), (int)(y0+i*xy), L, L, arc, arc);g.setColor(Color.blue);g.draw3DRect(200, y0, X, X, true);g.setColor(Color.green);g.fill3DRect(400, y0, Y, Y, false);29第29页,共276页。Applet小程序30第30页,共276页。绘制图形绘制几何图形画多边形public void drawPolygon(int xPoints, in

19、t yPoints, int Points)public void fillPolygon(int xPoints, int yPoints, int Points)public void drawPolygon(Polygon p)public void fillPolygon(Polygon p)Polygon类构造方法Polygon()Polygon(int xPoints, int yPoints, int numberOfPoints)31第31页,共276页。演示图像定义图像对象java.awt.Image类图像高度和宽度int getHeight(ImageObserver ob

20、server)int getWidth(ImageObserver observer)获取图像信息方法Image getImage(URL url, String name)显示图像的操作drawImage(Image img, int x, int y, ImageObserver observer)drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)32第32页,共276页。Applet小程序import java.applet.Applet;import java.awt.*;p

21、ublic class Exam4_13 extends Applet public void paint(Graphics g) Image pic;pic = getImage(getDocumentBase(), “edonkey.jpg”);int x0 = 10, y0 = 30;int w = pic.getWidth(this);int h = pic.getHeight(this);g.drawImage(pic, x0, y0, w/8, h/8, this);g.drawImage(pic, x0+150, y0, w/12, h/12, this);g.drawImage

22、(pic, x0+150, y0+100, w/15, h/15, this);g.drawImage(pic, x0+250, y0+30, (int)(w*0.1), (int)(h*0.2), this);33第33页,共276页。Applet小程序34第34页,共276页。演示图像动画创建Image类的对象数组drawImage()Thread.sleep()方法repaint()播放声音public void play(URL url)public AudioClip getAudio(URL url)play()方法/loop()方法/stop()方法35第35页,共276页。第四

23、章 结束!36第36页,共276页。概述事件处理基本控制组件布局设计常用容器组件第五章 图形用户界面设计37第37页,共276页。概述用户界面(User Interface)用户与计算机系统(各种程序)交互的接口38第38页,共276页。GraphicalUser InterfaceNatural User Interface1990GUIMultiple WindowsMenus1995InternetHyperlinksSearch EnginesDigital DecadeXMLWeb ServicesSmart devicesNatural LanguageMultimodal (sp

24、eech, ink)Personal AssistantCommand line1985PCUser Interface Evolution- Kai Fu Lee in 200339第39页,共276页。概述Java GUI的发展AWT (Java 1.0)AWT (Abstract Window Toolkit): 抽象窗口工具包概念设计实现 (about 1 month)字体设计(四种), 界面显示(二流水准)Swing (Lightweight Components, Java 1.1)Swing was the code name of the project that develo

25、ped the new componentsSwing API (附加包, Add-on package)JFC (Java 2)JFC (Java Foundation Classes): Java基础类JFC encompass a group of features to help people build graphical user interfaces (GUIs). JFC 是指包含在 Java 2 平台内的一整套图形和用户界面技术JFC was first announced at the 1997 JavaOne developer conference 40第40页,共27

26、6页。概述JFC (Java Foundation Classes)AWT (Abstract Window Toolkit)一些用户界面组件 (Component)事件响应模型 (Event-handling model)布局管理器 (Layout manager)绘图和图形操作类, 如Shape、Font、Color类等Swing Components (Swing组件, JFC的核心)a set of GUI components with a pluggable look and feel (包括已有的AWT组件(Button、Scrollbar、Label等)和更高层的组件 (如tr

27、ee view、list box、tabbed panes等)The pluggable look and feel lets you design a single set of GUI components that can automatically have the look and feel of any OS platform (Microsoft Windows, Solaris, Macintosh).基于Java 1.1 Lightweight UI Framework41第41页,共276页。概述JFC (Java Foundation Classes)Java 2D (a

28、dvanced 2D graphics and imaging)Graphics?Imaging? Print Service打印文档、图形、图像设定打印属性和页面属性发现打印机 (IPP, Internet Printing Protocol)42第42页,共276页。概述JFC (Java Foundation Classes)Input Method Frameworktext editing components to communicate with input methods and implement a well-integrated text input user inter

29、face用Java语言开发输入法Accessibility: 辅助功能,帮助伤残人士screen readers, speech recognition systems, refreshable braille displaysDrag & DropDrag and Drop enables data transfer both across Java programming language and native applications, between Java programming language applications, and within a single Java pro

30、gramming language application. 43第43页,共276页。图形用户界面的构成什么是组件?构成图形用户界面的元素,拿来即用用图形表示(能在屏幕上显示,能和用户进行交互)Button、Checkbox、Scrollbar、Choice、Frame44第44页,共276页。图形用户界面的构成一些特定的Java类java.awt包javax.swing包容器组件(Container): 可包含其他组件顶层容器: Applet, Dialog, Frame, Window一般用途容器: Panel, ScrollPane特定用途容器: InternalFrame非容器组件:

31、 必须要包含在容器中Button, Checkbox, Scrollbar, Choice, Canvas45第45页,共276页。图形用户界面的构成AWT组件 java.awt包ComponentButton, Canvas, Checkbox, Choice, Label, List, ScrollbarTextComponentTextAreaTextFieldContainerPanelWindowScrollPaneDialogFrameMenuComponentMenuBarMenuItem46第46页,共276页。图形用户界面的构成Swing组件 javax.swing包java

32、.awt.Component |-java.awt.Container |-java.awt.Window |-java.awt.Frame |-javax.swing.JFramejava.awt.Component |-java.awt.Container |-javax.swing.JComponent|-JComboBox, JFileChooser, JInternalFrame JLabel, JList, JMenuBar, JOptionPane, JPanel JPopupMenu, JProgressBar, JScrollBar JScrollPane, JSeparat

33、or, JSlider, JSpinner JSplitPane, JTabbedPane, JTable JTextComponent, JToolBar, JTree等47第47页,共276页。图形用户界面的实现选取组件设计布局响应事件应用原则Swing比AWT提供更全面、更丰富的图形界面设计功能Java 2平台支持AWT组件,但鼓励用Swing组件主要讲述AWT和Swing的图形界面设计48第48页,共276页。图形用户界面的实现简单实例import javax.swing.*; import java.awt.event.*; public class HelloWorldSwing

34、public static void main(String args) JFrame f = new JFrame(“Swing1); JLabel label = new JLabel(Hello!); f.getContentPane().add(label); f.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); f.setSize(200, 200); f.setVisible(true); import java.awt.*; impor

35、t java.awt.event.*; public class HelloWorldAWT public static void main(String args) Frame f = new Frame(AWT1); Label label = new Label(Hello!); f.add(label); f.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); f.setSize(200, 200); f.setVisible(true); 4

36、9第49页,共276页。概述事件处理基本控制组件布局设计常用容器组件第五章 图形用户界面设计50第50页,共276页。事件处理界面设计 (静态)界面动起来 !通过事件触发对象的响应机制事件? 鼠标移动、鼠标点击、键盘键入等事件处理机制事件源事件对象事件监听者如何实现实现(implements)事件监听接口(interface)产生一个监听器对象(Listener)监听谁? 将该监听器对象注册到组件对象中编写事件响应方法51第51页,共276页。事件处理import javax.swing.*;import java.awt.*;import java.awt.event.*;public cl

37、ass Beeper extends JApplet implements ActionListener JButton button; public void init() button = new JButton(Click Me); getContentPane().add(button, BorderLayout.CENTER); button.addActionListener(this); public void actionPerformed(ActionEvent e) System.out.println(“Click me once”); java.awt.event.Ac

38、tionListener (interface)public void actionPerformed(ActionEvent e)javax.swing.JButton (class)public void addActionListener(ActionListener l)52第52页,共276页。事件处理事件分类Act that results in the event Listener type User clicks a button, presses Return while typing in a text field, or chooses a menu item Actio

39、nListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or lis

40、t selection changes ListSelectionListener 53第53页,共276页。事件处理事件分类interface java.awt.event.ActionListenerpublic void actionPerformed(ActionEvent e)interface java.awt.event.WindowListenerpublic void windowOpened(WindowEvent e)public void windowClosing(WindowEvent e)public void windowClosed(WindowEvent e

41、)public void windowIconified(WindowEvent e)public void windowDeiconified(WindowEvent e)public void windowActivated(WindowEvent e)public void windowDeactivated(WindowEvent e)54第54页,共276页。事件处理事件分类interface java.awt.event.MouseListenerpublic void mouseClicked(MouseEvent e)public void mousePressed(Mouse

42、Event e)public void mouseReleased(MouseEvent e)public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)interface java.awt.event.MouseMotionListenerpublic void mouseDragged(MouseEvent e)Invoked when a mouse button is pressed on a component and then draggedpublic void mouseMoved(Mou

43、seEvent e)Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed55第55页,共276页。事件处理鼠标事件public class MouseEventDemo . implements MouseListener . /Register for mouse events on blankArea(TextArea) and applet blankArea.addMouseListener(this); public void mousePressed

44、(MouseEvent e) saySomething(Mouse pressed; # of clicks: “ + e.getClickCount(), e); public void mouseReleased(MouseEvent e) saySomething(Mouse released; # of clicks: + e.getClickCount(), e); public void mouseEntered(MouseEvent e) saySomething(Mouse entered, e); public void mouseExited(MouseEvent e) s

45、aySomething(Mouse exited, e); public void mouseClicked(MouseEvent e) saySomething(Mouse clicked (# of clicks: “ + e.getClickCount() + ), e); void saySomething(String eventDescription, MouseEvent e) textArea.append(eventDescription + detected on “ + e.getComponent().getClass().getName() + . + newline

46、); 56第56页,共276页。事件处理多个监听器(Listener)多个组件public class MultiListener . implements ActionListener . button1.addActionListener(this); button2.addActionListener(this); button2.addActionListener(new Eavesdropper(bottomTextArea); public void actionPerformed(ActionEvent e) topTextArea.append(e.getActionComma

47、nd() + newline); class Eavesdropper implements ActionListener . public void actionPerformed(ActionEvent e) myTextArea.append(e.getActionCommand() + newline); 57第57页,共276页。第五章 图形用户界面设计概述事件处理基本控制组件布局设计常用容器组件58第58页,共276页。AWT组件 (java.awt.*)Component Button Canvas ChoiceCheckBoxLabelListTextComponentScro

48、llbarTextFieldTextArea ContainerScrollPaneFrameFileDialogPanelWindowDialogApplet59第59页,共276页。基本控制组件使用步骤:创建基本控制组件类的对象,指定对象属性;将组件对象加入到制定容器的适当位置(布局设计);创建事件对象的监听者。Swing组件(javax.swing.*)60第60页,共276页。按钮和标签按钮(Button)创建按钮public Button()public Button(String label)常用方法public String getLabel()public void setLa

49、bel(String label)public void setActionCommand(String s)public String getActionCommand(String s)事件响应java.awt.event.ActionListener(接口)void actionPerformed(ActionEvent e)61第61页,共276页。按钮和标签标签(Label)创建标签public Label()public Label(String s)public Label(String s, int alignment)常用方法public String getText()pu

50、blic void setText(String s)public void setAlignment(int alignment)事件响应不引发事件62第62页,共276页。使用标签的例子import java.awt.*;import java.applet.*;public class Exam5_3 extends Applet Label lab1, lab2;TextField text1, text2;public void init() lab1 = new Label(“输入姓名”);lab2 = new Label(“输入年龄”);lab1.setBackground(Co

51、lor.red);lab2.setBackground(Color.green);text1 = new TextField(10);text2 = new TextField(10);add(lab1); add(text1);add(lab2);add(text2);63第63页,共276页。使用标签的例子64第64页,共276页。文本框和文本区文本框(TextField)TextComponent类的子类创建文本框public TextField()public TextField(int size)public TextField(String s)public TextField(S

52、tring s, int size)常用方法public void setText(String s)public String getText()public void setEchochar(char c)public void setEditable(boolean b)事件响应java.awt.event.TextListener(接口)java.awt.event.ActionListener(接口)65第65页,共276页。文本框和文本区文本区(TextArea)TextComponent类的子类创建文本区public TextArea()public TextArea(Strin

53、g s)public TextArea(int rows, int columns)public TextArea(String s, int rows, int columns)public TextArea(String s, int rows, int columns, int scrollbars)SCROLLBARS_BOTH, SCROLLBARS_NONESCROLLBARS_VERTICAL_ONLYSCROLLBARS_HORIZONTAL_ONLY常用方法public void append(String s)public void insert(String s, int

54、 index)pubilc void replaceRange(String s, int start, int end)事件响应java.awt.event.TextListener(接口)void textValueChanged(TextEvent e)66第66页,共276页。使用文本框的例子import java.awt.*;import java.awt.event.*;import java.applet.*;public class Exam5_4 extends Applet implements ActionListenerLabel lab1, lab2, lab3;Te

55、xtField text1, text2, text3;String str; int i; float f;public void init() lab1 = new Label(“输入整形数: ”); add(lab1);text1 = new TextField(“0”, 30);text1.addActionListener(this); add(text1);lab2 = new Label(“输入浮点数: ”); add(lab2);text2 = new TextField(“0.0”, 30);text2.addActionListener(this); add(text2);

56、lab3 = new Label(“输入字符串: ”); add(lab3);text3 = new TextField(“0.0”, 30);text3.addActionListener(this); add(text3);67第67页,共276页。使用文本框的例子public void actionPerformed(ActionEvent e) i = Integer.parseInt(text1.getText();f = (Float.valueOf(text2.getText().floatValue();str = text3.getText();repaint();publi

57、c void paint(Graphics g) g.drawString(“整形数=” + i, 20, 120);g.drawString(“浮点数=” + f, 20, 150);g.drawString(“字符串=” + str, 20, 180);68第68页,共276页。单复选框和列表复选框(Checkbox)创建复选框public Checkbox()public Checkbox(String s)public TextField(String s, boolean state)常用方法public boolean getState()public void setState(

58、boolean b)public void setLabel(String s)public String getLabel()事件响应java.awt.event.ItemListener(接口)void itemStateChanged(ItemEvent e)69第69页,共276页。单复选框和列表单选按钮组(CheckboxGroup)创建单选按钮组public Checkbox(String label, boolean state, CheckboxGroup group)public Checkbox(String label, CheckboxGroup group, bool

59、ean state)常用方法与复选框相同事件响应与复选框相同70第70页,共276页。单复选框和列表列表(List)创建列表public List()public List(int n)public List(int n, boolean b)常用方法public void add(String s)public void add(String s, int n)public void remove(int n)public void removeAll()public int getSelectedIndex()public String getSelectedItem()事件响应java.

60、awt.event.ItemListener(接口)java.awt.event.ActionListener(接口)71第71页,共276页。下拉列表和滚动条下拉列表(Choice)创建下拉列表public Choice()常用方法public int getSelectedIndex()public String getSelectedItem()public void select(int index)public void select(String item)public void add(String s)public void add(String s, int index)pu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论