欢迎来到天天文库
浏览记录
ID:20791080
大小:491.50 KB
页数:23页
时间:2018-10-15
《java设计用户注册界面》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、任务9——设计用户注册界面学习情境二(考试系统单机版)学习目标掌握JComboBox、JCheckBoxl、JRadioButton组件的创建及ItemEvent事件处理。掌握JList组件的创建及ListSelectionEvent事件的处理。熟悉网格包布局管理器、盒式布局的使用及其多种布局方式的综合应用。9.1任务描述9.2技术要点选择性组件选择事件复杂布局管理器选择性组件——组合框(JComboBox类)选择性组件——列表框(JList类)选择性组件——单选按钮(JRadioButton类)选择性组件——复选框(JCheckbox类)选择
2、事件——ItemEvent类举例ItemeventDemo.java1importjava.awt.*;2importjava.awt.event.*;3importjavax.swing.*;4publicclassItemeventDemoextendsJFrameimplementsItemListener,ActionListener{5JRadioButtonopt1;6JRadioButtonopt2;7ButtonGroupbtg;8JTextAreata;9JComboBoxcomb;10JLabelsex,city;11pub
3、licItemeventDemo(Stringtitle){12super(title);13setLayout(newFlowLayout(FlowLayout.LEFT));14sex=newJLabel("性别:");15city=newJLabel("籍贯:");16opt1=newJRadioButton("男");17opt2=newJRadioButton("女");18btg=newButtonGroup();19btg.add(opt1);20btg.add(opt2);21opt1.addItemListener(this)
4、;22opt2.addItemListener(this);23ta=newJTextArea(8,35);24comb=newJComboBox();25comb.addItem("北京");26comb.addItem("上海");27comb.addItem("南京");28comb.addItem("广州");29comb.addItem("成都");30comb.addItem("昆明");31comb.addItemListener(this);32comb.addActionListener(this);33getContentP
5、ane().add(sex);34getContentPane().add(opt1);35getContentPane().add(opt2);36getContentPane().add(city);37getContentPane().add(comb);38getContentPane().add(ta);39setTitle(title);40setSize(300,250);41setVisible(true);42}举例ItemeventDemo.java(续)举例ItemeventDemo.java(续)43publicstat
6、icvoidmain(Stringargs[]){44newItemeventDemo("ItemeventDemo");45}46//ItemEvent事件发生时的处理操作47publicvoiditemStateChanged(ItemEvente){48Stringstr;49if(e.getSource()==opt1)//如果是opt1被选择50ta.append("性别:"+"男");51elseif(e.getSource()==opt2)//如果opt2被选择52ta.append("性别:"+"女");53if(e.g
7、etSource()==comb){54str=comb.getSelectedItem().toString();55ta.append("籍贯:"+str+"=>ItemEvent事件");56}57}58publicvoidactionPerformed(ActionEvente){59Stringstr;60if(e.getSource()==comb){61str=comb.getSelectedItem().toString();62ta.append("籍贯:"+str+"=>ActionEvent事件");63}64}6
8、5}运行效果举例CheckBoxDemo.java1importjava.awt.*;2importjava.awt.event.*;3importj
此文档下载收益归作者所有