1、二级 JAVA 机试-127 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.本题提示输入年份,然后判断该年份是否为闰年。import java.io.*;public class java1public static void main(Stringargs)InputStreamReader ir;BufferedReader in;ir=new InputStreamReader(System.in);in=new BufferedReader(ir);int year=1900;System.out.print(“请输入年份:“)
2、;tryString s=in.readLine();_;_(Exception e)if(_)System.out.println(year+“是闰年“);elseSystem.out.println(year+“不是闰年“);(分数:30.00)_二、简单应用题(总题数:1,分数:40.00)2.本题使用下拉菜单来控制字体,窗口中有一个标签和一个下拉菜单,当选中下拉菜单中的任一项字体时,标签上字符串的字体就随之改变。import java.awt.*;import java.awt.event.*;import javax.swing.*;class ComboBoxFrame exten
3、ds 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.addItem(“Serif“);style.addItem(“SansSerif“);style.addItem(“Monospaced“);style.addItem(“
4、Dialog“);style.addItem(“DialogInput“);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)sour
5、ce.getSelectedItem();panel.setStyle(item);private ComboBoxTestPanel panel;private JComboBox style;class ComboBoxTestPanel extends JPanelpublic ComboBoxTestPanel()setStyle(“Serif“);public void setStyle(String s)setFont(new Font(s,Font.PLAIN,12);repaint();public void paintComponent(Graphics g)super.pa
6、intComponent(g);g.drawstring(“Welcome to China!“,0,50);public class java2public static void main(Stringargs)JFrame frame=new ComboBoxFrame();frame.show();(分数:40.00)_三、综合应用题(总题数:1,分数:30.00)3.本题是一个 Applet,功能是监听用对于文本域中文本的选择。页面中有一个文本域、一个“复制”按钮和一个文本框,选中文本域中部分文字后,单击按钮“复制”,所选文字将显示在文本框中。import java.applet.A
7、pplet;import java.awt.*;import java.awt.event.*;public class java3 extends Applet implements ActionListenerTextArea ta=new TextArea(5,30);TextField tf=new TextField(30);Button button=new Button(“复制“);String text=“AWT 提供基本的 GUI 组件,/n“+“具有可以扩展的超类,/n“+“它们的属性是继承的。/n“;public void init()setLayout(new Flow
8、Layout(FlowLayout.left);ta.setText(text);ta.setEditable(true);add(ta);add(button);add(tf);ta.addActionListener(this);public void actionPerformed(ActionEvent e)String s;s=ta.getSelectText();if(e.getSource()=button)tf.setText(s)s=ta.getSelectText()(分数:30.00)_二级 JAVA 机试-127 答案解析(总分:100.00,做题时间:90 分钟)一、
9、基本操作题(总题数:1,分数:30.00)1.本题提示输入年份,然后判断该年份是否为闰年。import java.io.*;public class java1public static void main(Stringargs)InputStreamReader ir;BufferedReader in;ir=new InputStreamReader(System.in);in=new BufferedReader(ir);int year=1900;System.out.print(“请输入年份:“);tryString s=in.readLine();_;_(Exception e)i
10、f(_)System.out.println(year+“是闰年“);elseSystem.out.println(year+“不是闰年“);(分数:30.00)_正确答案:(第 1 处:year=Integer.parseInt(s)第 2 处:catch第 3 处:year%4=0import java.awt.event.*;import javax.swing.*;class ComboBoxFrame extends JFrame_public ComboBoxFrame()setTitle(“java2“);setSize(300,200);addWindowListener(ne
11、w WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););style=new JComboBox();style.setEditable(true);style.addItem(“Serif“);style.addItem(“SansSerif“);style.addItem(“Monospaced“);style.addItem(“Dialog“);style.addItem(“DialogInput“);style.addActionListener(this);JPanel p=new JPanel
12、();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.getSelectedItem();panel.setStyle(item);private ComboBoxTestPanel panel;private JComb
13、oBox style;class ComboBoxTestPanel extends JPanelpublic ComboBoxTestPanel()setStyle(“Serif“);public void setStyle(String s)setFont(new Font(s,Font.PLAIN,12);repaint();public void paintComponent(Graphics g)super.paintComponent(g);g.drawstring(“Welcome to China!“,0,50);public class java2public static
14、void main(Stringargs)JFrame frame=new ComboBoxFrame();frame.show();(分数:40.00)_正确答案:(第 1 处:implements ActionListener第 2 处:evt.getSource()解析:解析 第 1 处是实现 ActionListener 接口,程序中有窗口监听器的注册;第 2 处返回ActionEvent 动作事件的最初发生对象。三、综合应用题(总题数:1,分数:30.00)3.本题是一个 Applet,功能是监听用对于文本域中文本的选择。页面中有一个文本域、一个“复制”按钮和一个文本框,选中文本域中
15、部分文字后,单击按钮“复制”,所选文字将显示在文本框中。import java.applet.Applet;import java.awt.*;import java.awt.event.*;public class java3 extends Applet implements ActionListenerTextArea ta=new TextArea(5,30);TextField tf=new TextField(30);Button button=new Button(“复制“);String text=“AWT 提供基本的 GUI 组件,/n“+“具有可以扩展的超类,/n“+“它们
16、的属性是继承的。/n“;public void init()setLayout(new FlowLayout(FlowLayout.left);ta.setText(text);ta.setEditable(true);add(ta);add(button);add(tf);ta.addActionListener(this);public void actionPerformed(ActionEvent e)String s;s=ta.getSelectText();if(e.getSource()=button)tf.setText(s)s=ta.getSelectText()(分数:30.00)_正确答案:(第 1 处:setLayout(new FlowLayout(FlowLayout.LEFT)第 2 处:button.addActionListener(this)第 3 处:s=ta.getSelectedText()解析:解析 第 1 处是设置构件的对齐方式为左对齐的且纵横间隔都是 5 个像素的布局管理器;第 2 处是为按钮注册监听器;第 3 处是在文本域 ta 中得到选中文本,将其赋给 String 类型的 s。