试题 百分网手机站

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

时间:2020-09-26 16:22:18 试题 我要投稿

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( )

  System.Out.println("char"+c1+"equals"+"char"

  +c2);

  else

  System.OUt.println("char"+cl+"doesn’t equal

  "+"char"+c2);

  if( )

  System.out.println("string"+strl+"equals"+

  "string"+str2):

  else

  System.OUt.println("string"+strl+"doesn’t e-

  qual"+"string"+str2); .

  }

  }

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

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

  import java.awt.*;

  import java.awt.event.*;

  import javax.swing.*;

  public class java2 extends JApplet

  {

  private JFrame framet;

  ()

  {

  frame=new JFrame();

  frame.setTitle("java2");

  frame.setSize(300,200);

  frame.getContentPane().add(new ButtonPanel

  ());

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

  getContentPane().add(PopButton);

  PopButtom addActionListener(new ActionListener();

  {

  public void actionPerformed(ActionEvent evt)

  {

  if(frame.isVisible())frame.setVisible(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(Color.orange);

  ColorAction coloractionl=new ColorAction(Color.blue);

  ColorAction coloraetion2=new ColorAction(Color.red);

  ibutton.addActionListener(coloraction);

  ibuttonl.addActionListener(coloractionl);

  jbutton2.addActionListener(coloraction2);

  }

  }

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

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

  import javax.swing.*;

  import java.awt.*;

  import java.awt.event.*;

  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){

  System.exit(0);

  }

  }

  );

  upper=new JPanel();

  upper.setBorder(BorderFactory.ereateTitledBor-

  der("Sample text"));

  upper.setlayout(new BorderLayout());

  text=new JTextField("Change the color of this

  text"):

  upper.add(text,BorderLayout.CENTER);