Java - AWTイベント


■ページ目次

Top

■イベント

Top
■イベント駆動のプログラムを作る
Top
■例題
  1. つぎのイベント・リスナー・クラス"MyActionListener01.java"と、それを実装したアプリケーション"Action01.java"を作成・実行してください。

    "MyActionListener01.java"
    1. import java.awt.event.*;
    2. public class MyActionListener01 implements ActionListener {
    3. public void actionPerformed(ActionEvent e) {
    4. System.exit(0);
    5. }
    6. }
    "Action01.java"
    1. import java.awt.*;
    2. public class Action01 {
    3. Frame frame;
    4. Button button;
    5. public Action01() {
    6. frame = new Frame("フレーム");
    7. button = new Button("ボタン");
    8. button.addActionListener(new MyActionListener01());
    9. frame.add(button);
    10. frame.pack();
    11. frame.setVisible(true);
    12. }
    13. public static void main(String[] args) {
    14. Action01 action01 = new Action01();
    15. }
    16. }

    □ 実行結果

    
    
Top
■イベント処理を1つのクラスで定義する
Top
■例題
  1. 上記クラス"MyActionListener01.java"と、それを実装したアプリケーション"Action01.java"を、匿名インナー・クラスでまとめたアプリケーション"Action02.java"を作成・実行してください。
    1. import java.awt.*;
    2. import java.awt.event.*;
    3. public class Action02 {
    4. Frame frame;
    5. Button button;
    6. public Action02() {
    7. frame = new Frame("フレーム");
    8. button = new Button("ボタン");
    9. button.addActionListener(new ActionListener() {
    10. public void actionPerformed(ActionEvent e) {
    11. System.exit(0);
    12. }
    13. });
    14. frame.add(button);
    15. frame.pack();
    16. frame.setVisible(true);
    17. }
    18. public static void main(String[] args) {
    19. Action02 action02 = new Action02();
    20. }
    21. }

    □ 実行結果

    
    
Top
■さまざまなイベント
Top
■例題
  1. つぎのクラス"Mouse02.java"を作成・実行してください。
    1. import java.awt.*;
    2. import java.awt.event.*;
    3. public class Mouse02 {
    4. Frame frame;
    5. Mouse02() {
    6. frame = new Frame("フレーム");
    7. frame.addMouseListener(new MouseAdapter() {
    8. public void mouseClicked(MouseEvent e) {
    9. System.out.println(e.getX() + ":" + e.getY());
    10. }
    11. } );
    12. frame.setSize(400, 200);
    13. frame.setVisible(true);
    14. }
    15. public static void main(String[] args) {
    16. Mouse02 mouse02 = new Mouse02();
    17. }
    18. }

    □ 実行結果

    
    
