在程序中,使用适当的布局管理器,在Frame框的North位置添加一句提示信息,在South位置添加一个单行文本框,在这个文本框中输入的内容将会显示在Center位置。运行结果如下图所示。
注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。
import java.awt.*;
import java.awt.event.*;
public class simple extends Frame
public static void main(String[] args)
simple frame=new simple("simple");
frame.init();
frame.setSize(300,300);
frame.show();
public simple(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", labelTitle);
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()
解析:[讲解] 本题考查知识点:AWT库的使用,布局管理器的使用。解题思路:程序使用label Title来显示提示信息,使用text Field来获取用户的输入,最后将用户输入的结果通过showTextLabel显示出来。Frame的关闭按钮的处理方式与JFrame的方式不同,Frame必须手动实现退出按钮的事件处理机制,否则单击关闭按钮程序不会做出响应。本题的第1空就是对退出按钮事件响应的内容。BorderLayout布局管理器将容器分为五个区域:North、South、East、West和 Center。可以指定构件放在哪个区域,但是每个区域只能放置一个构件。第2个空即是为Frame设置BorderLayout类型的布局管理器。