Javaイベントの初歩
GUIプログラムは現在まで発展し、事件はずっとその核心である.
従来のWin 32プログラムから,イベントを処理するにはイベントキューをチェックするためにコードを記述する必要があり,コードはswitchによって制御され,この方式で記述されたプログラムは可読性が悪い.
その後のVisual Basic,.NETは,イベントをプログラマーに隠す.
では、Javaではどのようにしてイベントを処理していますか?
Javaはイベント制御を統合し,イベントソースからイベントリスナーへの伝達を実現した.
イベント・ソースは、次のようにイベント・リスナー・オブジェクトを登録します.
eventSourceObject.addEventListener(eventListenerObject);
eg: ActionListener listener = ...; JButton button = new JButton("Ok"); button.addActionListener(listener);
次に例を挙げて、Frameを作って、上にパネルを置いて、パネルの上にもう一つ置きます.
button、buttonをクリックしてダイアログボックスをポップアップします.
これは非常に簡単な例です.
方法1:イベントリスナーをパネルのプライベートな内部クラスにすることができます
方法2:イベントリスナーを匿名クラスにすることもできます
このケースをさらに拡張すると、イベントリスナーにパラメータを入力できます.ケースは次のとおりです.
従来のWin 32プログラムから,イベントを処理するにはイベントキューをチェックするためにコードを記述する必要があり,コードはswitchによって制御され,この方式で記述されたプログラムは可読性が悪い.
その後のVisual Basic,.NETは,イベントをプログラマーに隠す.
では、Javaではどのようにしてイベントを処理していますか?
Javaはイベント制御を統合し,イベントソースからイベントリスナーへの伝達を実現した.
イベント・ソースは、次のようにイベント・リスナー・オブジェクトを登録します.
eventSourceObject.addEventListener(eventListenerObject);
eg: ActionListener listener = ...; JButton button = new JButton("Ok"); button.addActionListener(listener);
次に例を挙げて、Frameを作って、上にパネルを置いて、パネルの上にもう一つ置きます.
button、buttonをクリックしてダイアログボックスをポップアップします.
これは非常に簡単な例です.
方法1:イベントリスナーをパネルのプライベートな内部クラスにすることができます
package corejava2.event;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleButton1 {
public static void main(String[] args) {
SimpleFrame1 frame = new SimpleFrame1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* A frame with a button panel
*/
class SimpleFrame1 extends JFrame {
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public SimpleFrame1() {
setTitle("Simple Button Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
//add panel to frame
SimplePanel1 panel = new SimplePanel1();
add(panel);
}
}
/**
* A panel with one simple
*/
class SimplePanel1 extends JPanel {
public SimplePanel1() {
JButton simpleButton = new JButton("Simple Button");
add(simpleButton);
ButtonAction buttonAction = new ButtonAction();
simpleButton.addActionListener(buttonAction);
}
/**
* An action listener which is an inner class
*/
private class ButtonAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, "You clicked the button!");
}
}
}
方法2:イベントリスナーを匿名クラスにすることもできます
package corejava2.event;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleButton2 {
public static void main(String[] args) {
SimpleFrame2 frame = new SimpleFrame2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* A frame with a button panel
*/
class SimpleFrame2 extends JFrame {
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public SimpleFrame2() {
setTitle("Simple Button Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
//add panel to frame
SimplePanel2 panel = new SimplePanel2();
add(panel);
}
}
/**
* A panel with one simple
*/
class SimplePanel2 extends JPanel {
public SimplePanel2() {
JButton simpleButton = new JButton("Simple Button");
add(simpleButton);
simpleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, "You clicked the button!");
}
});
}
}
このケースをさらに拡張すると、イベントリスナーにパラメータを入力できます.ケースは次のとおりです.
package corejava2.event;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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 static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public ButtonFrame() {
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add panel to frame
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
/**
* 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 {
private Color backgroundColor;
public ColorAction(Color c) {
backgroundColor = c;
}
public void actionPerformed(ActionEvent event) {
String mColor = "";
setBackground(backgroundColor);
if (backgroundColor == Color.YELLOW)
mColor = "Yellow";
else if (backgroundColor == Color.BLUE)
mColor = "Blue";
else if (backgroundColor == Color.RED)
mColor = "Red";
JOptionPane.showMessageDialog(null, "You choosed " + mColor + " button");
}
}
}