1、二级 JAVA 机试-40 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题随机产生若干字母(AZ 间),直到出现字母 Q 停止。public class java1public static void main(Stringargs)_;doc=(char)( );System.out.print(c+“,“);while( );(分数:30.00)_二、简单应用题(总题数:1,分数:40.00)2.本题用复选框来控制字体的显示,窗口中有一个标签和两个复选按钮,这两个复选按钮分别对应的字体的两个特点:加粗和倾斜,任意选中一个按钮或两
2、个都选中,标签上的字符串就显示对应的字体。import java.awt.*;import java.awt.event.*;import javax.swing.*;class CheckBoxFrame extends JFrame implements ActionListenerpublic CheckBoxFrame()setTitle(“CheckBoxTest“);setSize(300,200);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0);
3、);JPanel p=new JPanel();bold=addCheckBox(p,“Bold“);italic=addCheckBox(p,“Italic“);getContentPane().add(p,“South“);panel=new CheckBoxTestPanel();getContentPane().add(panel,“Center“);public JCheckBox addCheckBox(JPanel p,String name)JCheckBox c=new JCheckBox(name);c.addActionListener( );p.add(c);retur
4、n c;public void_int m=(bold.isSelected()?Font.BOLD:0)+(italic.isSelected()?Font.ITALIC:0);panel.setFont(m);private CheckBoxTestPanel panel;private JCheckBox bold;private JCheckBox italic;class CheckBoxTestPanel extends JPanelpublic CheckBoxTestPanel()setFont(Font.PLAIN);public void setFont(int m)set
5、Font(new Font(“SansSerif“,m,12);repaint();public void paintComponent(Graphics g)super.paintComponent(g);g.drawstring(“The quick brown fox jumps over the lazy dog.“,0,50);public class java2public static void main(Stringargs)JFrame frame=new CheckBoxFrame();frame.show();(分数:40.00)_三、综合应用题(总题数:1,分数:30.
6、00)3.本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text color control”,窗口的底部还有一个复选按钮“Disable changes”。在“Sample text”面板中有一个带有字符串的文本框,而在“Text color control”面板中有三个按钮“Black”、“Red”和“Green”,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框的文本变成对应的颜色,如果选中“Disable changes”复选框,则三个颜色按钮变为不可用,如果取消选中复选框,则三个按钮变为可用。import javax.
7、swing.*;import java.awt.*;import java.awt.event.*;public class java3 extends JFrameprivate 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 voidwindow
8、Closing(WindowEvent e)System.exit(0););upper=new JPanel();upper.setBorder(BorderFactory.createTitledBorder(“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.cre
9、ateTitledBorder(“Text color control“);middle.setLayout(new FlowLayout(FlowLayout.CENTER);black=new JButton(“Black“,new Colorlcon(Color.black);black.addActionListener(new ButtonListener(Color.black);middle.add(black);red=new JButton(“Red“,new Colorleon(Color.red);red.addActionListener(new ButtonListe
10、ner(Color.red);middle.add(red);green=new JButton(“Green“,new Colorlcon(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 ItemLi
11、stener()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
12、);cp.add(lower,BorderLayout.SOUTH);pack();setVisible(true);class ButtonListener extends ActionListenerprivate Color c;public ButtonListener(Color c)this.c=c;public void actionPerformed(ActionEvent e)text.setForeground(c);class Colorlcon implements Iconprivate Color c;private static final int DIAMETE
13、R=10;public ColorIcon(Color c)c=c;public void paintIcon(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 getIconHeight()return DIAMETER;public int getIconWidth()return DIAMETER;public static void
14、main(Stringargs)new java3(“java3“);(分数:30.00)_二级 JAVA 机试-40 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题随机产生若干字母(AZ 间),直到出现字母 Q 停止。public class java1public static void main(Stringargs)_;doc=(char)( );System.out.print(c+“,“);while( );(分数:30.00)_正确答案:(第 1 处:char c第 2 处:(int)(Math.random()*26)
15、+A第 3 处:c!=Q)解析:解析 第 1 处是定义变量,从下面的变量赋值语句和输出语句可看出应为字符型变量 c;第 2 处是通过将字符 A 随机加上 026 之间的数来达到随机产生 AZ 之间字符的结果。第 3 处是 do-while 循环的终止条件。二、简单应用题(总题数:1,分数:40.00)2.本题用复选框来控制字体的显示,窗口中有一个标签和两个复选按钮,这两个复选按钮分别对应的字体的两个特点:加粗和倾斜,任意选中一个按钮或两个都选中,标签上的字符串就显示对应的字体。import java.awt.*;import java.awt.event.*;import javax.swin
16、g.*;class CheckBoxFrame extends JFrame implements ActionListenerpublic CheckBoxFrame()setTitle(“CheckBoxTest“);setSize(300,200);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););JPanel p=new JPanel();bold=addCheckBox(p,“Bold“);italic=addCheckBox(p,“Italic“
17、);getContentPane().add(p,“South“);panel=new CheckBoxTestPanel();getContentPane().add(panel,“Center“);public JCheckBox addCheckBox(JPanel p,String name)JCheckBox c=new JCheckBox(name);c.addActionListener( );p.add(c);return c;public void_int m=(bold.isSelected()?Font.BOLD:0)+(italic.isSelected()?Font.
18、ITALIC:0);panel.setFont(m);private CheckBoxTestPanel panel;private JCheckBox bold;private JCheckBox italic;class CheckBoxTestPanel extends JPanelpublic CheckBoxTestPanel()setFont(Font.PLAIN);public void setFont(int m)setFont(new Font(“SansSerif“,m,12);repaint();public void paintComponent(Graphics g)
19、super.paintComponent(g);g.drawstring(“The quick brown fox jumps over the lazy dog.“,0,50);public class java2public static void main(Stringargs)JFrame frame=new CheckBoxFrame();frame.show();(分数:40.00)_正确答案:(第 1 处:this第 2 处:actionPerformed(ActionEvent evt)解析:解析 第 1 处注册监听器进行授权,该方法的参数是事件处理的对象;第 2 处是acti
20、onPerformed 方法通过读取 ActionEvent 对象的相关信息来得到事件发生时的情况。三、综合应用题(总题数:1,分数:30.00)3.本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text color control”,窗口的底部还有一个复选按钮“Disable changes”。在“Sample text”面板中有一个带有字符串的文本框,而在“Text color control”面板中有三个按钮“Black”、“Red”和“Green”,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框的文本变成对应的颜色,如
21、果选中“Disable changes”复选框,则三个颜色按钮变为不可用,如果取消选中复选框,则三个按钮变为可用。import javax.swing.*;import java.awt.*;import java.awt.event.*;public class java3 extends JFrameprivate JPanel upper,middle,lower;private JTextField text;private JButton black,red,green;private JCheckBox disable;public java3(String titleText)s
22、uper(titleText);addWindowListener(new WindowAdapter()public voidwindowClosing(WindowEvent e)System.exit(0););upper=new JPanel();upper.setBorder(BorderFactory.createTitledBorder(“Sample text“);Upper.setlayout(new BorderLayout();text=new JTextField(“Change the color of this text“);upper.add(text,Borde
23、rLayout,CENTER);middle=new JPanel();middle.setBorder(BorderFactory.createTitledBorder(“Text color control“);middle.setLayout(new FlowLayout(FlowLayout.CENTER);black=new JButton(“Black“,new Colorlcon(Color.black);black.addActionListener(new ButtonListener(Color.black);middle.add(black);red=new JButto
24、n(“Red“,new Colorleon(Color.red);red.addActionListener(new ButtonListener(Color.red);middle.add(red);green=new JButton(“Green“,new Colorlcon(Color.green);green.addActionListener(new ButtonListener(Color.green);middle.add(green);lower=new JPanel();lower.setLayout(new FlowLayout(FlowLayout.RIGHT);disa
25、ble=new JCheckBox(“Disable changes“);disable.addhemListener(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=getContentPan
26、e();cp.add(upper,BorderLayout.NORTH);cp.add(middle,BorderLayout.CENTER);cp.add(lower,BorderLayout.SOUTH);pack();setVisible(true);class ButtonListener extends ActionListenerprivate Color c;public ButtonListener(Color c)this.c=c;public void actionPerformed(ActionEvent e)text.setForeground(c);class Col
27、orlcon implements Iconprivate Color c;private static final int DIAMETER=10;public ColorIcon(Color c)c=c;public void paintIcon(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 getIconHeight()return
28、 DIAMETER;public int getIconWidth()return DIAMETER;public static void main(Stringargs)new java3(“java3“);(分数:30.00)_正确答案:(第 1 处:upper.setLayout(new BorderLayout()第 2 处:class ButtonListener implements ActionListener第 3 处:this.c=c)解析:解析 第 1 处 Java 是大小写敏感的;第 2 处 ActionListener 是接口,应用 implements;第 3 处引用当前对象成员应用 this。