|
Java 编 程 练 习 题 库 (五)第 一 题 (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) 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(); } } 答 案: 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) 删 掉 将 会 如 何 ?
【8】 如 果 将 书 中 例10.5 中 的os.flush() 语 句 删 除 将 会 怎 样 ?
|