1 / 40

第 2 章 Java 的图形界面

第 2 章 Java 的图形界面. 孙焘. 1. 2. 3. 4. 包含 构件的 构件. 消息与事件. 外观的管理与控制. 使用 AWT 构件. 重点:. getFont, handleEvent,keyUp keyDown,mouseUp, mouseDown, mouseMove, mouseEnter, mouseExit, mouseDrag, repaint setFont,resize. action, disable,enable, getBackground, getForeground,hide,

thane
Download Presentation

第 2 章 Java 的图形界面

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第2章 Java的图形界面 孙焘

  2. 1 2 3 4 包含构件的构件 消息与事件 外观的管理与控制 使用AWT构件 重点:

  3. getFont, handleEvent,keyUp keyDown,mouseUp, mouseDown, mouseMove, mouseEnter, mouseExit, mouseDrag, repaint setFont,resize action, disable,enable, getBackground, getForeground,hide, inside,isEnable, isShowing,isVisible, locate,location, move,setBackground, setForeground,show, size 2.1 使用AWT构件

  4. 2.2 包含构件的构件----构件容器(container) 菜单 按钮 列表 窗口,对话框 container 另一个窗口 container

  5. 2.2 包含构件的构件----构件容器(container) 例:创建一个简单窗口 import java.awt.*; import java.applet.Applet; public class CountClick extends Applet { int CurrentMarks=0; public init() { Button b1=new Button(“按钮”); add.b1; } } 按钮

  6. 2.2 包含构件的构件----构件容器(container) • AWT使用Container类来定义最基本的构件容器,它有两个子类:Window类和Panel类. • 在Window类还有两个子类 • 1. 定义对话框,用Dialog子类; • Java还提了一个Dialog的子类---FileDialog, 用它生成文件对话框 • 2. 定义一般意义的窗口,用Frame类.

  7. 2.2 包含构件的构件----构件容器(container) • Panel干什么用的? • 使你更方便的组织你的构件,得到赏心悦目的布局 • Applet是Panel的子类,因此在小应用程序里可以直接加入构件,而一般的应用程序必须先定义构件容器. • 小应用程序在浏览器中所显示的区域就是Panel,所占的尺寸就是缺省得Panel尺寸.

  8. 2.2 包含构件的构件----构件容器(container) Component Container Panel Button Applet Menu Window Frame Textfield Dialog Checkbox FileDialog

  9. 2.3 消息与事件 • 2.3.1 什么是事件 • 用户用于交互而产生的键盘或鼠标动作. • 响应用户的动作称为处理事件. • 在类Event中,定义了所有的事件处理方法,小应用已经继承了这些方法. • import java.awt.Event; • Event在java.awt包中,无论哪个键按下或者释放,还是鼠标移动,按下鼠标或释放鼠标,AWT都会捕获,并产生一个事件. • 处理事件的核心是重写处理事件的方法 • 通用方法: • handEvent(Event evt, Object arg) • 专用方法: • MouseDown(Event evt, Object arg)

  10. int clickCount int key int id int modifiers Object target Event Object arg int x int y long when 2.3 消息与事件 • Event类的数据域

  11. MouseUp() MouseDown() Event MouseDrag() MouseMove() HandleEvent() MouseEnter() MouseExit() action() keyDown() KeyUp() 2.3 消息与事件 如果你不覆盖你要处理的方法,则缺省的方法返回一个假值,通知系统没有处理事件 。

  12. 2.3 消息与事件 • 2.3.2 鼠标的事件 • 鼠标事件的三个参数: • 1.事件的类型(是移动?托拽) • 2.是按下还是放开? • 3.鼠标的位置(x,y) • 方法的重写: • public boolean mouseDown(Event evt,int x,int y) • {….}

  13. 2.3 消息与事件 2.3.3 键盘事件 捕获的方法 keyDown(Event evt, int key) Event类的键常量 例:显示用户按下的字母键内容 • import java.applet.Applet;import java.awt.*; • { char Presskey; • public boolean keyDown(Event evt, int key) • { Presskey=(char)key; • repaint(); return true; • } • public void paint(Graphics g) • { g.drawString(Presskey,10,10); } • }

  14. 2.3 消息与事件 • 键盘事件处理通常包括:显示字符,光标移动 • 特殊键 public boolean keyDown(Event evt, int key) { switch(key) { case Event.F1: {….}; case Event.PGUP: {…} } } • 修正键 if(evt.shiftDown()) if(evt.controlDown());

  15. 确定 music sports 取消 art 2.3 消息与事件 • 凡是由构件产生的事件叫动作事件ACTION_EVENT,处理这类事件的方法是: action().

  16. int clickCount int key int id int modifiers Object target Event Object arg int x int y long when 2.3 消息与事件 • action(Event evt, Object arg) • evt.target:指明事件类型 • (button,checkbox,list,...)

  17. 2.3 消息与事件 • 判断组件类型(如是button或checkbox) • if(evt.target instanceof Button) • if(evt.target instanceof Checkbox) • 判断是哪多个同类组件中的哪一个 • if(evt.target==button1) • if(evt.target=button2) • 或者通过判断标签内容 • if(arg==“确定”) • if(arg==“取消”)

  18. 10 按钮 2.3 消息与事件 例: 记录按下按钮的次数,并显示出来. import java.awt.*; import java.applet.Applet; public class CountClick extends Applet { int CurrentMarks=0; public init() { Button b1=new Button(“按钮”); add.b1; } public boolean action(Event evt,Object arg) { CurrentMarks++; repaint(); return true; } public void paint(Graphics g) { g.drawString(" "+CurrentMarks,10,10);} }

  19. 方 2.3 消息与事件 • 2.3.4 Java响应鼠标事件画图 • 例:根据用户选择画图形 import java.awt.*; import java.applet.Applet; public class drawing extends Applet { boolean circlemark=true; public init() { Button b1=new Button(“园”); Button b2=new Button(“方”); add.b1; add.b2; }

  20. 2.3 消息与事件 • public void paint(Graphics g) • { if (circlemark) • g.filloval(10,10,30,30); • else • g.fillRect(20,20,40,40}; • } • public boolean action(Event evt,Object arg) • { if (evt.target==b1) circlrmark=true; • else circlrmark=false; • repaint(); return true; }

  21. 2.3 消息与事件 • 2.3.4 通用的事件处理程序 • handleEvent处理所有的小应用程序所接受的事件,由它将事件送给相对应的方法. • 让我们看一下handleEvent的缺省实现 • public boolean handleEvent(Event evt) { • switch(evt) { • case Event.MOUSE_ENTER: • return mouseEnter(evt,evt.x,evt.y); • case Event.MOUSE_EXIT: • return mouseExit(evt,evt.x,evt.y); • case Event.MOUSE_MOVE: • return mouseMove(evt,evt.x,evt.y); • case Event.KEY_ACTION_RELEASE: • return keyUp(evt,evt.key); • case Event.ACTION_EVENT: • return action(evt,evt.arg); • case Event.GOT_FOCUS: • return gotFocus(evt,evt.arg); • case Event.LOST_FOCUS: • return lostFocus(evt,evt.arg); • } • return false; • }

  22. 窗口 panel 按钮 2.3 消息与事件 当动作发生在按钮上时, 首先看按钮这个类有没有action方法,如果没有则看包含按钮的容器类(即panel)有没有action方法,如果没有事件就传递窗口, 如果没有就传递给 component的通用处理方法,如果程序中没有定义任何action方法,实际上事件最终不被处理

  23. Panel ok 2.3 消息与事件 窗口 class MyClass extends Frame { MyPanel p=new Mypanel(); add(p); Button b=new Button(exit); add(b); boolean action() {…….;} } exit class MyPanel extends Panel { MyBtn b=new MyBtn(“ok”); add(b); boolean action() {…….;} } class MyBtn extends Button{ boolean action() { …….; return true;} }

  24. 2.3 消息与事件 • 事件由包含层次由内向外传递. • 每个处理事件的方法要有一个返回值,通知是否继续向上传递 boolean action(Event evt, Object arg) { ……; return true; }

  25. 窗口获面板的监听器 窗口 panel 按钮的监听器 button 2.3 消息与事件

  26. 2.4 构件的应用实例--按钮 • Button类 • 功能:创建按钮事件 • 创建一个Button • Button myButton = new Button(str); • 将button放到窗口系统中: • add(new Button(“确定”); • add(new Button(“取消”); • Button类的常用方法 • getLabel setLabel 取消 确定

  27. 2.4 构件的应用实例--按钮 • 处理button产生的事件 • 例:创建一个按钮,每当按下它时,在屏幕显示文字(singlebutton.html) • 想一想: • 应该有哪些类? Button ,Font; • 应有哪些方法? init---建立button action---接受动作事件,调用repaint paint---显示文字

  28. 2.4 构件的应用实例--按钮 import java.awt.*;import java.applet.Applet; public class button extends Applet { Font font; Button b1; public void init() { font= newFont("TimesRoman",Font.BOLD,20); b1=new Button("push"); add(b1); setFont(font); }

  29. 2.4 构件的应用实例--按钮 • boolean action(Event evt,Object arg) • { y+=5; repaint(); • return true; • } • paint(Graphics g) • { g.drawString("Button”, 10,y); }

  30. 加入add 处理 响应action 创建 new 2.4 构件的应用实例--按钮 import java.awt.*;\\例:不在applet中的按钮 class ButtoninFrame { public static void main(String args[]) { Frame myframe=new Frame(); myframe.setTitle("Button in Frame"); myframe.resize(200,200); myframe.show(); Button b1=new Button("Button1"); Button b2=new Button("Button2"); myframe.add(b1);myframe.add(b2); } } 一般步骤: .

  31. file edit 2.4 构件的应用实例--menu • 创建菜单条 • mb=new MenuBar(); • setMenuBar(mb);(类Frame中的方法) • 创建菜单 • menu1=new Menu(“file”); • menu2=new Menu(“edit) • mb.add(menu1);mb.add(menu2);

  32. File edit New open Save Close 2.4 构件的应用实例--menu • 创建菜单项 • mi1=new MenuItem(“new”); • mi2=new MenuItem(“open”); • mi3=new MenuItem(“save”); • mi4=new MenuItem(“close”); • menu1.add(mi1); menu1.add(mi2);

  33. 2.4 构件的应用实例--menu 如何处理事件 public boolean action(Event e, Object arg) { if (e.target instanceof MenuItem) { MenuItem selected=(MenuItem)e.trget; tring s=selected.getLabel(); switch(s) { case “new”: ….; case “open”: ….; case “save”: ….; case “close”: ….; } } }

  34. A A 2.4 构件的应用实例--menu • 在处理菜单事件 时应该注意的事情是:判断层次 • MenuContainer uplevel;(定义一个菜单容器) • MenuItem target=(MenuItem)evt.target;(当前被选中的对象) • uplevel=target.getParent(); • strMessage=uplevel.getLabel()(得到上一级容器的标签)

  35. button1 button2 button3 button4 2.5 外观的管理与控制 • Panel类(面板) • 功能:容纳其他对象,安排合理布局 • 创建面板: • Panel myPanel=new Panel(); • add(myPanel); • 将面板作为容器: • mypanel.add(button)

  36. 中 东 西 南 2.5 外观的管理与控制 • BorderLayout类 • 功能:Applet分成五个区 • 创建 • setLayout(new BorderLayout()); • 将其他构件加入 • add(“East”, new Button(“东”); • add(“South”, new Button(“南”); • add(“West”, new Button(“西”); • add(“North”, new Button(“北”); • add(“Center”, new Button(“中”);

  37. 2.5 外观的管理与控制 • FlowLayout类 • 缺省的输出管理器 • GridLayout类 • GridLayout mylayout = new • GridLayout(3,3,0,0) • setLayout(); rows cols hspace vspace

  38. 2.5 外观的管理与控制 • GridBagLayout类和 • GridBagConstraints类 • 功能:借助于GridBagConstraints类,实现更灵活的外观管理 • 每个构件后都跟随一个GridBagLayout对象实体,来决定构件的外观. • 创建 • GridBagLayout myLayout=new • GridBagLayout();

  39. 2.5 外观的管理与控制 • GridBagConstraints类的约束条件gridwidth, gridheight, gridx, gridy, • weightx, weighty, ipadx, ipady, insets • fill及其设置 • GridBagConstraints.NONE • GridBagConstraints.HORIZONTAL • GridBagConstraints.VERTICAL • GridBagConstraints.BOTH • GridBagConstraints.RELATIVE

  40. 结束

More Related