Top
■アダプタクラス
Top
■イベントオブジェクト
■商品管理クライアントへのイベントの実装
  1. 商品管理システムのTestProductをGUIで実現するアプリケーション"GUIProduct.java"にイベントを実装してください。
    1. // 1901
    2. // package application;
    3. import java.awt.*;
    4. import java.awt.event.*;
    5. // 商品システムGUIクライアント・アプリケーションです。
    6. public class GUIProduct {
    7. private Frame frame;
    8. private Panel northPanel, westPanel, centerPanel, eastPanel, southPanel;
    9. private Label codeLabel, nameLabel, priceLabel, discountLabel, messageLabel;
    10. private TextField codeField, nameField, priceField, discountField;
    11. private TextArea messageArea;
    12. private Button searchButton, updateButton, deleteButton, resetButton, exitButton;
    13. private String code;
    14. private String name;
    15. private int price;
    16. private String supplierId;
    17. private String message;
    18. public GUIProduct() {
    19. frame = new Frame("商品情報");
    20. northPanel = new Panel();
    21. westPanel = new Panel();
    22. centerPanel = new Panel();
    23. eastPanel = new Panel();
    24. southPanel = new Panel();
    25. westPanel.setLayout(new GridLayout(0, 1));
    26. centerPanel.setLayout(new GridLayout(0, 1));
    27. searchButton = new Button("検索");
    28. searchButton.addActionListener(new ActionListener() {
    29. public void actionPerformed(ActionEvent e) {
    30. System.out.println("clecked search button");
    31. System.out.println(" product code: " + codeField.getText());
    32. messageArea.setText("clecked search button"
    33. + System.getProperty("line.separator")
    34. + " product code: " + codeField.getText()
    35. + System.getProperty("line.separator")
    36. );
    37. }
    38. } );
    39. northPanel.add(searchButton);
    40. updateButton = new Button("新規/更新");
    41. updateButton.addActionListener(new ActionListener() {
    42. public void actionPerformed(ActionEvent e) {
    43. System.out.println(" product code: " + codeField.getText());
    44. System.out.println(" product name: " + nameField.getText());
    45. System.out.println(" price: " + Integer.parseInt(priceField.getText()));
    46. System.out.println(" message: " + messageArea.getText());
    47. System.out.println();
    48. }
    49. } );
    50. northPanel.add(updateButton);
    51. deleteButton = new Button("削除");
    52. deleteButton.addActionListener(new ActionListener() {
    53. public void actionPerformed(ActionEvent e) {
    54. System.out.println("clecked delete button");
    55. System.out.println(" product code: " + codeField.getText());
    56. System.out.println();
    57. }
    58. } );
    59. northPanel.add(deleteButton);
    60. resetButton = new Button("取消");
    61. resetButton.addActionListener(new ActionListener() {
    62. public void actionPerformed(ActionEvent e) {
    63. codeField.setText("");
    64. nameField.setText("");
    65. priceField.setText("");
    66. messageArea.setText("");
    67. System.out.println("clecked reset button");
    68. System.out.println();
    69. }
    70. } );
    71. northPanel.add(resetButton);
    72. exitButton = new Button("終了");
    73. exitButton.addActionListener(
    74. new ActionListener() {
    75. public void actionPerformed(ActionEvent e) {
    76. System.exit(0);
    77. }
    78. }
    79. );
    80. northPanel.add(exitButton);
    81. codeLabel = new Label("商品コード:");
    82. codeField = new TextField(8);
    83. westPanel.add(codeLabel);
    84. centerPanel.add(codeField);
    85. nameLabel = new Label("商品名:");
    86. nameField = new TextField(30);
    87. westPanel.add(nameLabel);
    88. centerPanel.add(nameField);
    89. priceLabel = new Label("定価:");
    90. priceField = new TextField(8);
    91. westPanel.add(priceLabel);
    92. centerPanel.add(priceField);
    93. discountLabel = new Label("割引率:");
    94. discountField = new TextField(8);
    95. westPanel.add(discountLabel);
    96. centerPanel.add(discountField);
    97. messageLabel = new Label("メッセージ:");
    98. messageArea = new TextArea();
    99. southPanel.add(messageArea);
    100. frame.add(northPanel, BorderLayout.NORTH);
    101. frame.add(westPanel, BorderLayout.WEST);
    102. frame.add(centerPanel, BorderLayout.CENTER);
    103. frame.add(eastPanel, BorderLayout.EAST);
    104. frame.add(southPanel, BorderLayout.SOUTH);
    105. frame.setSize(480, 320);
    106. frame.setVisible(true);
    107. }
    108. public static void main(String[] args) {
    109. GUIProduct gp = new GUIProduct();
    110. }
    111. }

    □ 実行結果

    
    
