awt组件库(ppt130)-经营管理(编辑修改稿)内容摘要:

main(String args[]) { TextExample te = new TextExample(); (); } public void go() { f = new Frame(Text Example); ( new FlowLayout() ); tf1 = new TextField(Init,20); (this); (this); ta1 = new TextArea(Initial text,4,30); (this); ta2 = new TextArea(Only horizontal scrollbar , 4, 30, HORIZONTAL_ONLY); } (tf1); (ta1); (ta2); (300,300); (true); public void actionPerformed(ActionEvent e) { (\nActionPerformed); } public void textValueChanged(TextEvent e) { (\nTextValueChanged); } } 文本组件 虽然 TextArea和 TextField是两个不同的类,但是如果查阅名为 TextComponent类的文档,就会发现这个类定义了很多 TextArea类和 TextField类共有的方法,这是因为该类是 TextArea类和 TextField类的父类。 TextComponent类中定义的主要方法 getSelectedText( )从文本组件中提取被选中的文本内容。 getText( )从文本组件中提取所有文本内容。 select(int, int)在文本组件中选中部分内容。 selectAll( )在文本组件中选中所有内容。  setEditable(boolean)设置为可编辑或不可编辑状态。  setText(String)设置文本组件中的文本内容。 列表  列表( List) 是可供用户进行选择的一系列可选项,它支持 单项选择 和 多项选择。  三种构造方法:  public List( ) 构造一个单选列表。  public List(int rows) 构造一个显示指定项数的单选列表,其中 int型参数指定要显示的项数。  public List(int rows, boolean multipleMode) 构造一个显示指定项数的列表。 其中 int型参数指定要显示的项数, boolean型参数指定该列表是单选( false)列表还是多选( true)列表。 使用addItem( )方法向列表中加入可选项。 列表 例如: List l = newList(4, false); //这条命令构造一个显示 4项的单选列表。 列表  当用户单击列表中的可选项时,将引发ItemEvent事件,该事件需要ItemListener接口中的itemStateChanged()方法进行处理。  当用户双击列表中的可选项时,将引发ActionEvent事件,该事件需要由ActionListener接口中的actionPerformed()方法进行处理。 程序 98: 目标: 构造一个显示 5个可选项的单选列表,通过 ItemListener接口中的 itemStateChanged( )方法,用户的选择被显示在窗口底部。 import .*; import .*; public class ListExample extends Frame implements ItemListener{ Panel p; List theList; TextField tf; ListExample( String s ){ super( s ); } public static void main( String args[] ){ ListExample le = new ListExample (List Example ); (); } void init(){ // 构造一个显示 5个可选项的单选列表 theList = new List(5,false ); ( Monday ); ( Tuesday ); ( Wednesday ); ( Thursday ); ( Friday ); ( Saturday ); ( Sunday ); // 注册 ItemEvent事件监听程序 (this); p = new Panel(); (theList ); add(p,Center); tf = new TextField(); add(tf,South); setSize(300,200); setVisible(true); } // 实现 ItemListener接口中的方法 public void itemStateChanged(ItemEvent e) { (()); } } 框架  框架 是一种带 标题框 和 边框 并且 能够改变大小 的窗口。  两种构造方法: (1) Frame( ) 构造一个没有标题的框架。 (2) Frame(String) 构造一个显示指定标题的框架。 框架 例如: Frame f = new Frame(“Frame Example”); /*这条命令构造一个标题为“ Frame Example”的框架。 */ 框架 框架的默认布局管理器是 BorderLayout。 框架的大小可用 setSize( )方法设定,然而更常用的方法是使用 pack( )。 pack( )能够使布局管理器进行计算,得出一个适当的框架大小,使得框架以紧缩形式包容其中的组件。 框架 框架可对多种事件进行监听。 框架能够作为单独窗口使用。 当用户从窗口控制菜单中选择关闭窗口时,将引发 WindowEvent事件,可以用WindowListener接口中的 WindowClosing( )方法对此进行 处理。 框架 如果需要监听键盘事件,最好还是在框架中加入画布或面板之类的组件,然后再通过这类组件对键盘事件进行监听。 面板 面板 通常只作为纯粹的容器来使用,它不能像框架、窗口或对话框那样独立存在, 在面板中也可对多种事件进行监听。 与画布类似,当处理键盘事件时,需要事先取得焦点。 面板  两种构造方法: (1) Panel() 构造一个使用默认布局管理器( FlowLayout)的面板。 (2) Panel(LayoutManager) 构造一个使用指定布局管理器的面板 面板 例如: Panel p = new Panel( ); /*这条命令构造一个使用 FlowLayout布局管理器的面板。 */ 对话框 对话框( Dialog) 是与框架类似的 可移动窗口 ,它与 框架的区别 在于具有较少的修饰并且能够被设置为“模式( modal)”窗口 ——在该窗口被关闭之前,其他窗口无法接收任何形式的输入。 对话框 四种构造方法: (1) public Dialog(Frame parent) 构造一个 没有标题 的对话框。 (2) public Dialog(Frame parent, boolean modal) 构造一个 没有标题 的对话框, boolean型参数指定对话框是否为模式窗口。 对话框 四种构造方法: (3) public Dialog(Frame parent, String title) 构造一个 显示指定标题 的对话框。 (4) public Dialog(Frame parent, String title, boolean modal) 构造一个 显示指定标题的对话框 ,boolean型参数指定对话框是否为模式窗口。 对话框 例如: Dialog d = new Dialog(f, Dialog,true); /*这条命令构造一个标题为 Dialog的模式对话框,该对话框由框架 f所拥有。 */ 对话框 刚刚创建的对话框是不可见的,需要调用setVisible(true)方法才能将其显示出来。 当对话框不需要显示时,调用setVisible(false)方法可以将一个对话框隐藏起来。 对话框可对各种窗口事件( WindowEvent)进行监听。 程序 99: 目标: 构造对话框,单击其中的按钮时,显示对话框。 import .*; import .*; public class DialogExample extends WindowAdapter implements ActionListener{ Frame f; Button b; Dialog d; static public void main(String args[]) { DialogExample de = new DialogExample(); (); } public void go() { f = new Frame(Dialog Example); b = new Button(Show Dialog); (this); (South,b); d = new Dialog(f,Dialog,true); (Center,new Label (Hello,I’m a Dialog)); (); (this); (250,150); (true); } public void actionPerformed(ActionEvent e) { // 显示对话框 (true); } public void windowClosing(WindowEvent e) { // 隐藏对话框 (false); }} 文件对话框。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。