第5次上机题实验报告内容摘要:

yout (cardlayout1)。 Panel pnlFirst=new Panel( )。 ()。 ()。 (first,pnlFirst)。 (new Label(This is the first Panel) )。 Panel pnlSecond=new Panel( )。 ()。 ()。 (second,pnlSecond)。 (new Label(This is the second Panel) )。 Panel pnlThird=new Panel( )。 ()。 ()。 (third,pnlThird)。 (new Label(This is the third Panel) )。 Panel pnlFourth=new Panel( )。 ()。 ()。 (fourth,pnlFourth)。 (new Label(This is the fourth Panel) )。 (new ActionListener(){ public void actionPerformed(ActionEvent e) { processAction(e)。 } })。 (new ActionListener(){ public void actionPerformed(ActionEvent e) { processAction(e)。 } })。 (new ActionListener(){ public void actionPerformed(ActionEvent e) { processAction(e)。 }})。 (new ActionListener(){ public void actionPerformed(ActionEvent e) {processAction(e)。 } })。 //把四个功能按钮加入到 Panel ( btnFirst )。 ( btnPrevious )。 ( btnNext )。 ( btnLast )。 } public static void main( String[] args ) { //创建框架窗体的实例 CardLayoutDemo frmCardLayout= new CardLayoutDemo()。 //设置框架窗体的事件监听 (关闭窗体事件 ) (new WindowAdapter(){ public void windowClosing(WindowEvent e) {(0)。 } })。 //显示框架窗体 ()。 ()。 } public Dimension getPreferredSize() { return new Dimension(300,300)。 } //处理按钮的事件 private void processAction(ActionEvent e) { //获取事件源 ( 用户选择是哪个按钮 ) Button btnEvent=(Button)()。 if( (btnFirst)) ( pnlDisplayArea )。 else if( (btnLast)) ( pnlDisplayArea )。 else if( (btnPrevious)) ( pnlDisplayArea )。 else if( (btnNext)) ( pnlDisplayArea )。 } } 运行结果 GridLayout布局管理器 GridLayout 提供了一种基于网格的布局管理方式。 网格的行数和列数可以在创建 GridLayout 对象时指定。 另外,容器中每个组件将占据大小完全相同的一个网格。 向网格中布局 GUI组件有两种方法: 使用默认的顺序 采用 add(Component p,int index) 构造函数  GridLayout( )  GridLayout(int rows,int cols)  GridLayout(int rows,int cols, int hgap,int vgap) import .*。 import .*。 public class GridLayoutDemo extends Frame { public GridLayoutDemo( ) { ( new GridLayout(2,4))。 ( new Button(1) )。 ( new Button(2) )。 ( new Button(3) )。 ( new Button(4) )。 ( new Button(5) )。 ( new Button(6) )。 ( new Button(7) )。 ( new Button(8) )。 } public static void main( String[ ] args ) { GridLayoutDemo frmGridLayout=new GridLayoutDemo(。 (new WindowAdapter( ){ public void windowClosing(WindowEvent e) {(0)。 }})。 ( )。 ( )。 } } 运行结果 不使用布局管理器 import .*。 import .*。 public class NullLayoutDemo extends Frame { Label lblUserName = new Label(用户名称: )。 TextField txtUserName = new TextField( )。 Label lblUserPasswd = new Label(用户密码: )。 TextField txtUserPasswd = new TextField( )。 Button btnLogin = new Button(登录 )。 Button btnReset = new Button(重设 )。 public NullLayoutDemo( ) { (null)。 (new Rectangle(45, 38, 67, 34))。 (new Rectangle(115, 39, 210, 33))。 (new Rectangle(43, 86, 66, 26))。 (new Rectangle(115, 84, 210, 33))。 (39。 *39。 )。 (new Rectangle(78, 150, 86, 30))。 (new Rectangle(193, 150, 86, 30))。 (lblUserName)。 (txtUserName)。 (lblUserPasswd)。 (txtUserPasswd)。 (btnLogin)。 (btnReset)。 } public static void main( String[] args ) { NullLayoutDemo frmNullLayout=new NullLayoutDemo( )。 (new WindowAdapter( ){ public void windowClosing(WindowEvent e) {(0)。 }})。 ( )。 ( )。 } public Dimension getPreferredSize() { return new Dimension(355, 204)。 } } 运行结果 菜单组件 菜单组件包括菜单栏 (MenuBar)、菜单 (Menu)和菜单项 (MenuItem)三部分。 通常, MenuBar中包含一个或多个 Menu,每个 Menu又包含一个或多个 MenuItem。 创建菜单的基本步骤 创建 MenuBar 创建 Menu,并加入到 MenuBar中 创建 MenuItem,并加入到 Menu中 将 MenuBar加入到容器中 事件处理 Swing—— 特点  Swing是 100%的纯 Java,不依赖于具体的操作系统,可在各种平台上实现  Swing对很多 AWT组件进行了重写  Swing提供了可插入观感 (Pluggable look and feel)技术,可以让用户定制自己的桌面,以便更适合用户的习惯和需求 模型 视图 控制器体系 (modelviewcontroller)  维护数据并提供访问数据的方法  绘制和显示模型数据  处理用户与组件的交互。 必要时,更新模型或视图以响应用户的行为 模型 (组件 数据 ) 视图 (外观 ) 控制器 (响应 事件 ) 用户界面管理器 JComponent组件类 JComponent是所有 Swing组件的基类。 它增加了以下功能: 支持组件的可插入观感 支持工具提示 当组件被拖拽时,支持列表或树的自动滚动 支持扩展组件类,以创建自己需要的组件 JToggleButton组件  JToggleButton有称反转按钮,具有“选取”和“取消选取”两种状态,它是 JCheckBox和 JRadioButton的基类。  JToggleButton一般与 ButtonGroup一起搭配使用,组成具有相互排斥的选取组件。 构造函数  JToggleButton( )  JToggleButton(Icon)  JToggleButton( String)  JToggleButton(String,Icon )  JToggleButton( Icon,boolean)  JToggleButton(String,boolean )  JToggleButton(String,Icon,boolean ) JRadioButton组件  JRadioButton( )  JRadioButton( Icon)  JRadioButton( String)  JRadioButton( String,Icon)  JRadioButton( Icon,boolean)  JRadioButton( String,boolean)  JRadioButton( String,Icon,boolean) JPasswordField组件 //获取口令 内容 char[ ] getPassword( ) //将口令复制到缓冲区,但不清除组件中输入的口令 void copy( ) //将口令复制到缓冲区,并清除组件中输入的口令 void cut( ) char getEchoChar( ) //返回回显字符 void setEchoChar(char c) //设置回显字符 JProgressBar组件 进度条组件主要用来反应耗时任务完成的百分比,让用户知道任务完成的状态。 JFrame容器组件 JFrame类不能直接添加组件和使用布局管理器,必须获取一个内容窗格,在此窗格中添加组件或使用布局管理器。 Component Container Window Frame JFrame 第 8 章 事件处理 事件  事件是系统对其可能处于的某种状态或某种操作的预先定义。 例如:当 Quit菜单被选中时,事件生成且启用事件监听器进行处理。  事件监听器包括事件处理的代码。  几乎所有围绕 GUI进行的活动都会产生事件,但事件并不仅限于 GUI组件。  事件发生后,系统可以自动地启用相应的事件处理程序。 Java 采用基于 代理 (委托 )的事件模型,即产生事件的对象可以委托另外一个类对象对事件做出响应,其基本原理是:组件激发事件,事件监听器监听并进行处理。 事件处理的基本过程 事件源 系统事 件队列 dispatchEvent() processEvent() 调用特定的 事件处理方法 是否监听 放弃事件 分发事件 代理模型的三要素  事件:具体的事件类型  事件源:产生事件  事件监听器:处理事件 EventObject AWTEvent ActionEvent TextEvent ItemEvent ComponentEvent。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。