2015計算機(jī)二級《JAVA》模擬操作和應(yīng)用模擬題

字號:

二、基本操作題
    本題中定義了一個長度為20的整數(shù)數(shù)組,然后將1~20分別賦給數(shù)組元素,計算該數(shù)組中所有下標(biāo)為奇數(shù)的元素的和。
    public class javal{
    public static void main(String args[]){
    int sum;
    ;
    int arrayList[]=new int[20];
    for(int i=0;i<=19;i++)
    arrayList[i]=i+1;
    int pos=0;
    while(pos<20){
    if( )
    sum=sum+arrayList[pos];
    ;
    }
    System.out.println("sum="+sum);
    }
    }
    三、簡單應(yīng)用題
    本題的功能是通過按鈕來選擇窗口顯示的風(fēng)格。窗口
    中有三個按鈕:“Metal”、“Motif”和“Windows”,單擊任何一
    個按鈕,就能將窗口的風(fēng)格改變?yōu)榘粹o名稱所對應(yīng)的風(fēng)格。
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class PlafPanel extends JPanel implements ActionLis-
    tener
    {public ()
    {metaIButton=new JButton("Metal");
    motifButtOn=new J Button("Motif");
    windowsButton=new JButton("Windows");
    add(metalButton);
    add(motifButton);
    add(windowsButton);
    metalButton.addActionListener(this);
    motifButton.addActionListener(this);
    windowsButton.addActionListener(this);
    }
    Dublic void actionPerformed(ActionEvent evt)
    {Object source=evt.getSource();
    String plaf="":
    if(source= =metalButton)
    plaf="javax.swing.plaf.metal.MetalLookAnd-
    Feel";
    else if(source= =motifButton)
    plaf="com.sun.java.swing.plaf.motif.Moti-
    fLookAndFeel";
    else if(source= =windowsButton)
    Dlaf="com.sun.java.swing.plaf.windows.Win-
    dowsLookAndFeel";
    try
    {UIManager.setLookAndFeel( );
    SwingUtilities.updateComponentTreeUI(this);
    }
    catch(Exception e){)
    }
    private JButton metalButton;
    private JButton motifButton;
    private JButton windowsButton;
    }
    class PlafFrame extends JFrame
    {public PlafFrame()
    { setTitle("simple");
    setSize(300,200);
    addWindowListener(new WindowAdapter()
    {public void windowClosing(WindowEvent e)
    {System.exit(O);
    }
    });
    Container contentPane=getContentPane();
    contentPane.add(new PlafPanel());
    }
    }
    public class java2
    {public static void main(String[]args)
    f JFrame frame=new PlafFrame();
    frame.show();
    }
    四、綜合應(yīng)用題
    本程序的功能是獲取文本框中的文本。窗口中有兩個文本框“用戶名”和“密碼”,以及三個按鈕“登錄”、“其他用戶登錄,,和“關(guān)閉”,初始狀態(tài)“用戶名”文本框是只讀的,單擊“其他用戶登錄”按鈕后變成可寫的,“密碼”文本框使用的不是密碼文本框,在用戶鍵入的時候設(shè)置顯示為*號。輸入用戶名和密碼后,單擊“登錄”按鈕后,如果輸入的密碼為空,則彈出提示消息框,否則后臺將顯示輸入的用戶名和密碼。比如顯示為“admin用戶的密碼:password”(admi為輸入的用戶名,password為輸入密碼)。
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JOptionPane;
    public class java3
    {
    public static void main(String args[])
    {
    final Frame frmFrame=new Frame();
    Panel pnlPanel=new Panel();
    Label lblUsername=new Label("用戶名");
    Label lblPassword=new Label("密碼");
    final TextField txtUsername=new TextField("
    Student");
    final TextField txtPassword=new TextFidd("",
    8);
    txtUsername.setEditable(false);
    txtPassword.setChar(’*’);
    Button btnButtonl=new Button("登錄");
    ButtOn btnButton2=new Button("其他用戶登錄");
    Button btnButton3=new Button("關(guān)閉");
    btnButtonl.addActionListener(new ActionListen-er()
    {
    public void actionPerformed(ActionEvent e)
    {
    if((txtPassword.getText()).length()= =0)
    {
    JOptionPane.showMessageDialog(frmFrame,"密碼不能為空");
    return;
    }
    txtPassword.setColumns(16);
    System.out.println(txtUsername.getText()+"
    用戶的密碼:"
    +txtPassword.getPassword());
    }
    });
    btnButton2.addActionListener(new ActionListen-
    er()
    {
    public void actionPerformed(ActionEvent e)
    {
    txtUsername.setEnable(true);
    }
    });
    btnButton3.addActionListener(new ActionListen-
    er()
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    });
    pnlPanel.add(1blUsername);
    pnlPanel.add(txtUsername);
    pnlPanel.add(1blPassword);
    pnlPanel.add(txtPassword);
    pnlPanel.add(btnButtonl);
    pnlPanel.add(btnButton2);
    pnlPanel.add(btnButton3);
    frmFrame.add(pnlPanel);
    frmFrame.setTitle("advance");
    frmFrame.pack();
    frmFrame.show();
    }
    }