2015年全国计算机等级考试《二级JAVA》考前巅峰冲刺试题(3)

0
收藏   分享
  • 卷面总分:120分
  • 试卷类型:模拟考试
  • 测试费用:免费
  • 答案解析:是
  • 练习次数:155次
  • 作答时间:120分钟
试卷简介

2015年全国计算机等级考试《二级JAVA》考前巅峰冲刺试题(3),本试卷总分120分,共有3类型题目。

  • 基本操作题
  • 简单应用题
  • 综合应用题
试卷预览
1

本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。

import java.awt.*;

import java.awt.event*;

import java.awt.geom.*;

import javax.swing.*;

import javax.swing.event.*;

public class java3

{

public static void main(String[]args)

{

TextTestFrame. frame=new TextTestFrame():

frame.setDefauhCloseOperation(JFrame.EXIT_

0N_CLOSE);

frame.show();

}

}

class TextTestFrame. extends JFrame

{

public TextTestFrame()

{

setTitle("java3"):

setSize(DEFAULT_WIDTH,DEFAULT_

HElGHT);

Container contentPane=getContentPane();

DocumentListener listener=new DoeumentListen-

er()

JPanel panel=new JPanel();

hourField=new JTextField("12",3);

panel.add(hourField);

hourField.getDocument().addDocumentListener

(this)

minuteField=new JTextField("00",3):

panel.add(minuteField);

minuteField.getDocument().addDocumentListener

(listener);

contentPane.add(panel,BorderLayout.S()UTH);

clock=new ClockPanel();

contentPane.add(clock,BorderLayout.CEN-

TER);

}

public void setClock()

{

try

{

int hours

=Integer.parseInt(hourField.getText().trim

()):

int minutes

=Integer.parseInt(minuteField.getText().trim

());

clock.setTime(hours,minutes);

}

catch(NumberFormatExcepfion e){}

}

public static final int DEFAULT_WIDTH=300;

public static final int DEFAULT_HEIGHT

=300;

private J TextField hourField;

private JTextField minuteField;

private ClockPanel clock;

private class clockFieldListener extends Docu-

mentListener

{

public void insertUpdate(DocumentEvent e){ set-

Clock();}

public void removeUpdate(DocumentEvent e){

setClock();}

public void changedUpdate(DocumentEvent e){}

}

}

class ClockPanel extends JPanel

{

pubhc void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphies2D g2=(Graphics2D)g;

Ellipse2D circle

=new Ellipse2D.Double(0,0,2* RADIUS,2

*RADIUS);

g2.draw(circle);

double hourAngle

=Math.toRadians(90-360*minutes/(12

*60));

drawHand(92,hourAngle,HOUR_HAND_

LENGTH);

double minuteAngle

=Math.toRadians(90-360*minutes/60);

drawHand(g2,minuteAngle,MINUTE_HAND_

LENGTH):

}

punic void drawHand(Graphics2D g2,

double angle,double handLength)

{

Point2D end=new Point2D.Double(

RADIUS+handLength*Math.cos(angle),

RADIUS-handLength*Math.sin(angle));

Point2D center=new Point2D.Double(RADIUS,

RADIUS):

g2.draw(new Line2D.Double(center,end));

}

public void setTime(int h,int m)

{

minutes=h*60+m;

repaint();

}

private double minutes=0;

private double RADIUS=100;

private double MINUTE_HAND_LENGTH=

0.8*RADIUS;

private double HOUR_HAND_LENGTH=0.6

*RADIUS:

}

1

本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text col—or control”,窗口的底部还有一个复选按钮“Disable chan-ges”。在“Sample text”面板中有一个带有宇符串的文本框,而在“Text color control”面板中有三个按钮“Black”、“Red”和“Green”,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框的文本变成对应的颜色,如果选中“Disa-ble 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);

middle=new JPanel();

middle.setBorder(BorderFactory.createTitledBor-

der("Text color control"));

middle.setLayout(new FlowLayout(FlowLayout.

CENTER)):

black=new JButton("Black",new ColorIcon(

Color.black));

black.addActionListener(new ButtonListener(

Color.black));

middle.add(black);

red=new JButton("Red",new ColorIeon(Col-

or.red));

red.addActionListener(new ButtonListener(Col-

or.red));

middle.add(red);

green=new JButton("Green",new ColorIcon(

Color.green));

