Profile

ys2310

Author:ys2310
2008年春にNew York Cityにあるふる〜い大学を卒業。


Categories


new postings


new comments


new trackbacks


monthly archeive


FC2ブログ 転職
DATE: CATEGORY:スポンサー広告
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
| BLOG TOP |
DATE: CATEGORY:Java swing

アクション・イベントとなんだろうか

アプリケーションに対してユーザの操作に対応した動作を指定できます。ユーザのアクションはイベントとしてアプリケーションに受けとられ、イベント に対応した動作を記述できます。アクションはコンポーネントなどのオブジェクトであるイベントソース (Event Source)から受け取られ、対応した動作を記述したイベントリスナー (Event Listner) に渡されます。イベント自身はオブジェクトであり、イベントソースを識別する情報を含みます。

Act that results in the eventListener type
ボタンクリック、テキストフィールドにフォーカス中にReturnキー、メニュー項目の選択ActionListener
フレーム(メインウィンドウ)の終了WindowListener
コンポーネントのクリックMouseListener
コンポーネント上にマウスカーソルを移動MouseMotionListener
コンポーネントが可視化ComponentListener
コンポーネントがキーボード入力に対してフォーカスFocusListener
テーブルやリストの選択が変更ListSelectionListener

発生したイベントはオブジェクトとして扱われ、発生したイベントの種類に応じたイベントリスナーが受け取って処理します。イベントが発生したオブジェクトをイベント・ソースと呼び、発生したイベントを受け取るオブジェクトをイベント・リスナーと呼びます。

  1. イベントを処理するアクション・リスナー・オブジェクトのために、イベント・タイプに応じたインタフェースを実装したクラスを作成します。
  2. イベントが発生するオブジェクトに、発生したアクション・イベントを受け取るための、イベント・リスナーを登録します。
  3. 実行時には、イベントが発生すると、指定されたイベント・リスナーの、関連するメソッドが自動的に呼び出され、イベントが引数として渡されます。

例えば、マウスの動作を処理するためのイベント・リスナーは MouseListener インタフェースを実装します。例えば、コンポーネントにマウスが入ると、このリスナーの mouseEntered(MouseEvent e) が呼び出されます。コンポーネントにこのリスナーを追加するメソッドは、addMouseListener(MouseListener l) です。

また、ボタンのクリックなど、典型的なアクションを処理するためのリスナーは ActionListener インタフェースを実装します。イベントが発生すると、 actionPerformed(ActionEvent e) が呼び出されます。コンポーネントにこのリスナーを追加するメソッドは、 addActionListener(ActionListener l) です。

イベントの実装方法

以下、四つ実装方法を示しますが、例1と2がよく使われます。

イベント処理その1

次に挙げるのは、自分自身がリスナークラスの場合です。

class MyPanel extends JPanel implements MouseListener {
MyPanel() {
...
JButton button = new JButton();
button.addMouseListener(this);
...
}

// 以下 MouseListener の実装
public void mouseClicked(MouseEvent e) {
...
}
public void mouseEntered(MouseEvent e){
...
}
public void mouseExited(MouseEvent e) {
...
}
public void mousePressed(MouseEvent e) {
...
}
public void mouseReleased(MouseEvent e) {
...
}
}

イベント処理その2

一コンポネントがリスナークラスの場合です。ActionListener interfaceをすぐさま実装し
classとしてinstance化しています。mybuttonActionPerformed methodは省略できます。

class MyFrame extends JFrame {
public static void main(String[] args) {
JButton button = new JButton();
button.addActionListener(new java.awt.event.ActionListener(){
public void mouseClicked(java.awt.event.MouseEvent evt) {
mybuttonActionPerformed(evt);
}
});
}

private void mybuttonActionPerformed(java.awt.event.MouseEvent evt) {
...
}
}

イベント処理その3

次に挙げるのは、イベント処理するクラスが別途定義されている場合です。

class MyFrame extends JFrame {
public static void main(String[] args) {
...
EventHandler l = new EventHandler();
JButton button = new JButton();
button.addActionListener(l);
...
}
}

class EventHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
...
}
}

イベント処理その4

次に挙げるのは、イベント処理のクラスが内部クラスとして実装しているものです。

class MyFrame extends JFrame {
public static void main(String[] args) {
...
EventHandler l = new EventHandler();
JButton button = new JButton();
button.addActionListener(l);
...
}

class EventHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
...
}
}
}

