import java.awt.*; import java.awt.event.*; public class Test { public static void main (String[] args) { Win win = new Win("Test Window", "Test Data"); } } /*******************/ /* クラスWinの定義 */ /*******************/ class Win extends Frame { /******************/ /* コンストラクタ */ /******************/ Win (String name, String data) { // Frameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // レイアウトの変更(行,列,水平ギャップ,垂直ギャップ) setLayout(new GridLayout(2, 1, 5, 10)); Font f = new Font("MS 明朝", Font.PLAIN, 20); // 上のパネル // パネルの追加 Panel pn1 = new Panel(); pn1.setLayout(new GridLayout(2, 1, 5, 10)); add(pn1); // テキストフィールドの追加 TextField tx1 = new TextField("Text Field 1"); tx1.setFont(f); pn1.add(tx1); TextField tx2 = new TextField(); tx2.setFont(f); tx2.setEchoChar('*'); pn1.add(tx2); // 下のパネル // パネルの追加 Panel pn2 = new Panel(); add(pn2); // テキストエリアの追加 TextArea ta = new TextArea("Text Area", 4, 25); ta.setFont(f); pn2.add(ta); // テキストエリア内の操作 String str = ta.getText(); ta.append(str); ta.insert(str + "\n", 0); ta.select(5, 9); str = ta.getSelectedText(); ta.insert(str, 19); ta.replaceRange("XX", 23, 27); // Windowの大きさ setSize(350, 300); // ウィンドウを表示 setVisible(true); // イベントアダプタ addWindowListener(new WinEnd()); } /******************************/ /* 上,左,下,右の余白の設定 */ /******************************/ public Insets getInsets() { return new Insets(35, 10, 10, 10); } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } }