2016年二级JAVA考试预测试卷及答案

  一、基本操作题(共18分)

2016年二级JAVA考试预测试卷及答案

本题分别比较两个字符串"A"和"a"是否相等,并比较两个字符"A"和"a"是否相等,并输出比较结果。

public class javal{

public static void main(String[]args){

;

cl=’A ’;c2=’a’;

String strl=new String("A"),str2=new String

("a"):

if( )

tln("char"+c1+"equals"+"char"

+c2);

else

tln("char"+cl+"doesn’t equal

"+"char"+c2);

if( )

tln("string"+strl+"equals"+

"string"+str2):

else

tln("string"+strl+"doesn’t e-

qual"+"string"+str2); .

}

}

  二、简单应用题(共24分)

本题是一个Applet,页面上有一个按钮“请单击”,单击该按钮后弹出一个对话框,对话框上有三个按钮“橙色”、“蓝色”和“红色”,单击其中任意一个按钮,则可以将对话框的背景色设置为按钮名称所对应的颜色。

import .*;

import t.*;

import g.*;

public class java2 extends JApplet

{

private JFrame framet;

()

{

frame=new JFrame();

itle("java2");

ize(300,200);

ontentPane()(new ButtonPanel

());

JButton PopButton=new JButton("请单击");

getContentPane()(PopButton);

PopButtom addActionListener(new ActionListener();

{

public void actionPerformed(ActionEvent evt)

{

if(sible())isible(false);

else ;

}

});

}

}

class ButtonPanel extends JPanel

{

private class ColorAction implements ActionLis-

tener

{

private Color backgroundColor;

public void actionPerformed(ActionEvent action-

event)

{

setBackground(backgroundColor);

repaint();

}

public ColorAction(Color color)

{

backgroundColor=color;

}

}

public ButtonPanel()

{

JButton jbutton=new JButton("橙色");

JButton jbuttonl=new JButton("蓝色");

JButton jbutton2=new JButton("红色");

add(jbutton);

add(jbuttonl);

add(jbutton2);

ColorAction coloraction=new ColorAction(ge);

ColorAction coloractionl=new ColorAction();

ColorAction coloraetion2=new ColorAction();

ctionListener(coloraction);

ctionListener(coloractionl);

ctionListener(coloraction2);

}

}

  三、综合应用题(共18分)

本题的功能是用按钮来控制文本框中文本的.颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text color control”,窗口的底部还有一个复选按钮“Disable changes”。在“Sample text”面板中有一个带有字符串的文本框,而在“Text color control”面板中有三个按钮:“Black”、“Red”和“Green”,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框中的文本变成对应的颜色,如果选中“Disable changes”复选项,则三个颜色按钮变为不可用,如果取消选中复选项,则三个按钮变为可用。

import g.*;

import .*;

import t.*;

public class java3 extends JFrame{

private JPanel upper,middle,lower;

private JTextField text;

private JButton black,red,green;

private JCheckBox disable;

public java3(String titleText){

super(titleText);

addWindowListener(new WindowAdapter(){

public void

windowClosing(WindowEvent e){

(0);

}

}

);

upper=new JPanel();

order(teTitledBor-

der("Sample text"));

ayout(new BorderLayout());

text=new JTextField("Change the color of this

text"):

(text,ER);