/*我用了"网格式布局",因每个单元格高度都一样,所以有很大的空挡(即右图圈出的部分)*/
import java.awt.*;
import java.awt.event.*;
class Student extends Frame implements TextListener,ActionListener
{
Panel p=new Panel();
Panel pName=new Panel();
Panel pHobby=new Panel();
Panel pInfo=new Panel();
Label lName=new Label("姓名:");
Label lHobby=new Label("爱好:");
Label lInfo=new Label("学生信息:");
TextField tName=new TextField(15);
List l=new List(4,true);
TextField tInfo=new TextField(15);
Button b=new Button("确定");
Student(String title)
{
super(title);
l.add("篮球");
l.add("排球");
l.add("乒乓球");
l.add("足球");
pName.add(lName);
pName.add(tName);
pHobby.add(lHobby);
pHobby.add(l);
pInfo.add(lInfo);
pInfo.add(tInfo);
pInfo.add(b);
p.setLayout(new GridLayout(3,1,5,5));
p.add(pName);
p.add(pHobby);
p.add(pInfo);
this.add(p);
tName.addTextListener(this);
b.addActionListener(this);
this.pack();
this.setVisible(true);
}
String s;
String sName;
String sHobby=""; //sHobby不赋初值下面输出"学生信息"时会多出一个null
public void textValueChanged(TextEvent e)
{
sName=tName.getText();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
String[] sHobby=l.getSelectedItems();
for(int i=0;i<sHobby.length;i++)
{
this.sHobby+=sHobby[i]+" ";
}
}
s=sName+" "+this.sHobby;
tInfo.setText(s);
}
public static void main(String[] args)
{
new Student("学生信息调查");
}
}