请完成下列Java程序。程序的执行结果是生成一个具有一个按钮的窗体,并且按钮的标签是“欢迎参加全国计算机等级考试—Java部分!”字样。
注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent
{
public static void main(String args[])
{
Frame. f=new Frame("Test");
Button b=new Button("欢迎参加Java考试!");
TestListener testmonitor=new TestListener();
Monitor bh=new Monitor();
b.addActionListener(bh);
f.addwindowListener(testmonitor);
f.add(b);
f.setSize(150,100);
f.setVisible(true);
}
}
class Monitor ___________ ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("a button has been pressed");
}
}
class TestListener__________WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
第1题:
请完成下列Java程序。实例listener是监听器,frm是事件源,fr上发生的事件委托tat进行处理。程序的执行结果是显示一个蓝色的窗口,单击关闭按钮,可关闭窗口。
注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
源程序文件代码清单如下:
import java.awt.*;
import java.awt.event.*;
public class BlueWindow
{
public static void main(String args[])
{
Frame. frm=new Frame. ("欢迎参加Java考试!");
TheAdapterTest listener=new TheAdapterTest();
frm. ______;
frm.setSize(200,200);
frm.setBackground(Color.blue);
frm. ______;
}
}
class TheAdapterTest extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
第2题:
请完成下列Java程序:实现打印出自己的源文件的功能。
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
import java.io.*;
import java.util.StringTokenizer;
public class ex27_2{
public static void main(String args[])throws IOException{
FileInputStream fis=new FileInputStream("ex27_2.java");
DataInputStream dis=new DataInputStream(fis);
String str=null;
while(true){
__________________;
if(str==null){
__________________;
}
StringTokenizer st=new StringTokenizer(str);
while(st.hasMoreTokens()){
System.out.print(st.nextToken()+ " " );
}
System.out.println();
}
}
}
第3题:
阅读以下说明和Java代码,将解答写入对应栏内。
【说明】
请完成下列Java程序。程序的执行结果是生成一个具有一个TextField类型的对象in、 Button类型的对象btn和Label类型的对象out图形用户界面,程序的功能是计算用户输入数的平方,如图3所示。
注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
【程序】
import java. awt.*;
import java, awt. event.*;
public class square {
public static void main(String args[ ]){
(1)
}
}
class AppFrame. extends Frame{
TheAdapterTest listener = new TheAdapterTest( );
Text Field in = new TextField (5);
Button btn = new Button("计算");
Label ut = new Label("用于显示计算结果");
public AppFrame( )
{
setLayout( new FlowLayout( ));
add(in);
add(btn)
add(out);
btn. addActionListener( new BtnActionAdapter( ));
addWindowListener (listener);
setSize(400,100);
show( );
}
class BtnActionAdapter implements (2) {
public void actionPerformed((3)) {
String s = in. getText( );
double d =(4)
double sq = d * d;
out. setText(d+"的平方是:" +sq);
}
}
class TheAdapterTest extends WindowAdapter
{
public void windowCIosing((5))
{
System. exit(1)
}
}
}
第4题:
下面的程序是用do_while语句计算10的阶乘。请在程序的每条横线处填写一个语句,使程序的功能完整。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
源程序文件代码清单如下:
public class DoWhileLoop
{
public static void main(________)
{
int n=10;
long result=1;
do
{
_______________
}
______________
System.out.println("10的阶乘为: "+result);
}
}
第5题:
请完成下列Java程序。程序的输出结果:a=6,b=5。
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
程序运行结果如下:
public class ex38_2{
public static void main(String args[])
{
int a=5,b=6;
a=_________;
b=a-b;
a=_________;
System.out.println("a="+a+"\tb="+b);
}
}