1、计算机二级 JAVA-134及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是随机产生 50个 0100 间的随机数,并计算 7080 间随机数的个数(包括 70,不包括 80)。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 public class basic public static void main(String args) int result = 0; int i = 0; int randomNum; while (i50) randomNum =_; if(_) result +;
2、_; System.out.println(“result =“ + result); (分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是:主窗口中有一个按钮,按钮的长和宽每 200ms增加 1,当达到 100时又恢复原来大小重新进行增加。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; public class simple public static void main (String args) Mywin win = new Mywin();
3、 class Mywin extends Frame _ Button b = new Button(“按钮“);int x = 5; Thread bird = null; Mywin () setBounds(100,100,400,400); setLayout(new FlowLayout(); setTitle (“simple“); setVisible (true); add(b); b.setBackground (Color.green); addWindowListener (new WindowAdapter () public void windowClosing(Wi
4、ndowEvent e) System.exit (0); ); bird = new Thread(this); bird.start(); public _ while (true) x = x+1; if(x 100) x = 5; b.setBounds(40,40,x,x); try bird.sleep (200); catch(InterruptedException e) (分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是通过菜单打开一个图像文件。窗口中有一个菜单“File”,其中包括菜单项“Open”和“Exit”。单击菜单项“Open
5、”可弹出一个打开对话框,选中一个图像文件后单击“打开”按钮返回主窗口,则选中的图像文件显示在窗口中。单击菜单项“Exit”可退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class advance public static void main(String args) JFrame frame = new ImageViewerFrame(); frame.setDefaultCl
6、oseOperation(JFrame.EXIT_ON_CLOSE); frame.show (); class ImageViewerFrame extends JFrame public ImageViewerFrame() setTitle(“advance“); setSize(DEFAULT WIDTH,DEFAULT_HEIGHT); label = new JLabel(); Container contentPane = getContentPane(); contentPane.add(label); _; chooser.setCurrentDireetory(new Fi
7、le(“.“); JMenuBar menuBar = new JMenuBar(); _; JMenu menu = new JMenu(“File“); menuBar,add (menu); JMenuItem openItem = new JMenuItem(“Open“); menu.add(openItem); openItem.addActionListener(new ActionListener() _ int r = chooser .showOpenDialog(null); if(r = JFileChooser.APPROVE_OPTION) String name
8、= chooser.getSelectedFile().getPath(); label.setIeon(new ImageIcon(name); ); JMenuItem exitItem = new JMenuItem(“Exit“); menu.add(exitItem); exitItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) System.exit(0); ); private JLabel label; private JFileChooser ch
9、ooser; private static final int DEFAULT_WIDTH=300; private static final int DEFAULT_HEIGHT=400; (分数:30.00)_计算机二级 JAVA-134答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是随机产生 50个 0100 间的随机数,并计算 7080 间随机数的个数(包括 70,不包括 80)。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 public class basic public static v
10、oid main(String args) int result = 0; int i = 0; int randomNum; while (i50) randomNum =_; if(_) result +; _; System.out.println(“result =“ + result); (分数:30.00)_正确答案:()解析:(int)(Math.random()*100)。 randomNum=70randomNum80。 i+。二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是:主窗口中有一个按钮,按钮的长和宽每 200ms增加 1,当达到 100时又恢复原
11、来大小重新进行增加。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; public class simple public static void main (String args) Mywin win = new Mywin(); class Mywin extends Frame _ Button b = new Button(“按钮“);int x = 5; Thread bird = null; Mywin () setBounds(100,100,400,400); setLa
12、yout(new FlowLayout(); setTitle (“simple“); setVisible (true); add(b); b.setBackground (Color.green); addWindowListener (new WindowAdapter () public void windowClosing(WindowEvent e) System.exit (0); ); bird = new Thread(this); bird.start(); public _ while (true) x = x+1; if(x 100) x = 5; b.setBound
13、s(40,40,x,x); try bird.sleep (200); catch(InterruptedException e) (分数:40.00)_正确答案:()解析:implements Runnable。 void run。三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是通过菜单打开一个图像文件。窗口中有一个菜单“File”,其中包括菜单项“Open”和“Exit”。单击菜单项“Open”可弹出一个打开对话框,选中一个图像文件后单击“打开”按钮返回主窗口,则选中的图像文件显示在窗口中。单击菜单项“Exit”可退出程序。请将下述程序补充完整(注意:不得改动程序的结构
14、,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class advance public static void main(String args) JFrame frame = new ImageViewerFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show (); class ImageViewerFrame extends JFrame p
15、ublic ImageViewerFrame() setTitle(“advance“); setSize(DEFAULT WIDTH,DEFAULT_HEIGHT); label = new JLabel(); Container contentPane = getContentPane(); contentPane.add(label); _; chooser.setCurrentDireetory(new File(“.“); JMenuBar menuBar = new JMenuBar(); _; JMenu menu = new JMenu(“File“); menuBar,add
16、 (menu); JMenuItem openItem = new JMenuItem(“Open“); menu.add(openItem); openItem.addActionListener(new ActionListener() _ int r = chooser .showOpenDialog(null); if(r = JFileChooser.APPROVE_OPTION) String name = chooser.getSelectedFile().getPath(); label.setIeon(new ImageIcon(name); ); JMenuItem exi
17、tItem = new JMenuItem(“Exit“); menu.add(exitItem); exitItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) System.exit(0); ); private JLabel label; private JFileChooser chooser; private static final int DEFAULT_WIDTH=300; private static final int DEFAULT_HEIGHT=400; (分数:30.00)_正确答案:()解析:chooser=new JFileChooser()。 setJMenuBar(menuBar)。 public void actionPerforme d(ActionEvent evt)。