第图形用户界面设计.ppt
《第图形用户界面设计.ppt》由会员分享,可在线阅读,更多相关《第图形用户界面设计.ppt(66页珍藏版)》请在三一办公上搜索。
1、1,概述事件处理基本控制组件布局设计常用容器组件,第五章 图形用户界面设计,2,概述,用户界面(User Interface),用户与计算机系统(各种程序)交互的接口,3,图形用户界面的构成,什么是组件?构成图形用户界面的元素,拿来即用用图形表示(能在屏幕上显示,能和用户进行交互)Button、Checkbox、Scrollbar、Choice、Frame,4,图形用户界面的构成,一些特定的Java类java.awt包javax.swing包容器组件(Container):可包含其他组件顶层容器:Applet,Dialog,Frame,Window一般用途容器:Panel,ScrollPane
2、特定用途容器:InternalFrame非容器组件:必须要包含在容器中Button,Checkbox,Scrollbar,Choice,Canvas,5,图形用户界面的构成,AWT组件 java.awt包,6,图形用户界面的构成,Swing组件 javax.swing包java.awt.Component|-java.awt.Container|-java.awt.Window|-java.awt.Frame|-javax.swing.JFramejava.awt.Component|-java.awt.Container|-javax.swing.JComponent|-JComboBox,
3、JFileChooser,JInternalFrame JLabel,JList,JMenuBar,JOptionPane,JPanel JPopupMenu,JProgressBar,JScrollBar JScrollPane,JSeparator,JSlider,JSpinner JSplitPane,JTabbedPane,JTable JTextComponent,JToolBar,JTree等,7,图形用户界面的实现,选取组件设计布局响应事件应用原则Swing比AWT提供更全面、更丰富的图形界面设计功能Java 2平台支持AWT组件,但鼓励用Swing组件主要讲述AWT和Swing
4、的图形界面设计,8,图形用户界面的实现,简单实例,import java.awt.*;import 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.setSi
5、ze(200,200);f.setVisible(true);,import javax.swing.*;import java.awt.event.*;public class HelloWorldSwing 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(Wi
6、ndowEvent e)System.exit(0););f.setSize(200,200);f.setVisible(true);,9,概述事件处理基本控制组件布局设计常用容器组件,第五章 图形用户界面设计,10,事件处理,界面设计(静态)界面动起来!通过事件触发对象的响应机制事件?鼠标移动、鼠标点击、键盘键入等事件处理机制事件源事件对象事件监听者如何实现实现(implements)事件监听接口(interface)产生一个监听器对象(Listener)监听谁?将该监听器对象注册到组件对象中编写事件响应方法,11,事件处理,import javax.swing.*;import java.
7、awt.*;import java.awt.event.*;public class 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(“Clic
8、k me once”);,java.awt.event.ActionListener(interface)public void actionPerformed(ActionEvent e)javax.swing.JButton(class)public void addActionListener(ActionListener l),12,事件处理,事件分类,Act that results in the event Listener type User clicks a button,presses Return while typing in a text field,or choose
9、s a menu item ActionListener 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 FocusList
10、ener Table or list selection changes ListSelectionListener,13,事件处理,事件分类P205interface 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(W
11、indowEvent e)public void windowIconified(WindowEvent e)public void windowDeiconified(WindowEvent e)public void windowActivated(WindowEvent e)public void windowDeactivated(WindowEvent e),14,事件处理,事件分类interface java.awt.event.MouseListenerpublic void mouseClicked(MouseEvent e)public void mousePressed(M
12、ouseEvent 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
13、(MouseEvent e)Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed,15,事件处理,鼠标事件,public class MouseEventDemo.implements MouseListener./Register for mouse events on blankArea(TextArea)and applet blankArea.addMouseListener(this);public void mousePressed(MouseEve
14、nt 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)saySomething(Mouse exited,e);
15、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);,16,事件处理,多个监听器(Listener)多个组件,public class MultiLi
16、stener.implements ActionListener.button1.addActionListener(this);button2.addActionListener(this);button2.addActionListener(new Eavesdropper(bottomTextArea);public void actionPerformed(ActionEvent e)topTextArea.append(e.getActionCommand()+newline);class Eavesdropper implements ActionListener.public v
17、oid actionPerformed(ActionEvent e)myTextArea.append(e.getActionCommand()+newline);,17,第五章 图形用户界面设计,概述事件处理基本控制组件布局设计常用容器组件,18,AWT组件(java.awt.*),Component,Button,Canvas,Choice,CheckBox,Label,List,TextComponent,Scrollbar,TextField,TextArea,Container,ScrollPane,Frame,FileDialog,Panel,Window,Dialog,Apple
18、t,19,基本控制组件,使用步骤:创建基本控制组件类的对象,指定对象属性;将组件对象加入到指定容器的适当位置(布局设计);创建事件对象的监听者。Swing组件(javax.swing.*),20,按钮和标签,按钮(Button)(P207)创建按钮public Button()public Button(String label)常用方法public String getLabel()public void setLabel(String label)public void setActionCommand(String s)public String getActionCommand(Str
19、ing s)事件响应java.awt.event.ActionListener(接口)void actionPerformed(ActionEvent e),21,按钮和标签,标签(Label)(P207)创建标签public Label()public Label(String s)public Label(String s,int alignment)Label_LEFT、Label_CENTER Label_RIGHT常用方法public String getText()public void setText(String s)public void setAlignment(int a
20、lignment)事件响应不引发事件,22,使用按钮的例子,import java.awt.*;import java.applet.*;import java.awt.event.*;public class Exam5_1 extends Applet implements ActionListenerprivate Button But1,But2;int button=0;public void init()But1=new Button(“按钮1”);But2=new Button(“按钮2”);add(But1);add(But2);But1.addActionListener(t
21、his);But2.addActionListener(this);public void paint(Graphics g)if(button=1)g.drawString(“您按了按钮1”,10,20);,else if(button=2)g.drawString(“您按了按钮2”,10,20);public void actionPerformed(ActionEvent e)if(e.getActionCommand().equals(“按钮1”)button=1;else if(e.getActionCommand().equals(”按钮2”)button=2;repaint();
22、,23,使用按钮的例子,24,使用标签的例子,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(Color.red);lab2.setBackground(Color.green);text1=new TextField(10);text2=new Text
23、Field(10);add(lab1);add(text1);add(lab2);add(text2);,25,使用标签的例子,26,文本框和文本区,文本框(TextField)(P209)TextComponent类的子类创建文本框public TextField()public TextField(int size)public TextField(String s)public TextField(String s,int size)常用方法public void setText(String s)public String getText()public void setEchocha
24、r(char c)public void setEditable(boolean b)事件响应java.awt.event.addTextListener(接口)void textValueChanged(TextEvent e)java.awt.event.addActionListener(接口)void actionPerformed(ActionEvent e),27,文本框和文本区,文本区(TextArea)(P210)TextComponent类的子类创建文本区public TextArea()public TextArea(String s)public TextArea(int
25、 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 index)pubilc void replaceRange
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 图形 用户界面 设计

链接地址:https://www.31ppt.com/p-2697398.html