신나게 개발하기

키이벤트 본문

홍익대 Java/수업

키이벤트

Yi,Junho 2009. 7. 30. 16:55
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyDemo extends JFrame implements KeyListener {
   private String [] area={"한국","중국","미국","미국","일본","캐나다","호주","러시아","프랑스","사우디"};
   private String line1;
   private JTextField text;
   private JLabel jh;
    
   public KeyDemo()
   {
      super( "Demonstrating Keystroke Events" );
      jh=new JLabel("숫자를 입력하세요");
	  text=new JTextField(20);
      
	 setLayout(new GridLayout(2,1));
	 JPanel panel = new JPanel();
	JPanel pane2 = new JPanel();
	panel.add(jh);
	panel.add(text);
	
	text.setEnabled(false);
	add(panel);


      addKeyListener( this );  // allow frame to process Key events
      setSize( 250, 150 );
      jh.setVisible( true );
	  text.setVisible( true );
	  setVisible( true );
   } // end KeyDemo constructor
   public void keyPressed( KeyEvent event )
   {
      line1 = event.getKeyText( event.getKeyCode() );
      char s ;
	  s=line1.charAt(0);
	 
		 setLines2and3( event,s);
		 
	
	 
	 
	  
   }
   // handle release of any key
   public void keyReleased( KeyEvent event )
   {
     
   }
   public void keyTyped( KeyEvent event )
   {
   }

   private void setLines2and3( KeyEvent event,char s )
   {
	   if (Character.isDigit(s)==true)
	   { 
		 
		  System.out.println(area[Integer.parseInt(line1)]);
		 text.setText(area[Integer.parseInt(line1)]);
		 
	   }
		else{
			JOptionPane.showMessageDialog(null,
            "숫자를 입력해주세요" );
			
		}

   }
   public static void main( String args[] )
   {
      KeyDemo application = new KeyDemo();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }
} 
0 Comments
댓글쓰기 폼