GUIプログラミング-イベントハンドリング



       卒業設計をしたいです。技術コースを選んだので、大きなテーマを選びました。JAVAに基づく汎用プロセス分析ソフトの分析と設計です。
      先生はたぶんJAVAの基礎があると思いますので、要求が高いです。JGraphamnerのコードを勉強して、XMLを利用してノード設計を行います。図论、特にDijktraアルゴリズムなどを勉强してください。先生がDemoを返したので、どう思いますか?本格的に着手して、やっと自分の知識の蓄積の少なさ、の浅さを知っています。二年生のjavaとData Structureを先生に返しました。今は一から勉強するのと同じです。クラスメートのサインを覚えています。一人が一番辛い時も彼の一番の収穫です。だから必ず堅持して、堅持して、放棄しないでください。
       この時間はSwingで悩んでいます。勉強したいことがたくさんあります。しかも、以前勉強したのが浅すぎて、ほとんど接触したことがないので、勉強するのが大変です。特にイベントハンドリング機構。最初のbutton Action Listenerはよかったですが、イベント依頼の仕組み。大雑把に内部の概念を見て、匿名の内部類を深くします。それらのGUIを学びます。 APIは、事件処理のメカニズムを少しずつ理解しながら。
       簡単な画像デザイン:
       1.Continerを建立して、一般的には(extensを受け継ぐ)JFrameに拡張されて、他のコンポーネントを盛り付けるために使われます。
       2.パンを建てるときは、一般的にはJPANELに広げて、他のセット、JButtonとか、JMenuとか、JLabelとか、絵を描いたりします。
       3.パン上のコンポーネントを確立し、モニターを登録する。一般的には、内部クラスまたは匿名内部クラス構成モニターを利用して、JButtonのアクション'Listenerなどの対応するモニターに拡張される。登録モニター用のObject.addxListenerメソッドです。
Core Java 2上の例:
/**

   @version 1.32 2004-05-04

   @author Cay Horstmann

*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
public class ButtonTest

{  

   public static void main(String[] args)

   {  

      ButtonFrame frame = new ButtonFrame();

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.setVisible(true);

   }

}

/**
   A frame with a button panel
*/

class ButtonFrame extends JFrame{
   public ButtonFrame()
   {
      setTitle("ButtonTest");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
      // add panel to frame
      ButtonPanel panel = new ButtonPanel();
      add(panel);
   }
   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;  
}
/**
   A panel with three buttons.
*/
class ButtonPanel extends JPanel{  
   public ButtonPanel()
   {        // create buttons
      JButton yellowButton = new JButton("Yellow");
      JButton blueButton = new JButton("Blue");
      JButton redButton = new JButton("Red");
      // add buttons to panel
      add(yellowButton);
      add(blueButton);
      add(redButton);
      // create button actions
      ColorAction yellowAction = new ColorAction(Color.YELLOW);
      ColorAction blueAction = new ColorAction(Color.BLUE);
      ColorAction redAction = new ColorAction(Color.RED);
      // associate actions with buttons
      yellowButton.addActionListener(yellowAction);
      blueButton.addActionListener(blueAction);
      redButton.addActionListener(redAction);
   }
   /**
    An action listener that sets the panel's background color. 
   */
   private class ColorAction implements ActionListener   {  
      public ColorAction(Color c)  {  
         backgroundColor = c;
      }
      public void actionPerformed(ActionEvent event) {  
         setBackground(backgroundColor);
      }
      private Color backgroundColor;
   }
}
 処理プロジェクト:
1.ラベル文字列でボタンを作成する
2.パネルにボタンを追加する
3.適当な色でモニターを色に当てる
4.動作モニタを追加
感じコードを繰り返し、改善し、補助方法を追加します。
void makeButton(Sring name,  final Color backgroudColor){
         JButton button = new JButton(String name);
         add(button);
         button.addActionListener( new
               Actionlistener(){
                      Public void actionPerformed(ActionEvent event){
                         setBackground(backgroundColor);
                         }
                     });
    }
 匿名の内部クラスを利用してコードを簡略化する。
 
最後に、いくつかの疑問:
      MVCのメカニズムに接触するのはまだ多くないので、このような委託代理機構は大型システムの中の構造にあります。Nの多部材を持つパンにおいて、各コンポーネントがこのような傍受処理方法を利用すれば、パン類はどれぐらいの大きさがありますか?他にも処理方法があると思いますが、内部カテゴリを利用しないと、コンポーネントはどうやって他のコンポーネントと通信しますか?例えば、Ecipseがオフになったら、あまりにも出てくるDialogの中の確定をクリックしてSystem.exit(0)を出発します。