Top
■商品管理クライアントとサーバーとの通信
  1. 商品管理サーバーと通信して商品情報を検索するアプリケーション"GUIProduct.java"にイベントを実装してください。
    1. // 1902
    2. // package application;
    3. import java.awt.*;
    4. import java.awt.event.*;
    5. import java.io.*;
    6. import java.net.*;
    7. // 商品システムGUIクライアント・アプリケーションです。
    8. public class GUIProduct {
    9. private static final int SERVER_PORT = 54321;
    10. private static String serverName = null;
    11. private static Socket sock = null;
    12. private Frame frame;
    13. private Panel northPanel, westPanel, centerPanel, eastPanel, southPanel;
    14. private Label codeLabel, nameLabel, priceLabel, discountLabel, messageLabel;
    15. private TextField codeField, nameField, priceField, discountField;
    16. private TextArea messageArea;
    17. private Button searchButton, updateButton, deleteButton, resetButton, exitButton;
    18. private String code;
    19. private String name;
    20. private int price;
    21. private String message;
    22. public GUIProduct() {
    23. frame = new Frame("商品情報");
    24. northPanel = new Panel();
    25. westPanel = new Panel();
    26. centerPanel = new Panel();
    27. eastPanel = new Panel();
    28. southPanel = new Panel();
    29. westPanel.setLayout(new GridLayout(0, 1));
    30. centerPanel.setLayout(new GridLayout(0, 1));
    31. searchButton = new Button("検索");
    32. searchButton.addActionListener(new ActionListener() {
    33. public void actionPerformed(ActionEvent e) {
    34. System.out.println("clecked search button");
    35. System.out.println(" product code: " + codeField.getText());
    36. messageArea.setText("clecked search button"
    37. + System.getProperty("line.separator")
    38. + " product code: " + codeField.getText()
    39. + System.getProperty("line.separator")
    40. );
    41. query(codeField.getText());
    42. }
    43. } );
    44. northPanel.add(searchButton);
    45. updateButton = new Button("新規/更新");
    46. updateButton.addActionListener(new ActionListener() {
    47. public void actionPerformed(ActionEvent e) {
    48. System.out.println(" product code: " + codeField.getText());
    49. System.out.println(" product name: " + nameField.getText());
    50. System.out.println(" price: " + Integer.parseInt(priceField.getText()));
    51. System.out.println(" message: " + messageArea.getText());
    52. System.out.println();
    53. }
    54. } );
    55. northPanel.add(updateButton);
    56. deleteButton = new Button("削除");
    57. deleteButton.addActionListener(new ActionListener() {
    58. public void actionPerformed(ActionEvent e) {
    59. System.out.println("clecked delete button");
    60. System.out.println(" product code: " + codeField.getText());
    61. System.out.println();
    62. }
    63. } );
    64. northPanel.add(deleteButton);
    65. resetButton = new Button("取消");
    66. resetButton.addActionListener(new ActionListener() {
    67. public void actionPerformed(ActionEvent e) {
    68. codeField.setText("");
    69. nameField.setText("");
    70. priceField.setText("");
    71. messageArea.setText("");
    72. System.out.println("clecked reset button");
    73. System.out.println();
    74. }
    75. } );
    76. northPanel.add(resetButton);
    77. exitButton = new Button("終了");
    78. exitButton.addActionListener(
    79. new ActionListener() {
    80. public void actionPerformed(ActionEvent e) {
    81. System.exit(0);
    82. }
    83. }
    84. );
    85. northPanel.add(exitButton);
    86. codeLabel = new Label("商品コード:");
    87. codeField = new TextField(8);
    88. westPanel.add(codeLabel);
    89. centerPanel.add(codeField);
    90. nameLabel = new Label("商品名:");
    91. nameField = new TextField(30);
    92. westPanel.add(nameLabel);
    93. centerPanel.add(nameField);
    94. priceLabel = new Label("定価:");
    95. priceField = new TextField(8);
    96. westPanel.add(priceLabel);
    97. centerPanel.add(priceField);
    98. discountLabel = new Label("割引率:");
    99. discountField = new TextField(8);
    100. westPanel.add(discountLabel);
    101. centerPanel.add(discountField);
    102. messageLabel = new Label("メッセージ:");
    103. messageArea = new TextArea();
    104. southPanel.add(messageArea);
    105. frame.add(northPanel, BorderLayout.NORTH);
    106. frame.add(westPanel, BorderLayout.WEST);
    107. frame.add(centerPanel, BorderLayout.CENTER);
    108. frame.add(eastPanel, BorderLayout.EAST);
    109. frame.add(southPanel, BorderLayout.SOUTH);
    110. frame.setSize(480, 320);
    111. frame.setVisible(true);
    112. }
    113. public static void main(String[] args) {
    114. serverName = args[0];
    115. GUIProduct gp = new GUIProduct();
    116. }
    117. void query(String code) {
    118. try {
    119. // クライアントと対話処理するストリーム、ソケットを生成
    120. sock = new Socket(serverName, SERVER_PORT);
    121. DataInputStream dis =
    122. new DataInputStream(sock.getInputStream());
    123. DataOutputStream dos =
    124. new DataOutputStream(sock.getOutputStream());
    125. dos.writeUTF(code);
    126. String str = dis.readUTF();
    127. System.out.println("商品コード " + code + " の商品は " + str + " です。");
    128. if (str.equals("[Not Found]")) {
    129. return;
    130. }
    131. String prods[] = str.substring(1, str.length()-1).split(",");
    132. nameField.setText(prods[1].trim());
    133. priceField.setText(prods[2].trim());
    134. // ストリーム、ソケットをクローズ
    135. dos.close();
    136. dis.close();
    137. sock.close();
    138. } catch (IOException e) {
    139. e.printStackTrace();
    140. } finally {
    141. try {
    142. sock.close();
    143. } catch (IOException e) {
    144. e.printStackTrace();
    145. }
    146. }
    147. }
    148. }

    □ 実行結果

    
    
