by Dinesh Thakur Category: Swing

 

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class KeyFrameJavaExample extends JFrame implements KeyListener
{
       private JLabel prmpt = new JLabel("Type Something in the TextBox");
       private JLabel OutPutLbl = new JLabel("Key Typed:");
       private JTextField TxtFld = new JTextField(10);
       public KeyFrameJavaExample()
         {
             setTitle("Key Frame in Java Swing");
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             setLayout(new BorderLayout());
             add(prmpt, BorderLayout.NORTH);
             add(TxtFld, BorderLayout.CENTER);
             add(OutPutLbl, BorderLayout.SOUTH);
             addKeyListener(this);
             TxtFld.addKeyListener(this);
        }
             public void keyTyped(KeyEvent e)
               {
                  char c = e.getKeyChar();
                  OutPutLbl.setText("Last Character you Typed : " + c);
               }
                  public void keyPressed(KeyEvent e)
                    {
                    }
                     public void keyReleased(KeyEvent e)
                        {
                          }
                     public static void main(String[] args)
                           {
                                 KeyFrameJavaExample keyFrame = new KeyFrameJavaExample();
                                     final int WIDTH = 250;
                        final int HEIGHT = 100;
                                 keyFrame.setSize(WIDTH, HEIGHT);
                                 keyFrame.setVisible(true);
                           }
  }

Key Frame  in Java Swing Example

Popular Article