problem with the KeyEvent VK_ESCAPE

hello there

here in my code i want to close my frame when the user hits the Esc key

on the keyboard i wrote the code as follows but no thing occurs

seems i've forgot something:

import java.io.*;

import java.awt.*;

import javax.swing.ImageIcon;

import javax.swing.*;

import javax.swing.JOptionPane;

import javax.swing.JButton;

import javax.swing.AbstractButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.ActionEvent;

import javax.swing.JFrame;

import javax.sound.sampled.*;

import java.awt.event.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

publicclass MyFrameextends JFrame

{

ImageIcon img =new ImageIcon("D:\\Other\\JAVA\\Icons\\heart7.png");

JButton btn=new JButton("Press Me", img);

MyInner inner;

MyInner5 inner5;

MyFrame ()

{

setupGUI();

}

privatevoid setupGUI()

{

JFrame f =new JFrame();

//f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

f.setTitle("Window Event");

f.setSize(550,350);

f.setResizable(false);

f.setLayout(new BorderLayout());

f.add("Center",btn);

btn.setVerticalTextPosition(AbstractButton.BOTTOM);

btn.setHorizontalTextPosition(AbstractButton.CENTER);

f.setVisible(true);

inner=new MyInner();

inner5=new MyInner5();

f.addWindowListener(inner);

f.addKeyListener(inner5);

}

class MyInnerextends WindowAdapter

{

publicvoid windowClosing(WindowEvent ee)

{

Toolkit tool = Toolkit.getDefaultToolkit();

tool.beep();

//JOptionPane.showMessageDialog(null, "Nice Work! ");

ImageIcon myImageIcon =new ImageIcon("D:\\Other\\JAVA\\Icons\\heart7.png");

JOptionPane.showMessageDialog(null,"Better Work!","Bye Bye!", JOptionPane.ERROR_MESSAGE, myImageIcon);

// JOptionPane.showMessageDialog(null, "Text", "Title", JOptionPane.Type, icon);

System.exit(0);

}

}

class MyInner5extends KeyAdapter

{

publicvoid keyPressed(KeyEvent k)

{

if (k.getKeyCode () == KeyEvent.VK_ESCAPE)

{

System.exit(0);

}

}

}

publicstaticvoid main(String[]args)

{

MyFrame frame=new MyFrame ();

}

}

[4641 byte] By [First_knighta] at [2007-11-27 7:37:11]
# 1
could any one help me?
First_knighta at 2007-7-12 19:17:43 > top of Java-index,Java Essentials,New To Java...
# 2
my question is simplewhy did no action happenedis there is something wrong or missing in the code?please help
First_knighta at 2007-7-12 19:17:43 > top of Java-index,Java Essentials,New To Java...
# 3
Probably frames don't get keyboard focus, so you need something to have keyboard focus to get key events. Try looking up "keyboard bindings", it'll make things easier.
bsampieria at 2007-7-12 19:17:43 > top of Java-index,Java Essentials,New To Java...
# 4
You should be using the InputMap along with the ActionMap in order to achieve such functionality.
Dalzhima at 2007-7-12 19:17:43 > top of Java-index,Java Essentials,New To Java...
# 5
would you please give me a simple demo?
First_knighta at 2007-7-12 19:17:43 > top of Java-index,Java Essentials,New To Java...
# 6
Try looking up "keyboard bindings", there's plenty of demo's out there already.
bsampieria at 2007-7-12 19:17:43 > top of Java-index,Java Essentials,New To Java...
# 7
didn't find suitable demoi want a simple one of closing JFrame when pressing Esc Key
First_knighta at 2007-7-12 19:17:43 > top of Java-index,Java Essentials,New To Java...