green.addActionListener(new ButtonListener(

Color.green));

middle.add(green);

lower=new JPanel();

lower.setLayout(new FlowLayout(FlowLayout.

RIGHT));

disable=new JCheckBox("Disable changes");

disable.addhemListener(new hemListener(){

public void itemStateChanged(hemEvent e){

boolean enabled

= (e.getStateChange()

= =ItemEvent.DESELECTED);

black.setEnabled(enabled);

red.setEnabled(enabled);

green.setEnabled(enabled);

}

}

);

lower.add(disable):

Container cp=getContentPane();

cp.add(upper,BorderLayout.NORTH);

ep.add(middle,BorderLayout.CENTER);

ep.add(10wer,BorderLayout.SOUTH);

pack();

setVisible(true);

}

class ButtonListener extends ActionListener{

private Color c;

public ButtonListener(Color c)f

this.c=c;

}

public void aetionPerformed(ActionEvent e){

text.setForeground(c);

}

}

class ColorIcon implements Icon{

private Color c;

private static final int DIAMETER=10;

public Colorlcon(Color c){

c=c

}

public void paintleon(Component cp,Graphics g,

int X,int Y){

g.setColor(e);

g.fillOval(x,Y,DIAMETER,DIAMETER);

g.setColor(Color.black);

g.drawOval(x,y,DIAMETER,DIAMETER);

}

public int getIconHeight(){

return DIAMETER:

}

public int getlconWidth(){

return DIAMETER;

}

}

public static void main(String[]args){

new java3("java3");

}

}

1

本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“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);

middle=new JPanel();

middle.setBorder(BorderFactory.createTitledBor-

der("Text color control"));

middle.setLayout(new FlowLayout(FlowLayout.

CENTER)):

black=new JButton("Black",new ColorIcon

(Color.black));

black.addActionListener( new ButtonListener

(Color.black));

middle.add(black);

red=new JButton("Red",new ColorIcon(Col-

or.red));

red.addActionListener(new ButtonListener(Col-

or.red));

middle.add(red);

green=new JButton("Green",new ColorIcon

(Color.green));

green.addActionListener(new ButtonListener

(Color.green));

middle.add(green);

lower=new JPanel();

lower.setLayout(new FlowLayout(FlowLayout.

RIGHT));

disable=new JCheckBox("Disable changes"):

disable.addItemListener(new ItemListener()(

public void itemStateChanged(ItemEvent e){

boolean enabled

=(e.getStateChange()

= =ItemEvent.DESELECTED):

black.setEnabled(enabled);

red.setEnabled(enabled);

green.setEnabled(enabled);

}

}

);

lower.add(disable);

Container cp=getContentPane();

cp.add(upper,BorderLayout.NORTH);

cp.add(middle,BorderLayout.CENTER);

cp.add(10wer,BorderLayout.SoUTH);

pack();

setVisible(true);

}

class ButtonListener extends ActionListener{

private Color c;

public ButtonListener(Color c){

this.c=c;

}

public void actionPerformed(ActionEvent e){

text.setForeground(c);

}

}

class ColorIcon implements Icon{

private Color c;

private static final int DIAMETER=10;

public ColorIcon(Color c){

c=c

}

public void paintlcon(Component cp,Graphics g,

int x,int y){

g.setColor(c);

g.fillOval(X,y,DIAMETER,DIAMETER);

g.setColor(Color.black);

g.drawOval(x,y,DIAMETER,DIAMETER);

}

public int getlconHeight(){

return DIAMETER;

}

public int getlconWidth(){

return DIAMETER;

}

}

public static void main(String[]args){

new java3("advance");

}

}

1

本题中定义了一个简单的计算器,可以进行基本的四则运算。程序中包含16个按钮用来表示0~9、+、-、 *、/、一运算符和小数点,程序顶部的文本框用来显示操作数以及结果。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class java2{

public static void main(String[]args){

try{

UIManager.setLookAndFeel(UIManager.getSys-

temLookAndFeelClassName());

}

catch(Exception e){}

JFrame. frame=new CalculatorFrame();

frame.show();

}

}

class CalculatorPanel extends JPanel implements Ac-

tionListener{

private JTextField display;

private JButton btn;

private double arg=0;

private String p="=";

private boolean start=true;

public CalculatorPanel(){

setLayout(new BorderLayout());

display=new JTextField("0");

display.setEditable(false);

add(display,"North");

JPanel P=new JPanel();

P.setLayout(new GridLayout(4,4));

String buttons="789/456*123-0.=+":

for(int i=0;i<;buttons.length();i++){

btn=new JButton(buttons.substring(i,i+

1));

P.add(btn);

____;

}

add(P,"Center");

}

public void actionPerformed(ActionEvent evt){

String s=evt.getActionCommand();

if(‘0 ‘<;=s.charAt(0)&&s.charAt(O)<;=‘

9‘‖ s.equals("-")){

if(start)display.setText(s):

else display.setText(display.getText()+s);

start=false;

}

else{

if(start){

if(s.equals("-")){

display.setText(s):

start=false;

}

else p=S;

}

else(

double x=____

calculate(x);

op=S:

start=true;

}

}

}

public void calculate(double n){

if(op.equals("+"))arg+=n:

else if(op.equals("-"))arg-=n;

else if(op.equals("*"))arg*=n;

else if(op.equals("/"))arg/=n;

else if(op.equals("="))arg=n;

display.setText(""+arg);

}

}

class CalculatorFrame. extends JFrame{

public CalculatorFrame(){

setTitle("java2");

setSize(220,180);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

Container contentPane=getContentPane();

contentPane.add(new CalculatorPanel());

}

}

1

本题使用下拉菜单来控制字体,窗口中有一个标签和一个下拉菜单,当选中下拉菜单中的任一项字体时,标签上字符串的字体就随之改变。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class ComboBoxFrame. extends JFrame.____{

public ComboBoxFrame(){

setTitle("java2");

setSize(300,200);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

style=new JComboBox():

style.setEditable(true);

style.addhem("Serif");

style.addItem("SansSerif");

style.addhem("Monospaced");

style.addhem("Dialog");

style.addhem("Dialoglnput");

style.addActionListener(this);

JPanel p=new JPanel();

P.add(style);

getContentPane().add(p,"South");

panel=new ComboBoxTestPanel();

getContentPane().add(panel,"Center");

}

public void actionPerformed(ActionEvent evt){

JComboBox source=(JComboBox)____;

String item=(String)source.getSelectedhem():

panel.setStyle(item);

}

private ComboBoxTestPanel panel;

private JComboBox style;

}

class ComboBoxTestPanel extends JPanel{

public ComboBoxTestPanel(){

setStyle("Serif");

}

public void setStyle(String s){

setFont(new Font(S,Font.PLAIN,12));

repaint();

}

public void paintComponent(Graphics g){

super.paintComponent(g);

9.drawString("Welcome to China!",0,50);

}

}

public class java2{

public static void main(String[]args){

JFrame. frame=new ComboBoxFrame();

frame.show();

}

}

1

本题的功能是对下拉菜单项的操作,包括添加和删除。页面包括一个下拉菜单、一个文本框和两个按钮“删除”和“添加”,选中下拉菜单的一项后,可以通过“删除”按钮从下拉菜单中删除该项,在文本框中填入字符串后,单击“添加”按钮就可以将该项添加到下拉菜单中,所有信息都将显示在右侧的文本域中。

import java.awt.*;

import java.awt.event.*;

public class java2 extends java.applet.Applet imple-

ments hemListener,ActionListener

{Choice choice;

TextField text;

TextArea area;

Button add,del;

public void init() .

{choice:new Choice();

text=new TextField(8);

area:new TextArea(6,15);

choice.add("音乐天地");

choice.add("武术天地");

choice.add("象棋乐园");

choice.add("交友聊天");

add=new Button("添加");

del=new Button("删除");

add.addActionListener(this);

del.addActionListener(this);

choice.addItemListener(this);

add(choice);

add(del);add(text);add(add);add(area);

}

public void itemStateChanged(hemEvent e)

{String name=____;

int index=choice.getSelectedIndex();

area.setText("\n"+index+":"+name);

}

public void actionPerformed(ActionEvent e)

{if(e.getSource()= =add||e.getSource()= =

text)

{String name=text.getText();

if(name.length()>;0)

{choice.add(name);

choice.select(name);

area.append("\n添加"+name);

}

}

else if(e.getSource()= =del)

{choice.remove(____);

area.append("\n删除"+choice.getSelectedItem

());

}

}

}