[Quote]:http://www.nextindex.net/java/Swing/event.html

| BLOG TOP |
DATE: CATEGORY:Java swing

リスナーとは

リスナー とは、ボタンをクリックした、メニューを実行した、ウィンドウをリサイズした、マウスを動かした、キーを押したなどのイベント監視し、イベント発生時に対応するアクションを実行するオブジェクトです。主なリスナーには次のようなものがあります。

リスナー イベント 主な監視対象
ActionListener actionPerformed MenuItem, List, TextField, Button, AbstructButton, JTextField, ButtonModel, JComboBox, Timer, DefaultButtonModel, ComboBoxEditor, JFileChooser, BasicComboBoxEditor
WindowListener windowOpened
windowClosing
windowClosed
windowIconified
windowDeiconified
windowActivated
windowDeactivated
Window, JWindow, Frame, Dialog
MouseListener mouseClicked
mousePressed
mouseReleased
mouseEntered
mouseExited
Component, Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent, TextField, TextArea
MouseMotionListener mouseDragged
mouseMoved
Component, Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent, TextField, TextArea
KeyListener keyPressed
keyReleased
keyTyped
Component, Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent, TextField, TextArea
TextListener textValueChanged TextComponent, TextField, TextArea
ItemListener itemStateChanged Checkbox, List, Choice, ItemSelectable, CheckboxMenuItem, AbstractButton, ButtonModel, JComboBox, DefaultButtonModel

■ アクションリスナー(ActionListener

ボタンが押されたなどのイベントを監視して、イベント発生時にアクションを実行するサンプルです。

§ActionListenerTest.java
import java.awt.*;
import java.awt.event.*;

public class ActionListenerTest extends Frame implements ActionListener {
ActionListenerTest() {
super("ActionListenerTest");
Button b1 = new Button("BUTTON1");
b1.addActionListener(this);
add(b1);
setSize(200, 100);
show();
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}

public static void main(String [] args) {
new ActionListenerTest();
}
}

ボタン(b1)が押されたことの監視は アクションリスナオブジェクト が行います。アクションリスナオブジェクトは、ActionListener インタフェースを実装(implement)したオブジェクトです。リスナーは実装すべきインタフェースが決まっています。ActionListener の場合は actionPerformed() というインタフェースを実装しなくてはなりません。サンプルでは、自分自身(this)がアクションリスナオブジェクトとなっています。イベント発生時に actionPerformed() インタフェースでこれを捕獲し、System.exit(0); でプログラムを終了しています。

下記のように、インナークラスのインスタンスをアクションリスナオブジェクトとすることもできます。

§ActionListenerTest2.java
import java.awt.*;
import java.awt.event.*;

public class ActionListenerTest2 extends Frame {
ActionListenerTest2() {
super("ActionListenerTest2");
Button b1 = new Button("BUTTON1");
b1.addActionListener(new MyActionListener());
add(b1);
setSize(200, 100);
show();
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String [] args) {
new ActionListenerTest2();
}
}

■ ウィンドウリスナー(WindowListener)

ウィンドウが、開かれた、閉じようとしている、閉じた、アイコン化された、アイコン化解除された、アクティブになった、非アクティブになったというイベントを監視します。

§WindowListenerTest.java
import java.awt.*;
import java.awt.event.*;

public class WindowListenerTest extends Frame implements WindowListener {
WindowListenerTest() {
super("WindowListenerTest");
this.addWindowListener(this);
setSize(200, 100);
show();
}
public void windowOpened(WindowEvent e) { // 開かれた
System.out.println("windowOpened");
}
public void windowClosing(WindowEvent e) { // 閉じられている
System.out.println("windowClosing");
}
public void windowClosed(WindowEvent e) { // 閉じた
System.out.println("windowClosed");
}
public void windowIconified(WindowEvent e) { // アイコン化された
System.out.println("windowIconified");
}
public void windowDeiconified(WindowEvent e) { // 非アイコン化された
System.out.println("windowDeiconified");
}
public void windowActivated(WindowEvent e) { // アクティブになった
System.out.println("windowActivated");
}
public void windowDeactivated(WindowEvent e) { // 非アクティブになった
System.out.println("windowDeactivated");
}
public static void main(String [] args) {
new WindowListenerTest();
}
}

7個すべてのインタフェースを実装するのが面倒なときは、WindowAdapter を用いると便利です。

■ マウスリスナー(MouseListener)

マウスのボタンに関するイベントを監視します。

§MouseListenerTest.java
import java.awt.*;
import java.awt.event.*;

public class MouseListenerTest extends Frame implements MouseListener {
MouseListenerTest() {
super("MouseListenerTest");
this.addMouseListener(this);
setSize(200, 100);
show();
}
public void mouseClicked(MouseEvent e) {
System.out.println("mouseClicked");
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited");
}

public static void main(String [] args) {
new MouseListenerTest();
}
}

■ マウスモーションリスナー(MouseMotionListener)

マウスの動きに関するイベントを監視します。

§MouseMotionListenerTest.java
import java.awt.*;
import java.awt.event.*;

public class MouseMotionListenerTest extends Frame implements MouseMotionListener {
MouseMotionListenerTest () {
super("MouseMotionListenerTest");
addMouseMotionListener(this);
setSize(200, 100);
show();
}
public void mouseDragged(MouseEvent e) {
System.out.println("D: " + e.getX() + ", " + e.getY());
}
public void mouseMoved(MouseEvent e) {
System.out.println("M: " + e.getX() + ", " + e.getY());
}

public static void main(String [] args) {
new MouseMotionListenerTest();
}
}

■ キーリスナー(KeyListener)

キー入力に関するイベントを監視します。

§KeyListenerTest.java
import java.awt.*;
import java.awt.event.*;

public class KeyListenerTest extends Frame implements KeyListener {
KeyListenerTest() {
super("KeyListenerTest");
TextField tf1 = new TextField();
tf1.addKeyListener(this);
add(tf1);
setSize(200, 100);
show();
}
public void keyPressed(KeyEvent e) {
System.out.println("Press: " + e.getKeyText(e.getKeyCode()));
}
public void keyReleased(KeyEvent e) {
System.out.println("Release: " + e.getKeyText(e.getKeyCode()));
}
public void keyTyped(KeyEvent e) {
System.out.println("Type: " + e.getKeyChar());
}

public static void main(String [] args) {
new KeyListenerTest();
}
}

■ テキストリスナー(TextListener)

テキスト入力に関するイベントを監視します。Swing では DocumentListener を使用するのが一般的です。

§TextListenerTest.java
import java.awt.*;
import java.awt.event.*;

public class TextListenerTest extends Frame implements TextListener {
TextListenerTest () {
super("TextListenerTest");
TextField tf1 = new TextField();
tf1.addTextListener(this);
add(tf1);
setSize(200, 100);
show();
}
public void textValueChanged(TextEvent e) {
TextField tf = (TextField)e.getSource();
System.out.println(tf.getText());
}

public static void main(String [] args) {
new TextListenerTest();
}
}

■ アイテムリスナー(ItemListener)

チェックボックスなどのアイテムの状態を監視するリスナーです。

§ItemListenerTest.java
import java.awt.*;
import java.awt.event.*;

public class ItemListenerTest extends Frame implements ItemListener {
ItemListenerTest() {
super("ItemListenerTest");
Checkbox cb1 = new Checkbox("CB1");
cb1.addItemListener(this);
add(cb1);
setSize(200, 100);
show();
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("SELECTED");
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
System.out.println("DESELECTED");
}
}

public static void main(String [] args) {
new ItemListenerTest();
}
}

■ アダプター(XxxxAdapter)

ウィンドウリスナーを実装するには、必要の有無に関わらず windowOpened など 7個のインタフェースを実装する必要があります。この面倒を簡略するために、インタフェースを複数持つものについては アダプター が用意されています。下記のように、ウィンドウリスナーを WindowAdapter のサブクラスとして定義することにより、必要なインタフェースのみを実装することが可能になります。

§WindowAdapterTest.java
import java.awt.*;
import java.awt.event.*;

public class WindowAdapterTest extends Frame {
WindowAdapterTest() {
super("WindowAdapterTest");
this.addWindowListener(new MyWindowListener());
setSize(200, 100);
show();
}
public static void main(String [] args) {
new WindowAdapterTest();
}
}

class MyWindowListener extends WindowAdapter {
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
}

[Quote]:http://64.233.167.104/search?q=cache:U4F3Nc77-l0J:www.tohoho-web.com/java/listener.htm+Java+actionlistener&hl=en&ct=clnk&cd=3&gl=us&lr=lang_en%7Clang_ja&client=firefox-a

| BLOG TOP |

copyright © Manhattan life all rights reserved.Powered by FC2ブログ