基本图形用户界面构件.doc_第1页
基本图形用户界面构件.doc_第2页
基本图形用户界面构件.doc_第3页
基本图形用户界面构件.doc_第4页
基本图形用户界面构件.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

基本图形用户界面构件一:GUI(图形用户界面)的基本概念:统一用户界面:窗口、菜单、文本区、标签、按钮等、对话框等。二:JAVA中的构件设计:1:采用java.awt.*(abstract Windowing Toolkit)2:采用javax.swing.*特点:前者:它们直接系在本地平台的图形用户界面上,不同平台有不同的界面。后者:可以设计统一的图形用户界面,而且可以在程序运行过程中改变外观和风格。它比java.awt中的构件具有更大的可移植性和灵活性。具体表现:快捷键、tool tips功能(提示)辅助技术支持(盲人阅读)等。Javax.swing.*是相应的java.awt.*的子类。三:JAVA中的构件设计的所用到的基本类的层次结构:java.awt.*中的层次结构java.awt.Buttonjava.awt.Checkboxjava.awt.Choicejava.awt.Labeljava.awt.Listjava.awt.Scrollbarjava.awt.TextComponentJava.lang.objectJava.awt.ComponentJava.awt.ContainerJava.awt.PanelJava.awt.windowAppletDialogFrameComponent类:封装了可视构件的所有属性的抽象类,且定义了近100个用于事件管理的公共方法。包括mouse, 键盘等输入Container类:是Component的子类,主要作用是设计它所包含的组件的位置。主要通过设计管理器来实现。容器可以容纳组件,也可容纳其它容器。组件类:组件加入容器才能显示,组件的大小由容器管理器来决定。Frane类:带有标题框的不可见的对象,用setVisible()方法使之可见。四:例import java.awt.*; class myframe extends Frame private Button b1,b2,b3,b4,b5;myframe(String str)super(str);/this.setLayout(newb1=new Button(hello);b2=new Button(welcome);b3=new Button(hello);b4=new Button(welcome);b5=new Button(Center);this.add(b1,East);this.add(b2,South);this.add(b3,West);this.add(b4,North);add(b5);this.setSize(400,200);this.setVisible(true);class wcbpublic static void main(String args)myframe fr=new myframe(hello);五:几种布局器FlowLayout: Panel类和Applet类的缺省布局管理器BorderLayout:Window类、Dialog类缺省布局管理器GridLayoutCardLayoutGridBagLayout例:FlowLayout,也可用其它构造方式(LEFT、RIGHT、import java.awt.*; class myframe extends Frame private Button b1,b2,b3,b4,b5;myframe(String str)super(str);this.setLayout(new FlowLayout();b1=new Button(hello);b2=new Button(welcome);b3=new Button(hello);b4=new Button(welcome);b5=new Button(Center);this.add(b1);this.add(b2);this.add(b3);this.add(b4);add(b5);this.setSize(400,200);this.setVisible(true);class wcbpublic static void main(String args)myframe fr=new myframe(hello);GridLayout布局器import java.awt.*; class myframe extends Frame private Button b1,b2,b3,b4,b5;myframe(String str)super(str);this.setLayout(new GridLayout(3,2);b1=new Button(hello);b2=new Button(welcome);b3=new Button(hello);b4=new Button(welcome);b5=new Button(Center);this.add(b1);this.add(b2);this.add(b3);this.add(b4);add(b5);this.setSize(400,200);this.setVisible(true);class wcbpublic static void main(String args)myframe fr=new myframe(hello);六:复杂的布局设计:容器中包含容器import java.awt.*; class myframe extends Frame private Button b1,b2,b3,b4,b5;private Panel p;myframe(String str)super(str);/this.setLayout(new GridLayout(3,2);b1=new Button(hello);b2=new Button(welcome);b3=new Button(hello);b4=new Button(welcome);b5=new Button(Center);p=new Panel();this.add(b1,West);this.add(b2,Center);this.add(p,North);p.add(b3);p.add(b4);p.add(b5);this.setSize(400,200);this.setVisible(true);class wcbpublic static void main(String args)myframe fr=new myframe(hello);四:javax.swing.*中的构件设计1:类层次结构:Java.awt.*构件:如ButtonClass JComponentjava.lang.ObjectFrame, Panel | +-java.awt.Component |JFrame, +-java.awt.Container | +-javax.swing.JComponentAll Implemented Interfaces: ImageObserver, MenuContainer, Serializable Direct Known Subclasses: AbstractButton, BasicInternalFrameTitlePane, Box, Box.Filler, JColorChooser, JComboBox, JFileChooser, JInternalFrame, JInternalFrame.JDesktopIcon, JLabel, JLayeredPane, JList, JMenuBar, JOptionPane, JPanel, JPopupMenu, JProgressBar, JRootPane, JScrollBar, JScrollPane, JSeparator, JSlider, JSpinner, JSplitPane, JTabbedPane, JTable, JTableHeader, JTextComponent, JToolBar, JToolTip, JTree, JViewport public abstract class JComponent JButtonextends Container implements Serializable2:设计原则:l JFrame为最底层,用户可以:class myframe extends JFrame;l JFrame组织构件(JComponent)与Frame有所不同。具体表现:n JFrame可以相应WINDOW的某些事件,如closed.n JFrame 的布局管理器的作法与Frame 不同,前者的思路:Container c;u c=getContentPane();u c.setLayout(new GridLayout(3,2);u b1=new Button(hello);u b2=new Button(welcome);u b3=new Button(hello);u b4=new Button(welcome);u b5=new Button(Center);u JPanel p=new JPanel();u /JTextField u c.add(b1);u c.add(b2);u c.add(p);u c.add(b3);3:事件模型:1:事件基本概念:层次模型:1.0已不用。委托模型:1.1版本后。GUI是事件驱动的。事件的基本概念:是事件类的对象,由GUI构件产生,具体描述事件的有关信息。如:点击Button, 在文本区输入文本,在menu中选择等。事件类在JAVA中的java.awt.event.*或javax.swing.event.*中定义java.awt Class AWTEventjava.lang.Object | +-java.util.EventObject | +-java.awt.AWTEventAll Implemented Interfaces: Serializable Direct Known Subclasses: ActionEvent, AdjustmentEvent, AncestorEvent, ComponentEvent, HierarchyEvent, InputMethodEvent, InternalFrameEvent, InvocationEvent, ItemEvent, TextEvent Class ComponentEventjava.lang.Object | +-java.util.EventObject | +-java.awt.AWTEvent | +-java.awt.event.ComponentEventAll Implemented Interfaces: Serializable Direct Known Subclasses: ContainerEvent, FocusEvent, InputEvent, PaintEvent, WindowEvent 2:处理事件:l 对产生事件的GUI构件,注册一事件侦听器,其语法如下:GUI对象. AddTypeListener(事件侦听器对象);如JButton jb1=new JButton(“cancel”);jb1.addActionListener(l);/注意:“l”是侦听器对象。l “l”对象实现侦听器接口的类的对象。l 侦听器接口:规定了处理事件的抽象方法。l 实现侦听器接口的类则必须对其作具体实现。l 当已注册事件侦听器的GUI构件侦听到某一事件发生时,自动调用事件处理程序,例:import java.awt.*;import javax.swing.*;import java.awt.event.*;class myframe extends JFrameprivate JButton jb1;myframe(String str)super(str);Container c;c=getContentPane();c.setLayout(new FlowLayout();jb1=new JButton(cancel);c.add(jb1);handle h1=new handle();/“h1”对象实现侦听器接口的类的对象。jb1.addActionListener(h1);this.setSize(400,200);class handle implements ActionListenerpublic void actionPerformed(ActionEvent e)System.exit(0);class wcbevent1public static void main(String args)myframe fr=new myframe(hello);fr.show();事件模型的优点:1:不注册,构件对事件不相应。2:由于侦听器接口只规定了处理事件的抽象方法,则不同用户可以对同一事件作出不同相应。通过实现该接口来实现。3:进一步讨论事件模型:1:每一GUI构件可以产生各不相同的事件,相对应的注册事件侦听器不同,侦听器接口也应不同,java中规定了以下主要的侦听器接口:ActionListener, AdjustmentListener, FocusListener, KeyListener, MouseListener, TextListener等。以上接口在java.awt.event包中。则构件要实现或处理不同事件,则需要注册不同事件侦听器,如:password=new JpasswordField(“输入密码”);handle h1=new handle();/“h1”对象实现侦听器接口的类的对象。password.addActionListener(h1);blod=new JRadioButton(“Blod”);italic= new JRadioButton(“italic”);handle1 h2=new handle1();/“h2”对象实现侦听器接口的类的对象。bold.addItemListener(h2);italic.addItemListener(h2);2:在一个Jframe中,有多个同类构件(按钮),且注册相同事件侦听器,但处理事件的方法肯定不同。如:private JButton jb1jb2;myframe(String str)super(str);Container c;c=getContentPane();c.setLayout(new FlowLayout();jb1=new JButton(新建);jb2=new JButton(打开文件);c.add(jb1); c.add(jb2);handle h1=new handle();/“h1”对象实现侦听器接口的类的对象。handle1 h2=new handle1();/“h2”对象实现侦听器接口的类的对象。jb1.addActionListener(h1); jb2.addActionListener(h2);. class handle implements ActionListenerpublic void actionPerformed(ActionEvent e)新建文件的方法体内容;class handle1 implements ActionListenerpublic void actionPerformed(ActionEvent e)打开文件的方法体内容;以上是处理同一类事件(如按钮事件)的一种模型。以下是改进型的:import java.awt.*;import javax.swing.*;import java.awt.event.*;class myframe extends JFrameprivate JButton jb1,jb2;myframe(String str)super(str);Container c;c=getContentPane();c.setLayout(new FlowLayout();jb1=new JButton(cancel);jb2= new JButton(okl);c.add(jb1); c.add(jb2);handle h1=new handle();/“h1”对象实现侦听器接口的类的对象。jb1.addActionListener(h1);jb2.addActionListener(h1);this.setSize(400,200);private class handle implements ActionListener/inner类public void actionPerformed(ActionEvent e)if(e.getSource()=jb1)JOptionPane.showMessageDialog(,null,cancel); if(e.getSource()=jb2)System.exit(0);class wcbevent2public static void main(String args)myframe fr=new myframe(hello);fr.show();3:不同的事件,要注册不同的事件侦听器,不同的事件侦听器接口,有不同处理事件的方法名,如:对于JButton等构件,发生的是ActionEvent事件,必须注册jb1.addActionListener(h1);“h1”是实现ActionListener接口的类的对象(handle h1=new handle();)private class handle implements ActionListener/inner类public void actionPerformed(ActionEvent e)。对于。;则。private class handle1 implements ItemListener/inner类public void itemStateChanged(ItemEvent e)见教材p114表81;4:有些事件侦听器接口,有许多抽象方法,如MouseListener 接口,定义了以下5个方法(包括mouse的许多动作),应用程序只需对有些动作作出相应,综合例1:import java.awt.*;import java.awt.event.*;import javax.swing.*;class myframe extends JFrame private JTextField t; private Font plainFont, boldFont, italicFont, boldItalicFont; private JRadioButton plain, bold, italic, boldItalic; private ButtonGroup radioGroup; myframe() super( RadioButton Test ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); t = new JTextField( Watch the font style change, 25 ); c.add( t ); / Create radio buttons plain = new JRadioButton( Plain, true ); c.add( plain ); bold = new JRadioButton( Bold, false); c.add( bold ); italic = new JRadioButton( Italic, false ); c.add( italic ); boldItalic = new JRadioButton( Bold/Italic, false ); c.add( boldItalic ); / register events RadioButtonHandler handler = new RadioButtonHandler(); plain.addItemListener( handler ); bold.addItemListener( handler ); italic.addItemListener( handler ); boldI

温馨提示

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

评论

0/150

提交评论