荟萃馆

位置:首页 > 计算机 > java语言

计算机二级考试JAVA上机试题及答案

java语言7.35K

平时的练习对我们的考试来说十分重要,练习做得多了自然熟能生巧。下面是本站小编为大家收集整理的计算机二级考试JAVA上机试题及答案,希望对大家学习有所帮助!

计算机二级考试JAVA上机试题及答案

  一、基本操作题

在下画线上补充代码。

本题的功能是输出字符串"welcome!"和表达式"a/b=

c",以及输出"\do something"。

Public class javal{

Pubhc static void main(string[]args){

tln( );

tln( );

tln( );

}

  二、简单应用题

下面的程序中,有两个文本框,Input和Output,用户在Input文本框中输入字符串后回车,字符串会在Output文本框中出现。

import .*;

import t.*;

public class java2 extends Frame{

public static void main(String args[]){

java2 f=new java2("java2");

Panel pan=new Panel();

();

}

pubhc java2(String str){

super(str);

}

public void init(){

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

(O);

}

}};

setSize(200,200);

setLayout(new FlowlLayout());

final TextField tfl=new TextField(20):

final TextField tf2=new TextField(20);

final Label hi—new Label("Input:");

final Label la2一new Label("Output:");

ctionListener( {

public void actionPerformed(ActionEvent e){

ex( );

}

});

add(la1);

add(tfl);

add(la2);

add(tf2);

setVisible(true);

}

}

  三、综合应用题

本题的功能是展示4种不同的对话框。窗口中有4个按钮:“消息”、“输入”、“确定”和“选择”,单击任意一个按钮,就能弹出一个对应的对话框。其中,消息对话框只有一个提示信息和一个“确定’’按钮,输入对话框有一个供输入的`文本框及“确定”和“撤销”两个按钮;确定对话框中有一个提示信息和三个按钮“是”、“否”和“撤销”;而选择对话框中有一个提示信息和两个按钮“确定,,和“取消”。

import 9.*:

import t.*;

import .*;

Public class java3 extends JFrame implements ButtonSelecte ActionListener

{

JButton btnMessage=new JButton("消息");

JButton btnlnput=new JButton("输入");

JButton btnConfirm=new JButton("确认");

JButton btnOption=new JButton("选择");

public java3()

{

super("java3");

ctionListener(this);

ctionListener(this);

ctionListener(this);

ctionListener(this);

getContentPane()ayout( new FIowLavout

()):

getContentPane()(btnMessage);

getContentPane()(btnlnput):

getContentPane()(btnConfirm):

getContentPane()(btnOption);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

(0);

}

});

}

public static void main(String args[])

{

java3 fr=new java3();

();

isible(true);

}

Public void actionperformed(ActionEvent e)

{

Object[]opt={"确认","取消");

JButton instance:(JButton)bject();

if(instance= =btnMessage)

MessageDialog(this,"消息对话框");

else if(instance= =btnInput、

InputDialog(this,"输入对话框");

else if(instance= =btnConfirm、

ConfirmDialog(this,"确认对话框");

else

OptionDialog(this,"选择对话框","选择",_OPTION,TION_MESSAGE,null,opt,opt[1]);

}

}