Top
■実習
  1. "Uranai02.java"の「終了」ボタンをクリックすると、アプリケーションが終了するようにしてください。("Uranai03.java")


  2. "Uranai03.java"の「占う」ボタンをクリックすると、入力した名前をもとに、占い結果を出すようにしてください。("Uranai04.java")
  3. □ 実行結果

    
    
  4. "Uranai04.java"の「×」ボタンをクリックすると、アプリケーションが終了するようにしてください。("Uranai05.java")


  5. クリックした位置に円を描くアプリケーション"ClickCircle01.java"を作成してください。
    1. import java.awt.*;
    2. import java.awt.event.*;
    3. public class ClickCircle01 extends Canvas {
    4. Frame frame;
    5. private int x = 0, y = 0;
    6. private int w = 10, h = w;
    7. ClickCircle01(){
    8. // ここにコードを記述してください。
    9. addMouseListener(ml);
    10. setSize(300, 200);
    11. setBackground(Color.WHITE);
    12. frame = new Frame("クリックしてください");
    13. frame.add(this);
    14. frame.pack();
    15. frame.setVisible(true);
    16. }
    17. public void paint(Graphics g){
    18. // ここにコードを記述してください。
    19. }
    20. public static void main(String[] args){
    21. ClickCircle01 d = new ClickCircle01();
    22. }
    23. }

    □ 実行結果

    
    
  6. ドラッグして描画するアプリケーション"DragDraw01.java"を作成してください。
    1. import java.awt.*;
    2. import java.awt.event.*;
    3. public class DragDraw01 extends Frame {
    4. private int x = 0, y = 0;
    5. private int w = 5, h = 5;
    6. DragDraw01(String s){
    7. super(s);
    8. addWindowListener(new WindowAdapter(){
    9. public void windowClosing(WindowEvent e){
    10. System.exit(0);
    11. }
    12. });
    13. addMouseMotionListener(new MouseMotionAdapter(){
    14. public void mouseDragged(MouseEvent e){
    15. // ここにコードを記述してください。
    16. }
    17. });
    18. }
    19. public void update(Graphics g){
    20. // ここにコードを記述してください。
    21. }
    22. public void paint(Graphics g){
    23. // ここにコードを記述してください。
    24. }
    25. public static void main(String[] args){
    26. DragDraw01 d = new DragDraw01("ドラッグしてください");
    27. d.setSize(600, 400);
    28. d.setBackground(Color.white);
    29. d.setVisible(true);
    30. }
    31. }

    □ 実行結果

    
    
Top
おつかれさまでした。


inserted by FC2 system