XXXListener 모니터

3004 단어 자바
더 읽 기

package testDemo1;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


/*
 *   .addXXXListener(new XXXAdapter(){
 * 
 *     public void methodname(XXXEvent e){}    
 *     
 *     });    
 *     
 *     */
public class FrameTest {

	public static void main(String[] args) {
		Frame f = new Frame("       ");//         
		f.setSize(400, 600);//       
		f.setLocation(500, 50);//         
		f.setVisible(true);//     
        
		//        
		Button but = new Button("my button");//               
		f.setLayout(new FlowLayout());//         (    ),             
		f.add(but);//        
		
		//         
		TextField tf = new TextField(40);//         40
		TextField tf2 = new TextField(40);//      
		f.add(tf);
		f.add(tf2);

		but.addActionListener(new ActionListener() {//         
			
			@Override
			public void actionPerformed(ActionEvent e) {//             
				System.out.println("     " + e);
			}
			
		});
		
		but.addMouseListener(new MouseAdapter() {//        

			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("    ");
				
				if(e.getClickCount() == 2) {//       2 
					System.out.println("    ");
				}
			}
			
		});

		f.addWindowListener(new WindowAdapter() {//       
			
			@Override
			public void windowClosing(WindowEvent e) {
				System.out.println("    ");//          
				System.exit(0);//     (           )
			}
			
		});
		
		tf.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent e) {
				char key = e.getKeyChar();
				int code = e.getKeyCode();
				System.out.println("      :"+key);//      ABCD   
				System.out.println("      :"+KeyEvent.getKeyText(code));//          
				System.out.println("   ASCII  "+code);
			}

			
			
		});
		
		tf2.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent e) {
				int code = e.getKeyCode();
				if(!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9)) {//      0-9(    )
					System.out.println("   0-9   ");
				}
			}
			
		});

	}

}


좋은 웹페이지 즐겨찾기