一、基本操作题
本题中定义了长度为20的一维整型数组a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。
publicclassjaval{
publicstaticvoidmain(String[]args){
inta[]=;
inti;
for(;i++)
a[i]=i;
for(i=0;i<20;i++){
if()
System.OUt.print("a[It+i+"]="+a[i]+",");
}
}
}
点击进入>>全国计算机等级考试真题题库全套最新考试资料
二、简单应用题
本题中定义了一个树型的通信录,窗El左侧是一个树,
右侧是一个文本域,单击树的结点,则在右侧文本域中显示
相关信息,如果单击的是树结点,则显示对应名字的电话
信息。
importjavax.swing.*;
importjavax.swing.tree.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.event.*;
classMytree2extendsJFrame
{JTreetree=null;JTextAreatext=newJTextArea
(20,20);
Mytree2()
{Containercon=getContentPane();
DefauhMutableTreeNoderoot=newDefault-
MutableTreeNode("同学通信录");
DefaultMutableTreeNodetl=newDefault-
MutableTreeNode("大学同学");
DefaultMutableTreeNodet2=newDefault-
MutableTreeNode("研究生同学");
DefaultMutableTreeNodetll=newDefault-
MutatleTreeNode("陈艳");
DefaultMutableTreeNodetl2=newDefault-
MutableTreeNode("李小永");
DefaultMutableTreeNodet21=newDefauh-
MutableTreeNode("王小小");
DefauhMutableTreeNodet22=newDefauh-
MutableTreeNode("董小");
setTitle("java2");
root.add(t1);root.add(t2);
tl.add(t1_1);tl.add(t1_2);t2.add(t2_1);t2.
add(t2_2);
tree=newJTree(root);
JSerollPanescrollpane=newJScrollPane(text);
JSplitPanesplitpane=newJSplitPane(JSplitPane.
HORIZONTALSPLIT,
true,tree,scrollpane);
tree.addTreeSeleetionListener(this);
con.add(splitpane);
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{System.exit(0);}});
setVisible(true);setBounds(70,80,200,300);
}
publicvoidvalueChanged(TreeSelectionEvente)
{if(e.getSouree()==tree)
{DefauhMutableTreeNodenode=
(DefaultMutableTreeNode)tree.getLastSelected-
PathComponent();
if(node.isLeaf())
(Stringstr=;
if(str.equals("陈艳"))
(text.setText(str+":联系电话:0411-
4209876");}
elseif(str.equals("李小永"))
{text.setText(str+":联系电话:010-
62789876");}
elseif(str.equals("王小小"))
{text.setText(str+":联系电话:0430-
63596677");)
elseif(str.equals("董小"))
{text.setText(str+":联系电话:020-
85192789");}
}
else
{text.setText(node.getUserObject().toString
());
}
}
}
}
publicclassjava2
{publicstaticvoidmain(Stringargs[])
{Mytree2win=newMytree2();win.pack();}
}
三、综合应用题
本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。
importjava.awt.*;
importjava.awt.event.*;
importjavaxswing.*;
classMousePanelextendsJPanelextendsMouseMo-
tionListener
{publicMousePanel()
{addMouseListener(newMouseAdapter()
{publicvoidmousePressed(MouseEventevt)
{intX=evt.getX();
intY=evt.getY();
current=find(x,y);
if(current<0)
add(x,y);
}
publicvoidmouseClicked(MouseEventevt)
{intX=evt.getX();
intY=evt.getY();
if(evt.getClickCount()>=2)
{remove(current);
}
}
});
addMouseMotionListener(this);
}
publicvoidpaintComponent(Graphicsg)
{super.paintComponent();
for(inti=0;i draw(g,i);
}
publicintfind(intX,inty)
(for(inti=0;i if(squares[i].x-SQUARELENGTH/2<=
x&&
X<=squares[i].x+SQuARELENGTH/2
&&squares[i].Y-SQUARELENGTH/2<
=Y
&&y<=squares[i].Y+SQUARELENGTH
/2)
returni;
return-1;
}
publicvoiddraw(Graphicsg,inti)
{g.drawRect(squares[i].X-SQUARE-
LENGTH/2。
squares[i].Y-SQUARELENGTH/2,
SQUARELENGTH,
SQUARELENGTH);
}
publicvoidadd(intX,intY)
{if(nsquares {squares[nsquares]=newPoint(x,y);
current=nsquares;
nsquares++;
repaint();
}
}
publicvoidremove(intn)
{if(n<0‖n>=nsquares)return;
Nsquares--;
squares[n]=squares[nsquares];
if(current==n)current=-l;
repaint();
}
publicvoidmouseMoved(MouseEventevt)
{}
publicvoidmouseDragged(MouseEventevt)
{}
privatestaticfinalintSQUARELENGTH=10:
privatestaticfinalintMAXNSQUARES=100;
privatePoint[]squares=newPoint[MAX-
NSQUARES];
privateintnsquares=0;
privateintcurrent=-l;
}
classMouseFrameextendsJFramc
{publicMouseFrame()
{setTitle("java3");
setSize(300,200);
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{System.exit(0);
}
});
ContainercontentPane=getContentPane();
contentPane.add(MousePanel());
}
}
publicclassjava3
{publicstaticvoidmain(String[]args)
{JFrameframe=newMouseFrame();
frame.show();
}
}
上机考试试题答案与解析
一、基本操作题
第1处:newint[20]
第2处:i=0;i<20
第3处:i%21=0
【解析】第1处定义、了长度为20的一维整型数组a;第2处的for循环将数组元素的下标值赋给数组元素;第3处判断数组各个元素下标是否为奇数。
二、简单应用题
第1处:implementsTreeSelectionListener
第2处:node.toString()
【解析】第1处实现了一个JTree的监听器接口;第2处将node转换成String型。
三、综合应用题
第l处:extendsJPanelimplementsMouseMotionListener.
第2处:super.paintComponent(g)
第3处:contentPane.add(newMousePanel())
【解析】第1处是继承Jpanel实现鼠标移动监听器接口;第2处以g为参数重新绘制组件;第3处在contentPane内容面板中添加一个MousePanel鼠标面板。