1、二级 JAVA 机试-199 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序功能是分别比较两个字符串“A“和“a“是否相等及两个字符A和a是否相等,并输出比较结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basicpublic static void main (String args)_;c1= A;c2 = a;String str1= new String(“A“),str2 = new String(“a“);if(_)System.out.println(c1+“
2、equals “+c2);elseSystem.out.println(c1+“ doesnt equal “+c2);if(_)System.out.println(str1+“ equals “+str2);elseSystem.out.println(str1+“ doesnt equal “+str2);(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是使用 GridBayLayout 来组织窗口上的按钮。窗口中共有 7 个按钮,前 3 个按钮放置在第 1 排,第 4 个按钮独占第 2 排,第 6 个按钮位于第 3 排的右侧,而第 5 和第 7
3、个按钮共同处于第 3 排的左侧。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import javax.swing.*;import java.awt.*;import java.awt.event.*;public class simple extends Jframeprivate JPanel jpanel1=new JPanel();private GridLayout g1;private JButton JButton1 = new JButton(“第一个“);private JButton JButton2 = new JButton(“第二个“);priv
4、ate JButton JButton3 = new JButton(“第三个“);private JButton JButton4 = new JButton(“第四个“);private JButton JButton5 = new JButton(“第五个“);private JButton JButton6 = new JButton(“第六个“);private JButton JButton7 = new JButton(“第七个“);public _()super(“simple“);setSize(300,150);GridBagLayout gbl= _;jpanell.se
5、tLayout(gbl);GridBagConstraints gbc = new GridBagConstraints();gbc.fill = GridBagConstraints.BOTH;gbc.gridwidth = 1;gbc.gridheight = 1;gbc.gridx = 0;gbc.gridy = 0;jpanell.add(JButtonl,gbc);gbc.gridx = 1;jpanell.add(JButton2,gbc);gbc.gridx = 2;jpanell.add(JButton3,gbc);gbc.gridx = 0;gbc.gridy = 1;gbc
6、.gridwidth = 3;jpanell.add(JButton4,gbc);gbc.gridy = 2;gbc.gridwidth = 1;jpanell.add(JButton5,gbc);gbc.gridx = 1;gbc.gridwidth = 2;gbc.gridheight = 2;jpanell.add(JButton6,gbc);gbc.gridx = 0;gbc.gridy = 3;gbc.gridwidth = 1;gbc.gridheight = 1;jpanell.add(JButton7,gbc);this.setContentPane (jpanel1);pub
7、lic static void main (String args)simple fgl= new simple();fgl.show();fgl.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)System.exit(0););(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是用复选框来控制鼠标右键的弹出菜单是否弹出。窗口中有一个复选框“弹出菜单”,勾选该复选框后,鼠标置于窗口上,右击会弹出一个菜单,单击菜单中的命令后,后台会输出所单击的菜单项。
8、如果取消勾选该复选框,右击就不能弹出菜单。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;class CanvasWithPopup extends Canvas_;CanvasWithPopup(PopupMenu popup)enableEvents(AWTEvent.MOUSE EVENT MASK);this.popup = popup;void addPopup()add(popup);void removePopup()remove(popup);protected void pr
9、ocessMouseEvent(MouseEvent evt)if (popup.getParent() ! = null evt.isPopupTrigger()popup.show(evt.getComponent(),evt.getX(),evt.getY();super.processMouseEvent(evt);public class advance extends Frame implements ItemListener,ActionListenerCheckbox cb = new Checkbox(“弹出菜单“,false);CanvasWithPopup canvas;
10、advance()super(“advance“);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)System.exit(0););add(cb,BorderLayout.NORTH);cb.addItemListener(this);PopupMenu popup = new PopupMenu(“Button Control“);popup.add(“item1“);popup.add(“item2“);popup.addActionListener(this);canvas =
11、new CanvasWithPopup(popup);add(canvas,BorderLayout.CENTER);setSize(100,200);show();public void itemStateChanged(ItemEvent evt)_case ItemEvent.SELECTED:canvas.addPopup(); break;case ItemEvent.DESELECTED:canvas.removePopup(); break;public void actionPerformed(ActionEvent evt)_;static public void main(
12、String args)new advance ();(分数:30.00)_二级 JAVA 机试-199 答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序功能是分别比较两个字符串“A“和“a“是否相等及两个字符A和a是否相等,并输出比较结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basicpublic static void main (String args)_;c1= A;c2 = a;String str1= new String(“A“),str2 = new Stri
13、ng(“a“);if(_)System.out.println(c1+“ equals “+c2);elseSystem.out.println(c1+“ doesnt equal “+c2);if(_)System.out.println(str1+“ equals “+str2);elseSystem.out.println(str1+“ doesnt equal “+str2);(分数:30.00)_正确答案:(char c1,c2。c1=c2。str1.equals(str2)。)解析:二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是使用 GridBayLayout
14、 来组织窗口上的按钮。窗口中共有 7 个按钮,前 3 个按钮放置在第 1 排,第 4 个按钮独占第 2 排,第 6 个按钮位于第 3 排的右侧,而第 5 和第 7 个按钮共同处于第 3 排的左侧。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import javax.swing.*;import java.awt.*;import java.awt.event.*;public class simple extends Jframeprivate JPanel jpanel1=new JPanel();private GridLayout g1;private JButto
15、n JButton1 = new JButton(“第一个“);private JButton JButton2 = new JButton(“第二个“);private JButton JButton3 = new JButton(“第三个“);private JButton JButton4 = new JButton(“第四个“);private JButton JButton5 = new JButton(“第五个“);private JButton JButton6 = new JButton(“第六个“);private JButton JButton7 = new JButton
16、(“第七个“);public _()super(“simple“);setSize(300,150);GridBagLayout gbl= _;jpanell.setLayout(gbl);GridBagConstraints gbc = new GridBagConstraints();gbc.fill = GridBagConstraints.BOTH;gbc.gridwidth = 1;gbc.gridheight = 1;gbc.gridx = 0;gbc.gridy = 0;jpanell.add(JButtonl,gbc);gbc.gridx = 1;jpanell.add(JBu
17、tton2,gbc);gbc.gridx = 2;jpanell.add(JButton3,gbc);gbc.gridx = 0;gbc.gridy = 1;gbc.gridwidth = 3;jpanell.add(JButton4,gbc);gbc.gridy = 2;gbc.gridwidth = 1;jpanell.add(JButton5,gbc);gbc.gridx = 1;gbc.gridwidth = 2;gbc.gridheight = 2;jpanell.add(JButton6,gbc);gbc.gridx = 0;gbc.gridy = 3;gbc.gridwidth
18、= 1;gbc.gridheight = 1;jpanell.add(JButton7,gbc);this.setContentPane (jpanel1);public static void main (String args)simple fgl= new simple();fgl.show();fgl.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)System.exit(0););(分数:40.00)_正确答案:(simple。new GridBagLayout()。)解析:三
19、、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是用复选框来控制鼠标右键的弹出菜单是否弹出。窗口中有一个复选框“弹出菜单”,勾选该复选框后,鼠标置于窗口上,右击会弹出一个菜单,单击菜单中的命令后,后台会输出所单击的菜单项。如果取消勾选该复选框,右击就不能弹出菜单。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;class CanvasWithPopup extends Canvas_;CanvasWithPopup(PopupMenu popup)enableEvents(AW
20、TEvent.MOUSE EVENT MASK);this.popup = popup;void addPopup()add(popup);void removePopup()remove(popup);protected void processMouseEvent(MouseEvent evt)if (popup.getParent() ! = null evt.isPopupTrigger()popup.show(evt.getComponent(),evt.getX(),evt.getY();super.processMouseEvent(evt);public class advan
21、ce extends Frame implements ItemListener,ActionListenerCheckbox cb = new Checkbox(“弹出菜单“,false);CanvasWithPopup canvas;advance()super(“advance“);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)System.exit(0););add(cb,BorderLayout.NORTH);cb.addItemListener(this);PopupMen
22、u popup = new PopupMenu(“Button Control“);popup.add(“item1“);popup.add(“item2“);popup.addActionListener(this);canvas = new CanvasWithPopup(popup);add(canvas,BorderLayout.CENTER);setSize(100,200);show();public void itemStateChanged(ItemEvent evt)_case ItemEvent.SELECTED:canvas.addPopup(); break;case ItemEvent.DESELECTED:canvas.removePopup(); break;public void actionPerformed(ActionEvent evt)_;static public void main(String args)new advance ();(分数:30.00)_正确答案:(PopupMenu popup。switch(evt.getStateChange()。System.out.println(evt.getActio nCommand()+“is selected“)。)解析: