JAVAイベント処理の3つの異なる方法

3946 ワード

     Java        ,              ,               。        ,   3   。   Event  ,              ,       ,          WindowEvent,        KeyEvent;      ,        ,      、       、    ;         ,            ,     。

              3      ,         。

        1、     

         Java   Listener         ,  ,                ,         ,        。 :

        

import java.awt.*;
import java.awt.event.*;

public class Example1  extends WindowAdapter implements ActionListener
{
    private Frame win;
    private TextField tf;
    private TextArea ta;
    private Button bun;
   Example1()
   {
       win = new Frame("     ");
       tf = new TextField(20);
       ta = new TextArea(10, 20);
       ta.setEditable(false);
       bun = new Button("  ");
       bun.addActionListener(this);
       win.setLayout(new FlowLayout());
       win.add(tf);
       win.add(bun);
       win.add(ta);
       win.addWindowListener(this);    

       win.setSize(300, 300);
       win.setVisible(true);
  
   }
 
   public void actionPerformed(ActionEvent e)
  {
      String s = tf.getText();
      if(s != "")
      {
         ta.append(s+"
"); } } public void windowClosing(WindowEvent e) { System.exit(0); } public static void main(String args[]) { Example1 w = new Example1(); } } ,Example1 WindowAdapter ActionListener , 3 : 、 , , ActionEvent , , actionPerformed 。win.addWindowListener(this); Example1 , , WindowEvent , Example1 windowClosing , , windowOpened( )、windowIconified( ) 。 2、 import java.awt.*; import java.awt.event.*; public class Example2 implements ActionListener { private Frame win; private TextField tf; private TextArea ta; private Button bun; Example2() { win = new Frame(" "); tf = new TextField(20); ta = new TextArea(10, 20); ta.setEditable(false); bun = new Button(" "); bun.addActionListener(this); win.setLayout(new FlowLayout()); win.add(tf); win.add(bun); win.add(ta); win.addWindowListener(new MyWinClose()); win.setSize(300, 300); win.setVisible(true); } public void actionPerformed(ActionEvent e) { String s = tf.getText(); if(s != "") { ta.append(s+"
"); } } class MyWinClose extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String args[]) { Example2 w = new Example2(); } } win.addWindowListener(new MyWinClose()); MyWinClose , WindowAdapter , windowClosing 。 : (1) , ; (2) ; (3) 。 3、 import java.awt.*; import java.awt.event.*; public class Example3 implements ActionListener { private Frame win; private TextField tf; private TextArea ta; private Button bun; Example3() { win = new Frame(" "); tf = new TextField(20); ta = new TextArea(10, 20); ta.setEditable(false); bun = new Button(" "); bun.addActionListener(this); win.setLayout(new FlowLayout()); win.add(tf); win.add(bun); win.add(ta); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); win.setSize(300, 300); win.setVisible(true); } public void actionPerformed(ActionEvent e) { String s = tf.getText(); if(s != "") { ta.append(s+"
"); } } public static void main(String args[]) { Example3 w = new Example3(); } } , , 。