习题09.docx
- 文档编号:11399587
- 上传时间:2023-05-31
- 格式:DOCX
- 页数:17
- 大小:19.75KB
习题09.docx
《习题09.docx》由会员分享,可在线阅读,更多相关《习题09.docx(17页珍藏版)》请在冰点文库上搜索。
习题09
习题九
1.什么是代理事件处理模型?
请举例说明。
【答】:
Java语言采用基于代理的事件处理模型。
基于代理事件处理模型是引进了监听器接口,每个事件类都有相关联的监听器接口。
当用户对GUI组件有所动作,GUI组件会自动产生相应的事件来响应用户的动作。
假设这些GUI组件已经注册了相关的监听器对象,监听器对象会一直监听已注册的组件对象是否有事件发生。
当监听器对象监听到组件有相对应的事件发生,就会调用监听器对象中方法,即事件处理器来实现一定的动作。
就这样,通过对目标监听器对象的相关Java方法调用,实现事件从事件源到监听者的传递。
2.常见的事件处理方式有哪几种?
请比较各种方式的特点?
【答】:
有通过
(1)实现监听器接口来处理事件、
(2)利用独立监听器类实现事件处理、(3)利用内部类实现事件处理、以及(4)利用匿名内部类实现事件处理四种事件处理方式。
方法
(1)可直接对监听器接口的方法进行处理和调用,并且应用简单方便。
但是,这种方式存在一个问题,由于实现接口的方法和定义类的方法处于平行定义,如果要求对多个组件有不同的处理,这种方法无疑会增加阅读程序的困难。
方法
(2)事件处理的方式优点在于,处理事件的方法清晰明了,可读性强。
另外,允许扩展事件适配器(具体内容见8.3节)来定义新的监听器的类,可以减少不需要的方法的处理。
但是,在事件发生的主类中,会增加事件发生对监听器对象的方法的额外调用。
方法(3)是利用内部类来实现事件处理,具有上述两种方法的优点。
但是随着发生事件的组件增多,程序会变得比较庞大,增加对程序的理解。
方法(4)是利用匿名内部类实现事件处理。
有效减少代码的长度,但是利用匿名内部类更会增加程序阅读的难度。
3.编写程序,统计用户在一个窗口点击的次数,并在窗口的状态栏中显示处理。
【答】:
实现窗口状态栏显示点击窗口次数的程序见ClickCounter.java,代码如下:
packagejavabook.answer.ch09;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassEX3extendsJFrameimplementsMouseListener{
publicEX3(){
container=getContentPane();
container.setLayout(newBorderLayout());
mainFrm=newJPanel();//定义主窗口
mainFrm.setSize(400,400);
mainFrm.setVisible(true);
mainFrm.addMouseListener(this);
statuPane=newJPanel();//定义状态栏
resultLbl=newJLabel("窗口状态");
statuPane.add(resultLbl);
container.add(mainFrm,BorderLayout.CENTER);
container.add(statuPane,BorderLayout.SOUTH);
setSize(500,400);
setVisible(true);
}
publicvoidmouseEntered(MouseEvente){
}
publicvoidmouseExited(MouseEvente){
}
publicvoidmouseClicked(MouseEvente){//处理点击事件
if(e.getSource()==mainFrm){
times++;
resultLbl.setText("点击次数为:
"+times);
}
}
publicvoidmousePressed(MouseEvente){
}
publicvoidmouseReleased(MouseEvente){
}
publicstaticvoidmain(Stringargs[]){
EX3cc=newEX3();
cc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
JPanelmainFrm;
JPanelstatuPane;
JLabelresultLbl;
Containercontainer;
publicinttimes=0;//点击次数;
}
4.编写程序,通过鼠标轮实现对一个圆形的大小控制及通过键盘实现圆形的移动。
【答】:
实现鼠标对圆形大小和位置的控制见下列代码:
packagejavabook.answer.ch09;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassEX4extendsJPanelimplementsKeyListener,MouseWheelListener{
Containercontainer;
intlength=40;//圆的外接矩形
intx=100,y=100;
publicEX4(){
this.addKeyListener(this);
this.addMouseWheelListener(this);
this.setFocusable(true);
setSize(400,300);
setVisible(true);
}
publicvoidmouseWheelMoved(MouseWheelEvente){//鼠标移动
if(e.getWheelRotation()<0){
length-=2;
if(length<10)
length=10;
}elseif(e.getWheelRotation()>0){
length+=2;
if(length>300)
length=300;
}
repaint();
}
publicvoidpaintComponent(Graphicsg){//绘制圆形
draw(g,x,y,length);
}
publicvoiddraw(Graphicsg,intx,inty,intlength){
g.clearRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.blue);//设置颜色
g.drawOval(x,y,length,length);//绘制圆形;
}
publicvoidkeyPressed(KeyEvente){
intkeycode=e.getKeyCode();
if(keycode==KeyEvent.VK_UP){
y-=4;
if(y<10)
y=10;
}elseif(keycode==KeyEvent.VK_DOWN){
y+=4;
if(y>400)
y=400;
}elseif(keycode==KeyEvent.VK_RIGHT){
x+=4;
if(x>400)
x=400;
}elseif(keycode==KeyEvent.VK_LEFT){
x-=4;
if(x<10)
x=10;
}
repaint();
}
publicvoidkeyTyped(KeyEvente){
}
publicvoidkeyReleased(KeyEvente){
}
publicstaticvoidmain(Stringargs[]){//主程序
EX4ms=newEX4();
JFramefrm=newJFrame();
frm.add(ms);
frm.setSize(400,400);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
5.编写程序,实现用户注册的功能。
具体包括用户的用户名、密码、确认密码、性别、出生年月。
另外设计两个按钮“确认”和“取消”。
当用户单击“确认”按钮时,可以将注册信息在屏幕上显示出来。
如果单击“取消”,则退出用户注册。
【答】:
用户注册的程序见下列代码:
packagejavabook.answer.ch09;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassEX5extendsJFrame{
Stringinformation;
Stringgendar="",name="",birthday="",password="";
JLabelnameLabel,passwordLabel,gendarLabel,confirmLabel,birthLabel;
JTextFieldnameField,birthField;
JPasswordFieldpasswordField;
JPasswordFieldconfirmField;JRadioButtonradio1,radio2;ButtonGroupgroup;JButtonconfirmBtn,cancelBtn;booleanjudge=false;publicEX5(){super("用户注册示例");Containercontainer=getContentPane();container.setLayout(newFlowLayout());nameLabel=newJLabel("用户名");nameField=newJTextField(10);nameField.addActionListener(newNewActionListener());container.add(nameLabel);container.add(nameField);passwordLabel=newJLabel("密码");passwordField=newJPasswordField(10);confirmLabel=newJLabel("确定密码");confirmField=newJPasswordField(10);passwordField.addActionListener(newNewActionListener());confirmField.addActionListener(newNewActionListener());container.add(passwordLabel);container.add(passwordField);container.add(confirmLabel);container.add(confirmField);birthLabel=newJLabel("出生日期");
birthField=newJTextField(10);
container.add(birthLabel);
container.add(birthField);
birthField.addActionListener(newNewActionListener());
gendarLabel=newJLabel("性别");
radio1=newJRadioButton("男");
radio2=newJRadioButton("女");
radio1.addItemListener(newRadioListener());
radio2.addItemListener(newRadioListener());
group=newButtonGroup();
group.add(radio1);
group.add(radio2);
container.add(gendarLabel);
container.add(radio1);
container.add(radio2);
confirmBtn=newJButton("确定");
cancelBtn=newJButton("取消");
confirmBtn.addActionListener(newNewActionListener());
cancelBtn.addActionListener(newNewActionListener());
container.add(confirmBtn);
container.add(cancelBtn);
setSize(480,140);
setVisible(true);
}
classRadioListenerimplementsItemListener{
//针对单选按钮的事件处理
@SuppressWarnings("deprecation")
publicvoiditemStateChanged(ItemEvente){
Objectitem=e.getItemSelectable();
if(item==radio1)
gendar=radio1.getLabel();
elseif(item==radio2)
gendar=radio2.getLabel();
}
}
classNewActionListenerimplementsActionListener{
@SuppressWarnings("deprecation")
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==confirmBtn){
if(judge)
information="姓名"+name+"\n密码"+password+"\n性别:
"
+gendar+"\n出生日期:
"+birthday;
else
information="密码与确认密码不一致!
";
JOptionPane.showMessageDialog(null,information);
}elseif(e.getSource()==nameField){
name=nameField.getText();
}elseif(e.getSource()==passwordField){
password=passwordField.getText();
}elseif(e.getSource()==confirmField){
StringconPassword=confirmField.getText();
if(conPassword.equals(password)){
judge=true;
}
}elseif(e.getSource()==birthField){
birthday=birthField.getText();
}elseif(e.getSource()==cancelBtn){
System.exit(0);
}
}
}
publicstaticvoidmain(Stringargs[]){
EX5re=newEX5();
re.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
6.编写程序实现菜单的制作并根据菜单项执行相应的处理。
该菜单包括“格式”和“窗口”两项,其中“格式”菜单可以设置前景色、背景色、字体的风格、指定字体格式等内容。
【答】:
实现相应功能的程序见下列代码:
packagejavabook.answer.ch09;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassEX6extendsJFrameimplementsActionListener,ItemListener{
JScrollPanescrollPane;
JTextAreaoutput;
StringcolorNames[]={"红色","兰色","绿色","灰色","白色","黑色"};
Colorcolor[]={Color.red,Color.blue,Color.green,Color.gray,
Color.white,Color.black};
StringfontStyleNames[]={"粗体","斜体"};
StringfontNames[]={"楷体","宋体","黑体","Serif","TimesNewRoman"};
intfontstyle=Font.PLAIN;
intfontbold,fontitalic;
Stringfontname="TimesNewRoman";
intfontsize=20;
JMenuItemcolorItems[],bcolorItems[];
ButtonGroupbuttonGroup;
JCheckBoxMenuItemfontStyleItem[];
JRadioButtonMenuItemfontItem[];
JMenuItemcloseItem;
publicEX6(){//constructor
super("MenuTest");
ImageIconicon=newImageIcon("D:
/javaexample/games.gif");
setIconImage(icon.getImage());
createAndShowGUI();
}
publicJMenuBarcreateMenuBar(){//createamenu
JMenuBarstyleMenuBar;
styleMenuBar=newJMenuBar();//菜单条
JMenustyleMenu=newJMenu("格式");//菜单;
JMenuforegroundItem=newJMenu("前景色");//建立下拉菜单
colorItems=newJMenuItem[colorNames.length];
for(inti=0;i colorItems[i]=newJMenuItem(colorNames[i]); colorItems[i].addActionListener(this); colorItems[i].setForeground(color[i]); foregroundItem.add(colorItems[i]); } styleMenu.add(foregroundItem); JMenubackgroundItem=newJMenu("后景色");//菜单项 bcolorItems=newJMenuItem[colorNames.length]; for(inti=0;i bcolorItems[i]=newJMenuItem(colorNames[i]); bcolorItems[i].setForeground(color[i]); bcolorItems[i].addActionListener(this); backgroundItem.add(bcolorItems[i]); } styleMenu.add(backgroundItem); styleMenu.addSeparator();//添加分隔 //创建复选菜单项,设置字体风格; fontStyleItem=newJCheckBoxMenuItem[fontStyleNames.length]; for(inti=0;i fontStyleItem[i]=newJCheckBoxMenuItem(fontStyleNames[i]); fontStyleItem[i].addItemListener(this); styleMenu.add(fontStyleItem[i]); } styleMenu.addSeparator(); //创建单选菜单项,设置字体; fontItem=newJRadioButtonMenuItem[fontNames.length]; buttonGroup=newButtonGroup(); for(inti=0;i fontItem[i]=newJRadioButtonMenuItem(fontNames[i]); fontItem[i].addItemListener(this); buttonGroup.add(fontItem[i]); styleMenu.add(fontItem[i]); } styleMenuBar.add(styleMenu); JMenuwindo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 习题 09