What will be the appearance of an applet with the following init() method?   public void init() {   add(new Button("hello"));  }  A、Nothing appears in the applet.B、A button will cover the whole area of the applet.C、A button will appear in the top left cor

题目

What will be the appearance of an applet with the following init() method?   public void init() {   add(new Button("hello"));  }  

  • A、Nothing appears in the applet.
  • B、A button will cover the whole area of the applet.
  • C、A button will appear in the top left corner of the applet.
  • D、A button will appear, centered in the top region of the applet.
  • E、A button will appear in the center of the applet.
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

下列程序使用FlowLayout布局管理器管理3个Button构件在Frame中的位置。请将程序补充完整。

注意:不改动程序结构,不得增行或删行。

import java.awt.*;

public class ex2

{

private Frame. frm;

private Button btn1;

private Button btn2;

private Button btn3;

public static void main(String[] args)

{

ex2 t=new ex2();

t.method();

}

public void method()

{

frm=new Frame("ex2")

______

Btn1=new Button("Button1");

btn2=new Button("Button2");

btn3=new Button("Button3");

frm.add(btn1);

frm.add(btn2);

______

frm.pack();

frm.setVisible(true);

}

}


正确答案:frm.setLayout(newFlowLayout()); frm.add(btn3);
frm.setLayout(newFlowLayout()); frm.add(btn3); 解析:本题考查对图形用户界面的掌握。第1空应该填入的语句的功能是设置布局管理器为FlowLayout,程序中的容器就是Frame的对象frm,设定frm的布局管理器,用newFlowLayout()作为参数调用frm.setLayout(newFlowLayout())实现了上述功能,因此,第1空填写frm.setLayout(newFlowLayout());。第2空应该填入的语句的功能是将btn3放入对象名为frm的Frame中,因此,第2空填写frm.add(btn3);。

第2题:

阅读下面代码:

import java.awt.*;

public class Exam11_1

