星期五, 9月 02, 2011

20110902 Java 練習小記


Swing
  • 元件 ( JComponent )
    • Swing 的零件
    • JLabel
      • 標籤
        • lb.setText("歡迎光臨");
        • 顯示文字
    • JButton
      • 按鈕
        • bt = new JButton("購買");
  • 容器 ( Container )
    • 包含元件的元件
    • Applet 就是容器
      • Applet 中會有一個 內容窗格 ( content pane ) 的中間容器, 並進行新增元件的處理
    • add(元件);
      • 將元件加入到容器
    • add(元件, BorderLayout.位置);
      • 將元件加入到容器
      • 指定元件位置
        • BorderLayout.NORTH
        • BorderLayout.SOUTH
  • 事件 ( Event )
    • 事件處理 ( event hadling )
    • 事件來源 ( event source )
    • 傾聽者 ( listener )
      • 負責處理事件的類別
      • bt.addActionListener( new SampleActionListener());
      • mouseClicked
        • 按下滑鼠
      • mouseEntered
        • 滑鼠進入範圍
      • mouseExited
        • 滑鼠離開範圍

package chapter19;
//撰寫 Swing Applet

import javax.swing.*;

public class Sample1 extends JApplet
{
private JLabel lb;
public void init()
{
//建立元件 製作一個標籤
lb = new JLabel();
//設定元件
lb.setText("歡迎光臨");
//新增到容器中
add(lb);
}

}



package chapter19;
//使用 JButton 按鈕元件
import javax.swing.*;

public class Sample2 extends JApplet
{
private JButton bt;
public void init()
{
//建立元件
bt = new JButton("購買");
//新增到容器中
add(bt);
}

}


package chapter19;
//將元件上下並排

import java.awt.BorderLayout;

import javax.swing.*;

public class Sample3 extends JApplet
{
private JLabel lb;
private JButton bt;
public void init()
{
//建立元件
lb = new JLabel("歡迎光臨");
bt = new JButton("購買");
//新增到容器內
//指定元件位置
add(lb, BorderLayout.NORTH);
add(bt, BorderLayout.SOUTH);

}

}


package chapter19;
//進行事件處理

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Sample4 extends JApplet
{
private JLabel lb;
private JButton bt;
public void init()
{
//建立元件
lb = new JLabel("歡迎光臨");
bt = new JButton("購買");
//新增到容器中
add(lb, BorderLayout.NORTH);
add(bt, BorderLayout.SOUTH);
//登錄 listener, 需要配合下面建立的類別
bt.addActionListener(new SampleActionListener());
}
class SampleActionListener implements ActionListener
{
public void actionPerformed( ActionEvent e)
{
lb.setText("謝謝惠顧");
}
}

}


package chapter19;
//點選滑鼠行為處理

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Sample5 extends JApplet
{
private JLabel lb;
public void init()
{
//建立元件
lb = new JLabel("歡迎光臨");
//新增到容器中
add(lb, BorderLayout.NORTH);
//登錄listener
addMouseListener(new SampleMouseListener());
}
//新增一個 listener 類別
class SampleMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
lb.setText("謝謝惠顧");
}
}

}


package chapter19;
//滑鼠進出的處理

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Sample6 extends JApplet
{
private JLabel lb;
public void init()
{
//建立元件
lb = new JLabel("您好");
//新增到容器中
add(lb, BorderLayout.NORTH);
//登錄 listener
addMouseListener(new SampleMouseListener());
}
class SampleMouseListener extends MouseAdapter
{
public void mouseEntered(MouseEvent e)
{
lb.setText("歡迎光臨");
}
public void mouseExited(MouseEvent e)
{
lb.setText("您好");
}
}

}


package chapter19;
//輸入按鍵時進行處理

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Sample7 extends JApplet
{
private JLabel lb1, lb2;
public void init()
{
//建立元件
lb1 = new JLabel("請選擇方向鍵");
lb2 = new JLabel();
//新增到容器中
add(lb1, BorderLayout.NORTH);
add(lb2, BorderLayout.SOUTH);
//登錄 listener
addKeyListener(new SampleKeyListener());
}

class SampleKeyListener extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
String str;
//取得輸入按鍵
int k = e.getKeyCode();
switch(k)
{
case KeyEvent.VK_UP:
str = "上"; 
break;
case KeyEvent.VK_DOWN:
str = "下";
break;
case KeyEvent.VK_LEFT:
str = "左";
break;
case KeyEvent.VK_RIGHT:
str = "右";
break;
default:
str = "其他按鍵";
}
lb2.setText(str + ".");
}
}
}

沒有留言: