1、计算机二级 JAVA-145及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是计算 110(包括 1和 10)中除 5以外各个自然数的和。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 public class basic public static void main (String args) int i = 1; int sum = 0; while(i = 10) if(i = 5) _; _; _; i+; System.out.println (“sum=“+sum); (分数:30.00)
2、_二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是通过滑动条修改颜色的 RGB值,从而控制颜色。程序中有一个面板、3 个标签和 3个滑动条,标签和滑动条一一对应,分别对应三原色红、绿、蓝,任意拖动其中的一个滑动条,所对应的颜色值就会发生变化,面板的颜色也会对应地发生变化。滑动条值的范围是 0255。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class simple extends JFrame imple
3、ments AdjustmentListener public simple() setTitle(“simple“); setSize(300,200); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit (0); ); Container contentPane =_; JPanel p = new JPane1(); p.setLayout(new GridLayout(3,2); p.add(redLabel = new JLabel(“Red 0“);
4、p.add(red = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255); red.setBlockIncrement(16); red.addAdjustmentListener(this); p.add(greenLabel = new JLabel(“Green 0“); p.add(green = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255); green.setBlockIncrement(16); green.addAdjustmentListener(this); p.add(blue
5、Label = new JLabel(“Blue 0“); p.add(blue = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255); blue.setBlockIncrement(16); blue.addAdjustmentListener(this); contentPane.add(p,“South“); colorPanel = new JPanel(); colorPanel.setBackground(new Color(0,0,0); contentPane.add(colorPanel,“Center“); public voi
6、d adjustmentValueChanged(AdjustmentEvent evt) redLabel.setText(“Red “ + red.getValue(); greenLabel.setText(“Green “ + green.getValue(); blueLabel.setText(“Blue “ + blue.getValue(); colorPanel.setBackground(new Color(red.getValue().green.getValue(),blue.getValue(); _; public static void main(String a
7、rgs) JFrame f = new simple(); f.show(); private JLabel redLabel; private JLabel greenLabel; private JLabel blueLabel; private JScrollBar red; private JScrollBar green; private JScrollBar blue; private JPanel colorPanel; (分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是监听对于列表项的操作。窗口中有一个列表和“添加”、“删除”和“关闭”三
8、个按钮。单击“添加”按钮,会在当前所选列表项后添加一个名为“新增表项”的列表项,同时后台输入列表中的表项数量。单击“删除”按钮,如果未选中表项,则弹出提示消息框“请选择表项”,否则将选中的表项删除,同时后台输出删除表项的内容和列表中的表项数量。单击“关闭”按钮退出程序。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; public class advance public static void main(Stri
9、ng args) final Frame frmFrame = new Frame(); Panel pnlPanel1 = new Panel(); Panel pnlPanel2 = new Panel(); final List istList = new List(8); for(int i = 0; i 10; i+) String strName = “表项“ + (new Integer(i+1).toString(); istList.add( strName ); Button btnButton1 = new Button(“添加“); Button btnButton2
10、= new Button(“删除“); Button btnButton3 = new Button(“关闭“); btnButton1.addActionListener( new ActionListener() publiC void actionPerformed(ActionEvent e) lstList.add(“新增表项“,istList.getSelected()+1); System.out.println(“列表中的表项数量“ + istList,getItemCount() ); ); btnButton2.addActionListener (new ActionLi
11、stener () public void actionPerformed(ActionEvent e) if(istList.getSelected() = null) JOptionPane.showMessageDialog(frmFrame,“请选择表项“); return; System.out.println(“删除表项的内容:“+ istList,getSelectedItem(); istList.delete(istList.getSelectedIndex(); System.out.println(“列表中的表项数量: “ + istList,getItemCount (
12、); ); btnButton3.addActionListener(new ActionListener () public void actionPerformed(ActionEvent e) System.exit (0); ); pnlPanel1 .add (istList); pnlPanel2 .add (btnButton1); pnlPanel2 .add (btnButton2); pnlPanel2,add (btnButton3); frmFrame.add(“North“,pnlPanel1); frmFrame.add(“South“,pnlPanel2); fr
13、mFrame,setTitle (“advance“); frmFrame.pack (); frmFrame,show (); (分数:30.00)_计算机二级 JAVA-145答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是计算 110(包括 1和 10)中除 5以外各个自然数的和。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 public class basic public static void main (String args) int i = 1; int sum = 0; while
14、(i = 10) if(i = 5) _; _; _; i+; System.out.println (“sum=“+sum); (分数:30.00)_正确答案:()解析:i+。 continue。 sum=sum+i。二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是通过滑动条修改颜色的 RGB值,从而控制颜色。程序中有一个面板、3 个标签和 3个滑动条,标签和滑动条一一对应,分别对应三原色红、绿、蓝,任意拖动其中的一个滑动条,所对应的颜色值就会发生变化,面板的颜色也会对应地发生变化。滑动条值的范围是 0255。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行
15、)。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class simple extends JFrame implements AdjustmentListener public simple() setTitle(“simple“); setSize(300,200); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit (0); ); Container con
16、tentPane =_; JPanel p = new JPane1(); p.setLayout(new GridLayout(3,2); p.add(redLabel = new JLabel(“Red 0“); p.add(red = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255); red.setBlockIncrement(16); red.addAdjustmentListener(this); p.add(greenLabel = new JLabel(“Green 0“); p.add(green = new JScrollBar
17、(Adjustable.HORIZONTAL,0,0,0,255); green.setBlockIncrement(16); green.addAdjustmentListener(this); p.add(blueLabel = new JLabel(“Blue 0“); p.add(blue = new JScrollBar(Adjustable.HORIZONTAL,0,0,0,255); blue.setBlockIncrement(16); blue.addAdjustmentListener(this); contentPane.add(p,“South“); colorPane
18、l = new JPanel(); colorPanel.setBackground(new Color(0,0,0); contentPane.add(colorPanel,“Center“); public void adjustmentValueChanged(AdjustmentEvent evt) redLabel.setText(“Red “ + red.getValue(); greenLabel.setText(“Green “ + green.getValue(); blueLabel.setText(“Blue “ + blue.getValue(); colorPanel
19、.setBackground(new Color(red.getValue().green.getValue(),blue.getValue(); _; public static void main(String args) JFrame f = new simple(); f.show(); private JLabel redLabel; private JLabel greenLabel; private JLabel blueLabel; private JScrollBar red; private JScrollBar green; private JScrollBar blue
20、; private JPanel colorPanel; (分数:40.00)_正确答案:()解析:getContentPane()。 colorPanel.repaint()。三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是监听对于列表项的操作。窗口中有一个列表和“添加”、“删除”和“关闭”三个按钮。单击“添加”按钮,会在当前所选列表项后添加一个名为“新增表项”的列表项,同时后台输入列表中的表项数量。单击“删除”按钮,如果未选中表项,则弹出提示消息框“请选择表项”,否则将选中的表项删除,同时后台输出删除表项的内容和列表中的表项数量。单击“关闭”按钮退出程序。程序中存在若干
21、错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; public class advance public static void main(String args) final Frame frmFrame = new Frame(); Panel pnlPanel1 = new Panel(); Panel pnlPanel2 = new Panel(); final List istList = new List(8); f
22、or(int i = 0; i 10; i+) String strName = “表项“ + (new Integer(i+1).toString(); istList.add( strName ); Button btnButton1 = new Button(“添加“); Button btnButton2 = new Button(“删除“); Button btnButton3 = new Button(“关闭“); btnButton1.addActionListener( new ActionListener() publiC void actionPerformed(Actio
23、nEvent e) lstList.add(“新增表项“,istList.getSelected()+1); System.out.println(“列表中的表项数量“ + istList,getItemCount() ); ); btnButton2.addActionListener (new ActionListener () public void actionPerformed(ActionEvent e) if(istList.getSelected() = null) JOptionPane.showMessageDialog(frmFrame,“请选择表项“); return;
24、 System.out.println(“删除表项的内容:“+ istList,getSelectedItem(); istList.delete(istList.getSelectedIndex(); System.out.println(“列表中的表项数量: “ + istList,getItemCount (); ); btnButton3.addActionListener(new ActionListener () public void actionPerformed(ActionEvent e) System.exit (0); ); pnlPanel1 .add (istLis
25、t); pnlPanel2 .add (btnButton1); pnlPanel2 .add (btnButton2); pnlPanel2,add (btnButton3); frmFrame.add(“North“,pnlPanel1); frmFrame.add(“South“,pnlPanel2); frmFrame,setTitle (“advance“); frmFrame.pack (); frmFrame,show (); (分数:30.00)_正确答案:()解析:第 23行的“lstList.getSelected()+1“改为“lstList.getSelectedIndex()+1“。 第 30行的“lstList.getSelected()=null“改为“lstList.getSelectedItem()=null“。 第 36行的“lstList.delete(lstList.getSelectedIndex()“改为“lstList.remove(lstList.get SelectedIndex()“。