{

private Frame. f;

private Button b1,b2,b3,b4;

public static void main(String args[]

{

Exam11_1 that = new Exam11 1 ();

that.go();

}

public void go()

{

______;

f.setLayout(new FlowLayout()) ;;

b1 = new Button ("Button 1");

b2 = new Button ("Button 2");

b3 = new Button ("Button 3");

b4 = new Button ("Button 4");

f.add (b1);

f.add (b2);

f.add (b3);

f.add (b4);

f.pack ();

f.setVisible (true);

}

}

请在程序中画线处填写正确的语句【 】,以便编译运行程序后得到正确的结果。


正确答案:f=new Frame("GUI example")
f=new Frame("GUI example") 解析:本题考查容器和布局管理器的基本知识,属于综合题。在本程序里,缺少建立Frame的语句,而且该Frame的名字为GUI example,而4个按钮的布局方式被压缩至最小,因此是 FlowLayou布局方式。

第3题:

public class X extends Frame{public static void main(String[] args){X x=new X();x.pack();x.setVisible(true);}public X(){setLayout(new GridLayout(2,2));Panel p1=new Panel(); add(p1);Button b1

A.all change height and width

B.Button One change height

C.Button Two change height and Button Three change width

D.Button Four change height and width


参考答案:D

第4题:

下列Applet在窗口中放置2个Button,标签分别为“东”和“西”,在窗口中的位置与它们的名字相同。选择正确的语句填入横线处。 import java.awt.*; import java.applet.*; public class ex16 extends Applet { Button e, w; public void init() { e = new Button("东"); w = new Button("西"); add("East", e); add("West", w); } }

A.setLayout(new BoxLayout());

B.setLayout(new FlowLayout());

C.setLayout(new BorderLayout());

D.setLayout(new GridLayout());


正确答案:C

第5题:

下列程序采用BorderLayout布局管理,选择正确的语句填入横线处,实现在North区域显示一个名字为“北方”的Button构件。 import java.awt.*; public class ex48 { public static void main(String[] args) { frame. frm = new Frame. ("北方"); frm.setLayout(new BorderLayout()); frm.setSize(200, 200); frm.setVisible(true); } }

A.add("Nouth", new Button("北方"));

B.frm.add("South", new Button("北方"));

C.frm.add("Nouth", new Button("北方"));

D.Frm.add("South", Button("北方"));


正确答案:C

第6题:

下列程序用GridLayout布局管理器将Frame分为1行3列,并放入Button构件,横线处应填入的语句是( )。 import java.awt.*; public class Test { public static void main (String[] args) { Frame. frm=new Frame. ("GridLayout"); ____________ frm.add (new Button("Button1")); frm.add (new Button("Button2")); frm.add (new Button("Button3")); frm.setSize (300,300); frm.setVisible (true); } }

A.frm.setLayout (GridLayout (1,3));

B.setLayout (new GridLayout(1,3));

C.frm.setLayout (new GridLayout(3,1));

D.frm.setLayout (new GridLayout(1,3));


正确答案:D
解析:GridLayout的构造方法参数第一个指出行数,第二个指出列数,由于容器是Frame的对象frm,因此需要用frm调用setLayout (newGridLayout (1,3))方法。

第7题:

下列程序在Frame中设定BorderLayout布局管理器,选择正确的语句填入程序的横线处。 import java.awt.*; public class ex43 extends Frame { public static void main(String[] args) { ex43 bj = new ex43("BorderLayout"); ______ obj.add("North", new Button("North")); obj.add("South", new Button("Sourth")); obj.add("East", new Button ("East")); obj.add("West", new Button ("West")); obj. add ("Center", new Button ( "Center" ) ); obj.pack(); obj. setVisible (true); } public ex43(String str) { super (str); } }

A.obj.setLayout(new BorderLayout());

B.setLayout(new Borderkayout());

C.setLayout(BorderLayout());

D.obj.setLayout(BorderLayout());


正确答案:A

第8题:

下面程序是一个计时器,从1000秒开始倒计时,直到为0结束。在界面上有两个按钮,一个可以暂停计时,另一个可以继续已经暂停的计时。请更正题中带下划线的部分。

注意:不改动程序的结构,不得增行或删行

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

public class Example3_4 extends Applet

{

public Color color = Color.red;

private int num= 1000;

public Counter theCounter;

private Button stop;

private Button start;

public void init()

{

stop = new Button("暂停");

start = new Button ("继续");

theCounter = new Counter(this);

stop.addActionListener(new Lst() implements ActionListener{

public void actionPerformed(ActionEvent e)

{

theCounter.sus();

}

});

start.addActionListener(new SuspenListener());

add(start);

add(stop);

theCounter.start();

}

public void paint(Graphics g)

{

g.setCotor(color);

g.drawString(String.valueOf(num),50,50);

}

public void setInt(int i)

{

num=i;

}

class SuspenListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

theCounter.conti ();

}

}

}

public class Counter extends Thread

{

Example3_4 example;

boolean isHold;

Counter (Example3_4 ex)

{

this.example = ex;

isHold = false;

}

public void sus()

{

isHold = true;

}

public synchronized void conti()

{

isHold = false;

notify();

}

public void run()

{

for (int i = 1000; i>0; i--)

{

if (i%2 == 1)

example.color = Color.red;

else

example.color = Color.blue;

example.setInt(i);

example.repaint();

try

{

sleep(1000);

synchronized {

while(isHold)

wait ( );

}

}

catch (InterruptedException ie)

{}

}

}

}

<HTML>

<HEAD>

<TITLE>Example3_4</TITLE>

</HEAD>

<BO


正确答案:①stop.addActionListener(new ActionListener(){ ②class Counter extends Thread ③synchronized(this)
①stop.addActionListener(new ActionListener(){ ②class Counter extends Thread ③synchronized(this) 解析:本程序使用线程类“Counter”来实现计时的功能,该线程的线程体每1000毫秒循环一次,并在每次循环的开始显示剩余的时间。计时器的暂停和继续通过线程的同步与共享来完成,每次循环结束时,判断标志暂停的变量“isHold”是否为真,如果为真则进入挂起状态。当用户按下“暂停”按钮,程序调用Counter类的sus()方法,将变量“isHold”设为真。当用户按下“继续”按钮,程序调用Counter类的conti()方法,将变量“isHold”设为假,并使用notify()取消线程的挂起状态。
本题第1个错误是考查对匿名类的使用。匿名类是Java语言中比较特殊的一种类,这种类不存在类名,而且只能在声明的时候被实例化一次。匿名类必须继承于一个父类或实现一个接口。第1条横线处,原程序打算声明一个新类 Lst实现ActionListener接口,并同时实例化一个新对象。但是只有匿名类才能在声明时创建一个新的类对象。因此此处应改用匿名类实现程序的目的。
第2个错误处考查Java源程序的结构。任何一个“java”文件中有且只有一个被声明为public的类,这个类的名字必须与“java”文件的文件名相同。因此在第2个错误位置处必须去掉 public声明。
synchronized用来定义一个同步块。
synchronized的定义格式是synchronized (expression)statement,expression是对象或类的名字,系统将为该对象设置唯一的锁;statement可以是一个方法定义,也可以是一个语句块,在同一时刻只能有一个线程执行这个块中的操作。因此第3个错误处必须指明同步的对象,本题中的同步对象是当前的Counter实例。
运行结果如下图所示。

第9题:

下面程序代码,让用户输入想显示的.gif文件名,之后将这个图像文件加载到内存并显示。请勿改动原有代码,在下画线处填人适当浯句,将程序补充完整。

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class test20_2 extends Applet implements ActionListener {

Label promptLbl=new Label(“请输入欲显示的图像文件名:”);

TextField inputTfd=new TextField20( );

Button getlmageBtn=new Button(“显示图像”);

Image mylmage;

public void init( ) {

add(promptLbl);

add(inputTfd);

add(getlmageBtn);

inputTfd.setText(””);

getlmageBtn.addActionListener(this);

}

public void paint(Graphics g) {

if(mylmage!=null)

g.______(mylmage,10,100,this);

}

public void actionPerformed(ActionEvent ae) {

if(ae.getSource( )==_______) {

String str=inputTfd.getText( ).trim( );

if(!(str.substring(Math.max(0,str.length( )-4)).equals(".gif")))

str=str.trim( )+".gif";

mylmage=getlmage(getDocumentBase( ),str);

repaint( );

}

}

}


正确答案:drawlmage getlmageBtn
drawlmage getlmageBtn

第10题:

以下程序中,当用户单击“移动”按钮以后,就可以使用方向键控制屏幕上句子的移动,单击“停止”按钮,则句子不再随着方向键移动。运行结果如下图所示

注意:请勿改动其他已有语句内容,仅在横线处填入适当语句。

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class Example2_8 extends Applet implements KeyListener

{

public void keyTyped(KeyEvent e) {}

public void keyReleased(KeyEvent e) {}

Button button;

Button stopButton;

Label out;

int x,y;

public void _______ ()

{

button = new Button("移动");

button.addActionListener(new AddMoveListener(this));

stopButton = new Button("停止移动");

stopButton.addActionListener(new RemoveListener(this));

stopButton.setEnabled(false);

out = new nabel("按下按钮以后我可以随方向键移动");

add(button);

add(stopButton);

add (out);

}

public void start()

{

super, start ();

}

public void keyPressed(KeyEvent e)

{

x=out.getBounds().x;

y=out.getBounds().y;

if(e.getKeyCode()==KeyEvent.VK_UP)

{

y=y-2;

if(y<=0) y=0;

out. setLocation (x, y);

}

else if(e.getKeyCode()==KeyEvent.VK_DOWN)

{

y=y+2;

if (y>=300) y=300;

out. setLocation (x, y);

}

else if(e.getKeyCode()==KeyEvent.VK_LEFT)

{

x=x-2;

if(x<=0) x=0;

out. setLocation (x, y);

}

else if(e.getKeyCode()==KeyEvent.VK_RiGHT)

{


正确答案:init addKeyListener
init addKeyListener 解析:本题考查知识点:小应用程序概念、Applet执行过程、JavaApplication和Applet。解题思路:Applet运行时,首先由浏览器调用init方法,该方法通知Applet已被加载,在这个方法中通常进行一些基本的初始化过程。Applet的基本方法还有start()、stop()、destroy()。类Example2_8实现了“KeyListener”监听器接口,就可以通过该监听器的方法监听键盘事件。需要填空的方法是初始化Applet程序,keyPressed()方法中专门处理方向键的事件。按下方向键以后,就会调用Label的setLocation()方法重新设置“out”所在的位置。当用户按下“移动”按钮以后,AddMoveListener为“移动按钮”添加了针对键盘的监听器。当用户按下“停止移动”按钮以后,RemoveListener从“移动”按钮中移出针对键盘事件的监听器。
本题中start方法已经实现,另外两个方法分别用于Applet的停止和卸载,所以第一个空只能填“init”,用来为Applet实现初始化。
由于本题是使用键盘来控制Label对象的移动,所以必须添加针对键盘的监听器,这样才能对键盘事’件做出反应,第二个空就是给“button”添加键盘事件监听器。

更多相关问题