コンポーネントの再配置

/****************************/
/* コンポーネントの再配置   */
/*      coded by Y.Suganuma */
/****************************/
import java.awt.*;
import java.awt.event.*;

public class Test
{
	public static void main(String[] args)
	{
		Win win = new Win("Test Window");
	}
}

class Win extends Frame implements TextListener
{
	int n;
	TextField tx;
	ScrollPane sp;

	/******************/
	/* コンストラクタ */
	/******************/
	Win (String name)
	{
					// JFrameクラスのコンストラクタ
		super(name);
					// ウィンドウサイズ
		setSize(450,200);
					// レイアウト,背景色,フォント
		setLayout(new BorderLayout(5, 10));
		setBackground(new Color(225, 255, 225));
		Font f = new Font("TimesRoman", Font.BOLD, 20);
		setFont(f);
					// 上のパネル
		Panel pn = new Panel();
		add(pn, BorderLayout.NORTH);
		pn.add(new Label("データの数"));
		tx = new TextField(5);
		tx.addTextListener(this);
		pn.add(tx);
					// 中央のパネル
		sp = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
		add(sp, BorderLayout.CENTER);
					// ウィンドウを表示
		setVisible(true);
					// イベントアダプタ
		addWindowListener(new WinEnd());
	}

	/************************************/
	/* パラメータが変更されたときの処理 */
	/************************************/
	public void textValueChanged(TextEvent e)
	{
		int i1;

		try {
			remove(sp);
			sp = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
			add(sp, BorderLayout.CENTER);
			Panel pn = new Panel();
			sp.add(pn);
			n = Integer.parseInt(tx.getText());
			if (n <= 0) {
				pn.setBackground(new Color(255, 255, 225));
				TextArea ta = new TextArea(3,35);
				ta.setForeground(Color.red);
				ta.setText(" 0 より大きい数値を入力してください");
				pn.add(ta);
			}
			else {
				pn.setBackground(new Color(204, 255, 255));
				pn.setLayout(new GridLayout(n, 1, 10, 10));
				Panel pn1[] = new Panel [n];
				TextField tx1[] = new TextField [n];
				for (i1 = 0; i1 < n; i1++) {
					pn1[i1] = new Panel();
					pn.add(pn1[i1]);
					pn1[i1].add(new Label((i1+1) + " 番目のデータ"));
					tx1[i1] = new TextField(5);
					pn1[i1].add(tx1[i1]);
				}
			}
			validate();
		}
		catch (NumberFormatException em) {}
	}
	
	/************/
	/* 終了処理 */
	/************/
	class WinEnd extends WindowAdapter
	{
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
}