JMenultemanltem=newJMenultem(menuList[i][j]);
anltem.setActionCommand(menuList[i][j]);
anltem.________;(anltem);}
menubar.__________;}
text=newJTextField();setJMenuBar(menubar);
(text,BorderLayout.SOUTH);}
publicvoidactionPerformed(ActionEvente){
text.setText(e.getActionCommand()+″菜单项被选中!
″);}}
publicclassTest29extendsApplet{
MenuWindowwindow;
StringmenuList[][]={{″体育″,″跑步″,″打蓝球″,″打乒乓″},
{″娱乐″,″唱歌″,″跳舞″}};
publicvoidinit(){
window=newMenuWindow(″体育娱乐之窗″,menuList);
(true);}}
30.以下是一个用鼠标自由作画的小应用程序。
最简单的方法是根据鼠标所在位置画点,跟随鼠标的移动,不断画圆点,就能实现用鼠标作画。
importjava.awt.*;
importjava..*;
publicclassTest30extendsjava.applet.AppletimplementsMouseMotionListener{
Colorcolor;intlineSize=2:
intx=-1,y=-l;
publicvoidinit(){
setLocation(30,20);setSize(300,300);
setBackground;addMouseMotionListener(this);}
publicvoidpaint(Graphicsg){
if(x!
=-1&&y!
=-1){
(color);(x,y,1ineSize,lineSize);}}
publicvoidmouseMoved(MouseEvente){}
publicvoidmouseDragged(MouseEvente){
x=();y=e.getY();_________;}
publicvoid________(Graphicsg){paint(g);}}
31.以下定义的类ShareData用于管理多个线程共享数据data。
一个线程生成data,另一个线程使用data。
约定,新生成的data只有被另一个线程使用后,才能生成下一个data。
反之,一个data被使用后,也不能再继续使用。
所以,生成和使用data的线程之间需要互斥和同步。
以下是管理上述使用方式的类,类内有要管理的共享数据,以及对共享数据的存操作putData()和取操作getData()。
classShareData{
intdata;∥共享数据
booleannewData=false;∥有最近新生成data的标志
synchronizedintgetData(){
while(!
newData){
try{_________;}catch(InterruptedExceptipne){}}
newData=false;notify();returndata;}
synchronizedvoidputData(intn){
while(newData){
try{wait();}catch(InterruptedExceptione){}}
data=n;__________;
notify();return;}}
五、程序分析题(本大题共5小题,每小题4分,共20分)
32.阅读下列程序,请写出该程序的输出结果。
classParent{
}}
classChildextendsParent{
publicstaticvoidmain(Stringargs[]){Parentp=newChild();();}}
33.阅读下列程序,请写出该程序的功能。
import.*;import.*;
publicclassTest33{
publicstaticvoidmain(Stringargs[]){
Stringstr=(String)JOptionPane.showInputDialog(null,″请输入信息″,
″输入对话框″,JOptionPane.PLAIN_MESSAGE,null,null,null);
StringTokenizerpas=newStringTokenizer(str,″,″);
intn=();
while(pas.hasMoreTokens()){
Strings=();
}}
34.阅读下列程序,请用示意图画出程序运行时呈现的界面。
import.*;import.*;import.*;
classMyPanelextendsJPanel{
JTextFieldtextl,text2;
MyPanel(Stringsl,Strings2){
textl=newJTextFieId(s1);text2=newJTextField(s2);
add(text1);add(text2);}}
classMySubPanelextendsMyPanel{JTextFieldtext;
MySubPanel(Stringsl,Strings2,Strings3){
super(s1,s2);text=newJTextField(s3);add(text);}}
publicclassTest34{
publicstaticvoidmain(Stringargs[]){
JFramemw=newJFrame(″一个示意窗口″);
(350,150);
Containercon=();
(newBorderLayout());
MyPanelpl=newMyPanel(″文本框l″,″文本框2″);
MySubPanelp2=newMySubPanel(″文本框3″,″文本框4″,″文本框5″);
JTextAreatext=newJTextArea(″这里是一个文本区″);
(pl,″North″);(p2,″South″);
(text,″Center″);mw.setVisible(true);}}
35.阅读下列程序,请写出该程序的功能。
publicclassClass1extendsAppletimplementsKeyListener{
JButtonbutton=newJButton(″开始″);
JTextAreatext=newJTextArea(5,20);
publicvoidinit(){
(this);add(button);add(text);}
publicvoidkeyPressed(KeyEvente){
intt=();
if(t>=&&t<={
(″″+(char)t);}}
publicvoidkeyTyped(KeyEvente){}
publicvoidkeyReleased(KeyEvente){}}
36.阅读下列程序,请写出该程序的功能。
import.*;import.*;
ThreadmyThread=null;
doubleseta=;
publicvoidstart(){
setSize(500,400);
if(myThread=null){myThread=newThread(this);();}}
publicvoidrun(){
while(myThread!
=null){
try{(40);}catch(InterruptedExceptione){}
seta+=;if(seta>=360)seta=0;repaint();}}
publicvoidpaint(Graphicsg){
finaldoublepi=;finaldoubler=;
intx0=250+(int)(r**seta));
inty0=200+(int)(r**seta));
;(x0,y0,10,10);}}
六、程序设计题(本大题共2小题,每小题6分,共12分)
37.请编写方法voidstrReverse(Stringstr),该方法的功能是输出一个新字符串,新字符串字符排列顺序与原字符串str的字符排列顺序相反。
例如,strReverse(″ABCD″)所输出的结果是″DCBA″。
请使用字符串与字节数组的相互转换方法进行设计。
38.请设计实现如下用于输入学号和姓名的对话框界面,其中空白格是文本框,用于输入相应的内容。
这里给出的是程序的一部分,你要编写的是类InputNoNameDialog的构造方法InputNoNameDialog(JFramef,Strings,JTextFieldt)。
其中参数f是对话框的依赖窗口,s是对话框标题,t是依赖窗口中显示对话框输入内容的文本框。
以下是类InputNoNameDialog的程序框架。
classInputNoNameDialogextendsJDialogimplementsActionListener{
JLabeltitle;JTextFieldtextl,text2,mainText;JButtondone;
InputNoNameDialog(JFramefStrings,JTextFieldt){
super(f,s,true);mainText=t;Containercon=getContentPane();
title=newJLabel(s);textl=newJTextField(10);
text2=newJTextField(10);(newGridLayout(3,2));
(200,100);setModal(false);
//请在以下位置续写其余代码
}
publicvoidactionPerformed(ActionEvente){
//输入结束按确定按钮后,将对话框中输入的学号和姓名在它依赖窗口的文本框中显示。
(″学号:
″+()+″姓名:
″+());
setVisible(false);dispose();
}
}