ActionTest.java



package talktool.window;    // package文は必ず先頭に置く

import ExitableFrame;   // 名前なしのパッケージに所属
import java.awt.*;
import java.awt.event.*;

/** アプリケーションのアクション処理を実験するためのクラス */

public class ActionTest extends LayoutTest {

  /** ControlPanelのアクションを処理するアダプタ */

     protected ControlAdapter controlAdapter;

  /** コンストラクタ */

     protected ActionTest() {

          this( new ControlPanel(),
                new MessagePanel(
                "テキストを入力して送信ボタンを押してください" ),
                new TextArea( "", 32, 40 ),
                new TextArea( "", 32, 40 ) );
     }

     protected ActionTest( ControlPanel top, MessagePanel bottom,
                 TextArea left, TextArea right ) {

          super( top, bottom, left, right );
          controlAdapter = new ControlAdapter();
          top.sendButton.addActionListener( controlAdapter );
          top.receiveButton.addActionListener( controlAdapter );
     }

  /** アクション処理の実験用 */

     private String tmp="";

  /** 送信ボタンの処理を記述する public なメソッド */

     public void sendButtonClicked() {

          tmp = getLeftText();
          setLeftText("");
          setMessage(
            "送信処理を実行しました。受信ボタンを押してください" );
     }

  /** 受信ボタンの処理を記述する public なメソッド */

     public void receiveButtonClicked() {

          setRightText( tmp );
          setMessage( "受信処理を実行しました" );
     }

  /** メッセージを表示する public なメソッド */

     public void setMessage( String message ) {

          messagePanel.setMessage( message );
     }

  /** leftTextAreaの内容を得る public なメソッド */

     public String getLeftText() {

          return leftTextArea.getText();
     }

  /** leftTextAreaの内容を設定する public なメソッド */

     public void setLeftText( String text ) {

          leftTextArea.setText( text );
     }

  /** rightTextAreaの内容を得る public なメソッド */

     public String getRightText() {

          return rightTextArea.getText();
     }

  /** rightTextAreaの内容を設定する public なメソッド */

     public void setRightText( String text ) {

          rightTextArea.setText( text );
     }

  /** 最初に呼び出されるメソッド(ここでは確認用) */

     public static void main( String argv[] ) {

          ExitableFrame frame = new ExitableFrame( "Test" );

          ActionTest actionTest = new ActionTest();
          frame.add( actionTest, "Center" );
          frame.pack();
          frame.setVisible( true );
     }

  /** 内部のアクション処理を行う innerクラス */

     class ControlAdapter implements ActionListener {

          public void actionPerformed( ActionEvent evt ) {

               if( evt.getSource()
                   == (Object)controlPanel.sendButton )
                   sendButtonClicked();
               else if( evt.getSource()
                   == (Object)controlPanel.receiveButton )
                   receiveButtonClicked();
          }
     }
}