Keyboard listener don't work!
The code:
private void this_keyTyped(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER) // this is just a test
{
jLabel1.setLocation(new Point(x--, y));
}
else
{
System.out.println("\n Error!");
}
}
do not work!
It moves when:
private void this_keyTyped(KeyEvent e)
{
jLabel1.setLocation(new Point(x--, y));
}
What is wrong?
Whole code:
/****************************************************************/
/* av*/
/* */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Summary description for av
*
*/
public class av extends JFrame
{
// Variables declaration
int x;
int y;
private JLabel jLabel1;
private JPanel contentPane;
// End of variables declaration
public av()
{
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//
this.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent()
{
jLabel1 = new JLabel();
contentPane = (JPanel)this.getContentPane();
x = 164;
y = 106;
//
// jLabel1
//
jLabel1.setIcon(new ImageIcon("D:\\Documents and Settings\\Administrator\\Desktop\\b-batsu.GIF"));
//
// contentPane
//
contentPane.setLayout(null);
contentPane.setBackground(new Color(255, 254, 254));
addComponent(contentPane, jLabel1, 164,106,30,42);
//
// av
//
this.setTitle("av - extends JFrame");
this.setLocation(new Point(30, 37));
this.setSize(new Dimension(390, 300));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e)
{
this_keyTyped(e);
}
});
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void this_keyTyped(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER) // this is just a test
{
jLabel1.setLocation(new Point(x--, y));
}
else
{
System.out.println("\n Error!");
}
}
//
// TODO: Add any method code to meet your needs in the following area
//
//============================= Testing ================================//
//==//
//= The following main method is just for testing this class you built.=//
//= After testing,you may simply delete it.=//
//======================================================================//
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new av();
}
//= End of Testing =
}

