任务9——设计用户注册界面概要

上传人:今*** 文档编号:107410828 上传时间:2019-10-19 格式:PPT 页数:23 大小:437KB
返回 下载 相关 举报
任务9——设计用户注册界面概要_第1页
第1页 / 共23页
任务9——设计用户注册界面概要_第2页
第2页 / 共23页
任务9——设计用户注册界面概要_第3页
第3页 / 共23页
任务9——设计用户注册界面概要_第4页
第4页 / 共23页
任务9——设计用户注册界面概要_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《任务9——设计用户注册界面概要》由会员分享,可在线阅读,更多相关《任务9——设计用户注册界面概要(23页珍藏版)》请在金锄头文库上搜索。

1、任务9设计用户注册界面,学习情境二(考试系统单机版),学习目标,掌握JComboBox、JCheckBoxl、JRadioButton组件的创建及ItemEvent事件处理。 掌握JList组件的创建及ListSelectionEvent事件的处理。 熟悉网格包布局管理器、盒式布局的使用及其多种布局方式的综合应用。,9.1 任务描述,9.2 技术要点,选择性组件 选择事件 复杂布局管理器,选择性组件组合框(JComboBox类),选择性组件列表框(JList类),选择性组件单选按钮(JRadioButton类),选择性组件复选框(JCheckbox类),选择事件ItemEvent类,举例 It

2、emeventDemo.java,1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 public class ItemeventDemo extends JFrame implements ItemListener,ActionListener 5 JRadioButton opt1; 6 JRadioButton opt2; 7 ButtonGroup btg; 8 JTextArea ta; 9 JComboBox comb; 10 JLabel sex,city; 11 public Ite

3、meventDemo(String title) 12 super(title); 13 setLayout(new FlowLayout(FlowLayout.LEFT); 14 sex=new JLabel(“性 别: “); 15 city=new JLabel(“ 籍 贯:“); 16 opt1=new JRadioButton(“ 男 “); 17 opt2=new JRadioButton(“ 女 “); 18 btg=new ButtonGroup(); 19 btg.add(opt1); 20 btg.add(opt2); 21 opt1.addItemListener(thi

4、s); 22 opt2.addItemListener(this);,23 ta=new JTextArea (8,35); 24 comb=new JComboBox(); 25 comb.addItem(“北 京“); 26 comb.addItem(“上 海“); 27 comb.addItem(“南 京“); 28 comb.addItem(“广 州“); 29 comb.addItem(“成 都“); 30 comb.addItem(“昆 明“); 31 comb.addItemListener(this); 32 comb.addActionListener(this); 33 g

5、etContentPane().add(sex); 34 getContentPane().add(opt1); 35 getContentPane().add(opt2); 36 getContentPane().add(city); 37 getContentPane().add(comb); 38 getContentPane().add(ta); 39 setTitle(title); 40 setSize(300,250); 41 setVisible(true); 42 ,举例 ItemeventDemo.java(续),举例 ItemeventDemo.java(续),43 pu

6、blic static void main(String args) 44 new ItemeventDemo(“Itemevent Demo“); 45 46 / ItemEvent事件发生时的处理操作 47 public void itemStateChanged(ItemEvent e) 48 String str; 49 if(e.getSource()=opt1) / 如果是opt1被选择 50 ta.append(“n性 别:“+“男“); 51 else if(e.getSource()=opt2) / 如果opt2被选择 52 ta.append(“n性 别:“+“女“); 5

7、3 if(e.getSource()=comb) 54 str=comb.getSelectedItem().toString(); 55 ta.append(“n籍 贯:“+str+“ =ItemEvent事件 “); 56 57 58 public void actionPerformed(ActionEvent e) 59 String str; 60 if(e.getSource()=comb) 61 str=comb.getSelectedItem().toString(); 62 ta.append(“n籍 贯:“+str+“ =ActionEvent事件 “); 63 64 65

8、 ,运行效果,举例CheckBoxDemo .java,1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 public class CheckBoxDemo extends JFrame implements ItemListener 5 private JTextField field; 6 private JCheckBox bold, italic; 7 private int valBold = Font.PLAIN; 8 private int valItalic = Font.PLAI

9、N; 9 public CheckBoxDemo() 10 super( “JCheckBox Demo“ ); 11 Container container = getContentPane(); 12 container.setLayout( new FlowLayout() ); 13 field = new JTextField( “2008,北京欢迎您!“, 20 ); 14 field.setFont( new Font( “隶书“, Font.PLAIN, 14 ) ); 15 container.add(field ); 16 bold = new JCheckBox( “Bo

10、ld“ );,17 container.add( bold ); 18 italic = new JCheckBox( “Italic“ ); 19 container.add( italic ); 20 bold.addItemListener(this); 21 italic.addItemListener( this ); 22 setSize( 280, 100 ); 23 setVisible( true ); 24 25 public void itemStateChanged(ItemEvent event) 26 if ( event.getSource() = bold )

11、27 valBold = bold.isSelected() ? Font.BOLD : Font.PLAIN; 28 if ( event.getSource() = italic ) 29 valItalic = italic.isSelected() ? Font.ITALIC : Font.PLAIN; 30 field.setFont( new Font( “隶书“, valBold + valItalic, 14 ) ); 31 32 public static void main(String args) 33 new CheckBoxDemo(); 34 35 ,选择事件 Li

12、stSelectionEvent类,首先必须声明实现监听者对象的类接口ListSelectionListener,并通过JList类的addListSelectionListener( )方法注册文本框的监听者对象, 在ListSelectionListener接口的valueChanged (ListSelectionEvent e)方法体中写入有关代码,就可以响应ListSelectionEvent事件。,例9-3 JListDemo.java,1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4

13、 import javax.swing.event.*; 5 public class JListDemo extends JFrame implements ListSelectionListener 6 JList list = null; 7 JLabel label = null; 8 String s = “宝马“,“奔驰“,“奥迪“,“本田“,“皇冠“,“福特“,“现代“; 9 public JListDemo() 10 JFrame f = new JFrame(“JList Demo“); 11 Container contentPane = f.getContentPane(

14、); 12 contentPane.setLayout(new BorderLayout(0,15); 13 label = new JLabel(“ “); 14 list = new JList(s); 15 list.setVisibleRowCount(5); 16 list.setBorder(BorderFactory.createTitledBorder(“汽车品牌:“);,17 list.addListSelectionListener(this); 18 contentPane.add(label,BorderLayout.NORTH); 19 contentPane.add

15、(new JScrollPane(list),BorderLayout.CENTER); 20 f.setSize(300,200); 21 f.setVisible(true); 22 23 public static void main(String args) 24 new JListDemo(); 25 26 public void valueChanged(ListSelectionEvent e) 27 int tmp = 0; 28 String stmp = “您喜欢的汽车品牌有: “; 29 int index = list.getSelectedIndices(); 30 for(int i=0; i index.length ; i+) 31 tmp = inde

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号