// JAVA GUIプログラムのサンプル // 日本語はUTF-8で記述すること import java.awt.*; import java.awt.event.*; public class Test extends Frame // Frameクラスから継承 implements ActionListener, WindowListener { // Event監視を行う public static void main(String[] args) { Test test = new Test("サンプル"); test.setSize(320, 360); test.setVisible(true); // これが無いと表示しない } TextArea tarea = new TextArea("Hello\nWorld\n"); public Test(String title) { super(title); Label label = new Label("テストプログラム"); add(label, "North"); // 上に配置 // TextArea tarea = new TextArea("Hello\nWorld\n"); add(tarea, "Center"); // 中央に配置 Panel vbox = new Panel(); // ボタンを2個縦に並べる vbox.setLayout( new BorderLayout() ); add(vbox, "South"); // 下に配置 Button button1 = new Button("btn01"); vbox.add(button1, "North"); // vboxの上に配置 button1.addActionListener(this);// 押下を検出 Button button2 = new Button("close"); vbox.add(button2, "South"); // vboxの下に配置 button2.addActionListener(this);// 押下を検出 addWindowListener(this); //ウィンドウ関係のイベント検出 } // ActionListner関係のイベント public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); // イベント発生元が if( obj instanceof Button ) { // ボタンであれば Button btn = (Button)obj; String bname = btn.getLabel(); // 名前を取得 if(bname == "btn01"){ // tareaに追加 tarea.append( "Button Label is " + bname + "\n" ); } else { // 終了 System.exit(0); } } // System.exit(0); } // WindowListener関係のイベント //  使用しないイベントも記述しないとダメ public void windowClosing(WindowEvent e) { System.exit(0); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} }