2015年計算機二級考試JAVA應用練習題及答案

字號:

三、簡單應用題(共24分)
    本題中,在窗口右側添加了一個菜單,右側為一個文本域,菜單有“File”和“Help”,“File”菜單中有菜單項“New”、“Open”、“Save”、“Save as”和“Exit”,其中“Open”的快捷鍵為,“Save”的快捷鍵為,而“Help”菜單以及其中的菜單項“Index”和“About”設定了第一個字母為其快捷字母,通過鼠標單擊任一個菜單項或通過快捷鍵以及快捷字母,都能在后臺輸入所選擇的菜單項。
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax swing.event.*;
    public class java2 extends JFrame
    {
    private J Menuhem savehem;
    private JMenuhem saveAsltem
    private JPopupMenu popup;
    private JTextArea textArea;
    public java2()
    {setTitle("java2");
    setSize(400,300);
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    textArea=new JTextArea(0,0);
    Container contentPane=getContentPane();
    contentPane.add(new JSerollPane(textArea).
    "Center");
    JMenuBar menuBar=new JMenuBar();
    menuBar.setLayout(new BoxLayout(menuBar.
    BoxLayout.Y_AXIS));
    getContentPane().add(menuBar, BorderLayout.
    WEST):
    HorizontalMenu fileMenu=new HorizontalMenu
    ("File");
    fileMenu.addMenuListener(this);
    JMenuhem openhem = new JMenultem
    ("Open");
    openItem.setAceelerator(KeyStroke.getKeyStroke
    (KeyEvent.VK_O,InputEvent.CTRL_MASK));
    saveltem=new JMenuhem("Save");
    savehem.setAccelerator(KeyStroke.getKeyStroke
    (KeyEvent.VK_S,InputEvent.CTRL_MASK));
    saveAshem=new JMenuhem("Save As");
    menuBar.add(makeMenu(fileMenu,
    new Object[]{
    "New",Openhem,null,savehem,saveAshem.
    null."Exit"
    },this));
    HorizontalMenu helpMenu=new Horizontal-
    Menu("Help");
    helpMenu. (’H’);
    menuBar.add(Box.createVerticalGlue());
    menuBar.add(makeMenu(helpMenu,
    new Object[]
    {new JMenuhem("Index",’I’),
    new JMenuhem("About",’A’)
    },this));
    }
    public void actionPerformed(ActionEvent evt){
    String arg=evt.getActionCommand();
    System.out.println(arg);
    if(arg.equals("Exit"))
    System.exit(0);
    }
    pubile void menuSelected(MenuEvent evt){
    }
    public void menuDeselected(MenuEvent evt){
    }
    public void menuCanceled(MenuEvent evt){
    }
    public HorizontalMenu makeMenu(Object parent,
    Object[]items,Object target)
    {
    HorizontalMenu m=null:
    if(parent instanceoI HorizontalMenu)
    m=(HorizontalMenu)parent;
    else if(parent instaneeof String)
    m=new HorizontalMenu((String)parrent);
    else
    return null;
    m.setMinimumSize(m.getPreferredSize());
    for(int i=0;i  if(items[i]= =null)
    m.addSeparator();
    else
    m.add(makeMenultem(items[i],target));
    }
    return m;
    }
    public static JMenuhem makeMenuItem(Object i-
    tem,Object target)
    {
    JMenuItem r=null:
    if(item instanceof String)
    r=new JMenultem((String)item);
    else if(item instanceof JMenuhem)
    r=(JMenultem)item;
    else return null;
    if(target instanceof ActionListener)
    r.addActionListener((ActionListener)target);
    return r;
    }
    class HorizontalMenu extends J Menu{
    HorizontalMenu(String label){
    super(label);
    JPopupMenu pm=getPopupMenu();
    pm.setLayout(new BoxLayout(pm,BoxLayout.X
    _AXIS));
    setMinimumSize(getPreferredSize());
    }
    }
    public static void main(String[] args){
    Frame f=newjava2();
    f.show();
    }
    }
    四、綜合應用題(共18分)
    本題中,主窗口中有兩個下拉菜單,一個控制繪制圖形的顏色,另一個控制繪制的圖形,在畫板中單擊鼠標,則以單擊的位置為左上角、以選定的顏色繪制選定的圖形。
    import java.awt.*;
    import java.awt.event.*;
    class java3 extends Frame {
    String[]figureNames={"圓形","橢圓形","正
    方形","長方形");
    String[]colorNames={"紅色","綠色","藍
    色","黃色"};
    Color[]colorValues={Color.red,Color.green,
    Color.blue,Color.yellow);
    Choice chFigure=new Choice();
    Choice chColor=new Choice();
    int curX,curY;
    java3(){
    super("java3");
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(O);
    }
    });
    Panel P=new Panel(new GridLayout(1,O));
    for(int i=0;i  ehFigure.addhem(figureNames[i]);
    }
    for(int i=0;i  chColor.addhem(eolorNames[i]);
    }
    P.add(chColor);
    P.add(chFigure);
    add(p,BorderLayout.NORTH);
    addMouseListener(this);
    setSize(300,300);
    show();
    }
    public void update(Graphics g){
    g.getColor(colorValues[chColor,getSelectedIndex
    ()]);
    switch(chFigure.getSelectedlndex()){
    case 0;
    g.fillOval(curX,curY,30,30);
    break;
    case l:
    g.fillOval(curX,curY,30,50);
    break;
    case 2:
    g.fillRect(curX,curY,30,30);
    break;
    case 3:
    g.fillRect(curX,curY,30,50);
    break;
    }
    }
    class MouseEventListener implements MouseAda-
    pter{
    public void mousePressed(MouseEvent evt){
    curX=evt.getX();
    curY=evt.getY();
    repaint();
    }
    }
    static public void main(String[]args){
    new java3();
    }
    )
    三、簡單應用題
    第1處:implements ActionListener,MenuListener
    第2處:setMnemonic
    【解析】第1處是實現(xiàn)了ActionListener接口,MenuListener接口;第2處是設置Help的快捷鍵為。
    四、綜合應用題
    第1處:addMouseListener(new MouseEventListener())
    第2處:g.setcolor(colorValues[chcolor.getselectedIn-
    dex()])
    第3處:class MouseEventListener extends MouseAda-
    pter
    【解析】第1處是注冊鼠標監(jiān)聽器,主要是單擊動作;第2處是設置Graphics類對象g的顏色通過將從chColor中選中的字符串轉換成colorValues類型來實現(xiàn);第3處定義一個MouseEventListener類來繼承MouseAdapter鼠標事件適配器。