Java 编 程 练 习 题 库 (五)


第 一 题
第 二 题
第 三 题
第 四 题
第 五 题
第 六 题
第 七 题
第 八 题

       【1】 要 在 一 个 拥 有BorderLayout 布 局 管 理 器 的 容 器 中 利 用 布 局 管 理 器 加 入 一 个 文 本 编 辑 框(TextArea 的 实 例)editspace, 下 面 适 宜 的 语 句 是 什 么 ?

(A)editspace=new TextArea(30,50);
(B)editspace=new TextArea(30,50)
       editspace.setEditable(true);
(C)add(TextArea("editspace"));
(D)TextArea editspace=new TextArea(30,50);
       add("Center",editspace); 
答 案:( D)

       【2】 要 让 消 息 继 续 向 上 传 递, 应 采 取 哪 条 语 句 ?

(A)return true;
(B)return false; 
答 案(B)

       【3】 写 出 在action(Event evt, Object arg) 用 于 辨 别"evt" 是 否 是 发 给TextArea 实 例editspace 的 消 息 的if 语 句:

if (____________________) {...}
答 案(e.target==editspace)

       【4】 设 置 一 个Frame 实 例 的GridLayout 布 局 管 理 器 应 选 语 句____;

      在 一 个 容 器 中 加 上 一 个 组 件 应 选 语 句____;
      在 一 个Menu 实 例File 中 加 上 一 个 菜 单 项open 应 选 语 句____;
      将 一 个TextArea 实 例editspace 设 成 可 编 辑 的 应 选 语 句____;
(A)add(......);
(B)File.add(open);
(C)editspace.setEditable(false);
(D)setLayout(new GridLayout(2,2));
(E)editspace.setEditable(true);
(F)setLayout(GridLayout());
答 案(DABE)

       【5】 有 如 下 档 案 资 料:

     Lili, Female, Birthday: 1980/8/30
     Wang, Male, Birthday: 1977/5/9
     Lee, Male, Birthday: 1979/2/26

       请 编 写 一 个 图 形 用 户 界 面 的 资 料 查 阅 程 序。 要 求 允 许 用 户 通 过 选 择 人 名 来 选 资 料, 还 允 许 用 户 翻 看 第 一 人、 最 后 一 人、 前 一 人、 后 一 人 的 资 料。

答 案:

import java.awt.*;

 class CardBox extends Panel{

      CardBox(){
         setLayout(new CardLayout());
         setBackground(Color.blue);
         setForeground(Color.white);
         add("Lili",new Label("Lili, Female, Birthday: 1980/8/30",Label.CENTER));
         add("Wang",new Label("Wang, Male, Birthday: 1977/5/9",Label.CENTER));
         add("Lee", new Label("Lee, Male, Birthday: 1979/2/26",Label.CENTER));
      }

      public Dimension preferredSize(){
         return (new Dimension(100,200));
      }

 }

 public class CardPanel extends Frame{

     CardBox cards;

     CardPanel(){

         setLayout(new BorderLayout());

         add("Center",cards=new CardBox());

         Panel p=new Panel();
         p.setLayout(new FlowLayout());
         p.setBackground(Color.gray);
         add("North",p);
         p.add(new Button("First"));
         p.add(new Button("Next"));
          Choice c=new Choice();
          c.addItem("Lili");
          c.addItem("Wang");
          c.addItem("Lee");
         p.add(c);
         p.add(new Button("Previous"));
         p.add(new Button("Last"));
         p.add(new Button("Close"));

     }

     public boolean action(Event e, Object arg){

         if ("Close".equals(arg)|e.id==Event.WINDOW_DESTROY) System.exit(0);

         if (e.target instanceof Choice){
             ((CardLayout)cards.getLayout()).show(cards,(String)arg);
         }else{
             if ("First".equals(arg)){
                 ((CardLayout)cards.getLayout()).first(cards);
             }else if ("Next".equals(arg)){
                 ((CardLayout)cards.getLayout()).next(cards);
             }else if ("Previous".equals(arg)){
                 ((CardLayout)cards.getLayout()).previous(cards);
             }else if ("Last".equals(arg)){
                 ((CardLayout)cards.getLayout()).last(cards);
             }else{
                 ((CardLayout)cards.getLayout()).show(cards,(String)arg);
             }
         }
         return true;
     }

     public static void main(String args[]){

        CardPanel mypanel=new CardPanel();
        mypanel.setTitle("Personal Information Cards");
        mypanel.resize(400,200);
        mypanel.show();
     }
 }


       【6】 实 现 一 个 框 架, 框 架 内 有 一 张" 画 布" 和 一 个"Exit" 按 钮。 点 击 画 布 内 任 意 一 点, 都 将 以 该 点 为 圆 心 画 出 一 个 圆 圈( 半 径 不 限); 点 击"Exit" 按 钮 将 结 束 应 用 程 序。

答 案:

import java.awt.*;
 public class Test extends Frame{
     MyCanvas canvas;
     MyPanel panel;
     boolean in=false;
     public Test(){
         in=false;
         resize(100,150);
         setLayout(new BorderLayout());
         canvas=new MyCanvas();
         panel=new MyPanel();
         add("Center",canvas);
         add("South",panel);
     }

     public boolean handleEvent(Event evt){
         if ((evt.target==panel.btnexit)||(evt.id==Event.WINDOW_DESTROY)){
             if (in) dispose();
             System.exit(0);
         }
         return super.handleEvent(evt);
     }

     public static void main(String args[]){
         Test test=new Test();
         test.show();
         test.in=true;
     }
 }

 class MyCanvas extends Canvas{
     final int r=4;
     Graphics gs;
     public MyCanvas(){
         super();
         resize(100,100);
     }
     public boolean mouseDown(Event evt, int x, int y){
         gs=getGraphics();
         gs.drawOval(x-r,y-r,2*r,2*r);
         return false;
     }
 }

 class MyPanel extends Panel{
     public Button btnexit;
     public MyPanel(){
         btnexit=new Button("Exit");
         add(btnexit);
     }
 }


       【7】 如 果 将 书 中 例10.4 的"public void update(Graphics g)" 方 法 的 最 后 一 条 语 句

    g.drawImage(offImage,0,0,this)
 删 掉 将 会 如 何 ?
  1. 由 于 有 上 一 条 语 句--"offGraphics.drawImage(imges[frame%10],0,0,this);", 所 以 仍 能 显 示 动 画
  2. 程 序 将 异 常 退 出
  3. Applet 将 不 能 显 示 所 要 的 任 何 一 张 图 象。
  4. Applet 只 能 显 示 所 有 动 画 图 象 的 第 一 张。
参 考 答 案(C)

       【8】 如 果 将 书 中 例10.5 中 的os.flush() 语 句 删 除 将 会 怎 样 ?

  1. 没 有 什 么 变 化
  2. 由 于 缺 少 了os.flush() 方 法, 储 存 在os 的 缓 冲 区 中 的 通 话 将 不 能 被 发 送 出 去, 以 致 导 致 双 方 应 用 程 序 堵 塞。
参 考 答 案(B)


返回