问题 问答题

以下程序中,使用适当的布局管理器,在Frame框的“North”位置添加一句提示信息,在“South”位置添加一个单行文本框,在这个文本框中输入的内容将会显示在“Center”位置。运行结果如下图所示。


注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。
import java.awt.*;
import java.awt.event.*;
public class Example2_6 extends Frame

public static void main(String [] argv)

Example2_6 frame = new Example2_6("Example2_6");
frame, init ();
frame.setSize(300, 300);
frame, show ();
public Example2_6(String name)

super (name);
addWindowListener (new WindowAdapter ()
public void windowClosing(WindowEvent e)
__________;

);

public void init()

setLayout (new ___________);
Label labelTitle = new Label("在文本框中输入字符串,可以在Frame 中间显示");
Label showTextLabel = new Label();
TextField textField = new TextField("请在这里输入字符串");
textField.addActionListener (new AddStringListener(showTextLabel, textField) );
add("North", labelTitte);
add("Center", showTextLabel);
add("South", textField);


class AddStringListener implements ActionListener

Label label;
TextField textField;
public AddStringListener(Label label, TextField textField)

this. label = label;
this.textField = textField;

public void actionPerformed(ActionEvent e)

label, setText (textField. getText ());

答案

参考答案:System.exit(0)
BorderLayout()或者带参数的BorderLayout(int,int)也正确

解析: 本题考查知识点:AWT库的使用、布局管理器的使用。
解题思路:程序使用“labelTitle”来显示提示信息,使用“textField”来获取用户的输入,最后将用户输入的结果通过“showTextLabel”显示出来。
Frame的关闭按钮的处理方式与JFrame的方式不相同,Frame必须手动实现退出按钮的事件处理机制,否则单击关闭按钮程序不会做出响应。
本题的第一个空就是对退出按钮的事件响应内容。
BorderLayout布局管理器将容器分为五个区域:North、South、East、West和Center。可以指定构件放在那个区域,但是每个区域只能放置一个构件。第二个空即是为Frame设置BorderLayout类型的布局管理器。

填空题
单项选择题