//gui.java -- GUI sample, @CopyLeft by tsaiwn@cs.nctu.edu.tw
//package ...;  // 指定我這 class 的命名空間 namespace, 阿就是要放哪個目錄啦
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
  // 注意要寫 public class 不然  Applet 有問題
public class gui extends Applet implements ActionListener {   // 我就是 Applet
   Panel ppp; Button bbb; TextArea tt; TextField tx;
   public static void main(String gg[ ]) {
       Frame f = new Frame("Windows 2011");
       gui me = new gui( );    // 模仿 Applet ..
       me.init( );  me.start( );  // ..
       f.add(me, "Center");   // 把 Applet (me) 放入窗 f 內
       f.setSize(580, 520);   // Frame 是 Window, 用 BorderLayout
       f.setVisible(true);   // f.show( );
   } // main(
   public gui( ) {  } // Constructor; 注意要寫 public 不然  Applet 有問題
   public void init( ) {
       ppp = new Panel( );  tx=new TextField("你可以在這打字", 38);
       bbb = new Button("  empty  ");   ppp.add(bbb);
       bbb.setSize(168, 120);  bbb.setFont(new Font("標楷體",1,36));
       setLayout(new BorderLayout( ) );  // change Layout Manager 擺設經理
         // 因為 Applet 是 Panel, 其 default Layout Manager 是 FlowLayout
       ppp.setBackground(Color.pink);
       tt = new TextArea("Hahaha\n", 5,20,TextArea.SCROLLBARS_BOTH);
       add(ppp, "North"); add(tt, "Center"); 
       tt.setFont(new Font("Ariel",3,32));
       Panel pn = new Panel( ); Panel ptt=new Panel( );
       Panel pOut = new Panel( ); pOut.setLayout(new BorderLayout( ));
       pOut.add(pn, "South");  // pn 塞在  pOut 這 Panel 內 :-) 木板上再放木板!
       pOut.add(ptt,"North");  // pOut 內塞兩個 Panel : pn, ptt
       ptt.add(new Button("請在右邊打字: "));  ptt.add(tx); 
       pn.setBackground(Color.yellow);  tx.setFont(new Font("標楷體",1,16));
       bt = new Button[98];  // 只會生出 98 個參考 (阿其實就是 C++ 的指標啦)
       for(int i=1; i< bMsg.length; ++i) {
           bt[i] = new Button(bMsg[i]);   // 真正生出六個 Button (注意 0 故意不用)
           pn.add(bt[i]); bt[i].setFont(new Font("細明體",0,16));
           bt[i].addActionListener(this);    // 請求監聽 "動作事件"(ActionEvent)
       } // for int i
       add(pOut, "South");   // 把 pOut 塞入我這個 Applet 南邊
       bt[3].setForeground(Color.red); validate( );  //
   } // init(
   Button bt[ ];   // 宣告說 bt[ ]  是 Button 陣列 (array)
   String bMsg[ ] = {"000", "Bt001", "btBBB",
       "哈哈哈會讀輸入", "bt西西", "BT五", "六 六 大順 囉", "Bye" };
   int kk = 0;  // Global variable to this class
   public void paint(Graphics g) {
       System.out.println(" Paint kk = " + kk++);
       // do drawing something ...
       validate( );  //
   } // paint(
  //////   ==================================================
   public void actionPerformed(ActionEvent e) {
       Object what = e.getSource( );  // 誰被按了一下 ?
       String gg = "You press " + e.getActionCommand( );
       if(what == bt[7]) System.exit(0);
       if(what == bt[1]) ppp.setBackground(Color.red);
       if(what == bt[6]) { ppp.setBackground(Color.green);
           tt.append("如果 林志玲 來演講, 你會去聽嗎? ");  } // if
       bbb.setLabel(gg);  tt.append(gg+"\n");
       if(what == bt[3])  { tt.append("讀到 " + tx.getText( ) + "\n");
           ppp.setBackground(Color.gray);  }
       repaint( );  // 會偷叫 update(Graphics);
   } // actionPerformed( 
   public void start( ) { System.out.println(" start  kk = " + kk++);
      super.start( ); }
   public void update(Graphics g) {  // 只是看看何時會跑過來這
       System.out.println(" update ==> kk = " + kk++);
       super.update(g); //  叫用上層(super class)的 update( )
   } // update(  will called by repaint( )
} // class
