1 / 24

第 13 讲 常用组件之二

第 13 讲 常用组件之二. 13.1 单行文本框与多行文本框 13.2 单选框与复选框 13.3 滚动面板 13.4 列表框和下拉列表框 13.5 菜单和快捷菜单. 本章要 点. 掌握单行文本框与多行文本框的基本属性与基本使用方法 掌握单选框与复选框的基本属性与基本使用方法 了解滚动面板的用法 掌握列表框与下拉列表框的用法 掌握菜单与快捷菜单的用法. 13.1 单行文本框和多行文本框.

haven
Download Presentation

第 13 讲 常用组件之二

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. 第13讲 常用组件之二 • 13.1 单行文本框与多行文本框 • 13.2 单选框与复选框 • 13.3 滚动面板 • 13.4 列表框和下拉列表框 • 13.5 菜单和快捷菜单

  2. 本章要点 • 掌握单行文本框与多行文本框的基本属性与基本使用方法 • 掌握单选框与复选框的基本属性与基本使用方法 • 了解滚动面板的用法 • 掌握列表框与下拉列表框的用法 • 掌握菜单与快捷菜单的用法

  3. 13.1 单行文本框和多行文本框 • 单行文本框是一个单行显示区域,可以从键盘上接收用户输入。用户将数据输入文本框,然后按回车键就可以在程序中使用该数据。单行文本框也可以用于显示信息。TextField类的构造函数。

  4. 例13.1 演示单行文本框的构造函数及常用方法的使用。 • import java.applet.Applet; • import java.awt.*; • public class TextFieldDemo extends Applet{ • private TextField textField1,textField2; • public void init(){ • textField1=new TextField("在这里输入用户名"); • textField2=new TextField(15); • textField2.setEchoChar('*'); • add(new Label("用户名: ")); • add(textField1); • add(new Label("密 码: ")); • add(textField2); • } • public void paint(Graphics g){ • String s; • s="用户名: "+textField1.getText(); • s=s+" 密码: "+textField2.getText(); • showStatus(s); • }}

  5. 例13.1 程序运行结果如图所示

  6. 例13.2 TextArea类的常用方法。 • 其中一个多行文本框不允许用户进行编辑,而另一个则允许用户输入信息。 • import java.applet.Applet; • import java.awt.*; • public class TextAreaDemo extends Applet{ • private TextArea textArea1,textArea2; • public void init() • { • //creating 2 10*20 TextAreas • textArea1=new TextArea("Read-only Text!",10,20); • textArea2=new TextArea(10,20); • //set textArea1 read-only • textArea1.setEditable(false); • add(textArea1); • add(textArea2); • } • }

  7. 例13.2 程序运行结果如图所示

  8. 13.2 复选框和单选按钮 • Checkbox类的构造函数及常用方法

  9. 例13.3 • import java.applet.Applet; • import java.awt.*; • public class MyCheckbox extends Applet{ • private String city[]={"桂林","福州","青岛","济南","深圳","大连"}; • private Checkbox c[] = new Checkbox[6]; • public void init() • { • add(new Label("请选择是省会的城市: ")); • //Create six Checkboxes and add them to applet • for(int i=0;i<6;i++){ • c[i]=new Checkbox(city[i]); • add(c[i]); • } • add(new Label("这是一个复选框的例子")); • } • }

  10. 例13.3程序运行结果如图所示

  11. 例13.4 演示单选按钮的创建方法 • import java.applet.Applet; • import java.awt.*; • public class RadioButton extends Applet{ • private String city[]={"北京","上海","西安","重庆","深圳","大连"}; • private Checkbox radio[] = new Checkbox[6]; • private CheckboxGroup c=new CheckboxGroup(); • public void init() • { • add(new Label("请选择中国最大的城市: ")); • //Create six radio buttons and add them to applet • for(int i=0;i<6;i++){ • radio[i]=new Checkbox(city[i],c,false); • add(radio[i]); • } • add(new Label("这是一个单选按钮的例子")); • } • }

  12. 例13.4程序运行结果如图所示

  13. 13. 3 滚动面板 • 滚动面板(ScrollPane)实现了为单一组件提供水平方向或者/和竖直方向的滚动条。滚动条的显示方式可以设置成三种模式:需要的时候才显示(SCROLLBARS_AS_NEEDED,只有当滚动面板需要的时候才创建并显示滚动条)、总是显示(SCROLLBARS_ALWAYS,滚动面板总是创建并显示滚动条)和决不显示(SCROLLBARS_NEVER,滚动面板决不创建和显示滚动条)。 ScrollPane类的构造函数 :

  14. 例13.5 演示滚动面板的三种显示策略。 • import java.awt.*; • import java.applet.Applet; • public class ScrollPaneDemo extends Applet{ • private TextArea t1=new TextArea(); • private TextArea t2=new TextArea(); • private TextArea t3=new TextArea(); • private ScrollPane p1=new ScrollPane(ScrollPane.SCROLLBARS_NEVER); • private ScrollPane p2=new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED); • private ScrollPane p3=new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS); • public void init(){ • setLayout(new GridLayout(3,1)); • p1.add(t1); • p2.add(t2); • p3.add(t3); • add(p1); • add(p2); • add(p3); • } • }

  15. 例13.5程序运行结果如图所示

  16. 13.4 列表框和下拉列表框 • 列表框(List)用于显示一系列的选项,用户可以从中选择一个或多个选项。 • 下拉列表框(Choice)与列表框相似,不同的是下拉列表框只能从列表中选择一个选项,当用户单击旁边下拉箭头按钮时,选项列表打开。

  17. 例13.6 列表框和下拉列表框的使用 • import java.applet.Applet; • import java.awt.*; • public class ListAndChoiceDemo extends Applet{ • private List cityList; • private Choice cityChoice; • public void init() • { cityList=new List(5,true); • cityList.add("桂林"); • cityList.add("福州"); • cityList.add("济南"); • cityList.add("大连"); • cityChoice=new Choice(); • cityChoice.addItem("北京"); • cityChoice.addItem("上海"); • cityChoice.addItem("西安"); • cityChoice.addItem("重庆"); • add(new Label("中国最大的城市: ")); • add(cityChoice); • add(new Label("省会城市: ")); • add(cityList); • }}

  18. 例13.6程序运行结果如图所示

  19. 13.5 菜单和快捷菜单 • 菜单及其相关的类在java.awt软件包中的层次关系。

  20. • MenuItem类用于创建菜单项。一个菜单项就是包含在菜单中的一个字符串,当选中它时将会执行一个动作。 • MenuBar类用于创建菜单栏,菜单栏是菜单的容器。 • Menu类用于创建菜单,菜单中包含若干菜单项,并将其添加到菜单栏上。当使用鼠标点击一个菜单时,就将展开该菜单,并显示一列菜单项。 • CheckboxMenuItem类用于创建复选框菜单。复选框菜单项就是带有选择标记的菜单项。当选中复选框菜单项时,在该菜单项左边出现一个选择标记。如果再次选中该项,则该选项左边的选择标记就会消失。

  21. 例13.7 演示在应用程序中创建菜单的方法。 • import java.awt.*; • public class MenuDemo extends Frame{ • private TextArea t; • public MenuDemo() • { • super("菜单示例"); • t=new TextArea(); • add("Center",t); • MenuBar bar=new MenuBar(); • Menu fontMenu=new Menu("字体"); • MenuItem mi1=new MenuItem("宋体"); • MenuItem mi2=new MenuItem("黑体"); • MenuItem mi3=new MenuItem("隶书"); • fontMenu.add(mi1); • fontMenu.add(mi2); • fontMenu.add(mi3); • bar.add(fontMenu); • setMenuBar(bar); • resize(300,200); • setVisible(true); • } • public static void main(String[] args) • { • new MenuDemo(); • }}

  22. 例13.7程序运行结果如图所示

  23. 例13.8 为多行文本框添加快捷菜单。程序运行如图10.17所示。 • import java.awt.*; • import java.awt.event.*; • public class PopupMenuDemo extends Frame implements MouseListener{ • TextArea t; • PopupMenu pm; • public PopupMenuDemo(){ • super("快捷菜单示例"); • t=new TextArea(); • add("Center",t); • pm=new PopupMenu(); • MenuItem mi1=new MenuItem("宋体"); • MenuItem mi2=new MenuItem("黑体"); • MenuItem mi3=new MenuItem("隶书"); • pm.add(mi1); • pm.add(mi2); • pm.add(mi3); • t.add(pm); • t.addMouseListener(this); • setSize(300,200); • setVisible(true);} • public void mouseReleased(MouseEvent e){ • if(e.isPopupTrigger()){pm.show((Component)e.getSource(),e.getX(),e.getY()); }} • public void mousePressed(MouseEvent e){} • public void mouseClicked(MouseEvent e){} • public void mouseEntered(MouseEvent e){} • public void mouseExited(MouseEvent e){} • public static void main(String[] args){new PopupMenuDemo(); }}

  24. 例13.8程序运行结果如图所示

More Related