KeyListener, Keyevent, Cursor keys
Hi,
I am new to Java as well as new to Event Handling. I am building a GUI using Netbeans 5 and would like to utilize the four cursor keys in my application. For now I just would like to show a dialog box if one og the cursor keys has been pressed. However, my code doesn't interpret the cursor keys (I just tried it with the cursor key left = VK_LEFT):
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
publicclass Hauptfensterextends javax.swing.JFrame{
/** Creates new form Hauptfenster */
public Hauptfenster(){
initComponents();
}
/** 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 Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
privatevoid initComponents(){
jPanel1 =new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Test");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jPanel1.setBackground(new java.awt.Color(51, 102, 255));
jPanel1.setPreferredSize(new java.awt.Dimension(400, 400));
jPanel1.addKeyListener(new java.awt.event.KeyAdapter(){
publicvoid keyPressed(java.awt.event.KeyEvent evt){
CursorHinauf(evt);
CursorHinunter(evt);
CursorRechts(evt);
CursorLinks(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout =new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 800, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 800, Short.MAX_VALUE)
);
org.jdesktop.layout.GroupLayout layout =new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
privatevoid CursorLinks(java.awt.event.KeyEvent evt){
// TODO add your handling code here:
if(evt.getKeyCode() == KeyEvent.VK_LEFT){
JOptionPane.showMessageDialog(jPanel1,"Left");
}
}
privatevoid CursorRechts(java.awt.event.KeyEvent evt){
// TODO add your handling code here:
}
privatevoid CursorHinunter(java.awt.event.KeyEvent evt){
// TODO add your handling code here:
}
privatevoid CursorHinauf(java.awt.event.KeyEvent evt){
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
publicstaticvoid main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable(){
publicvoid run(){
new Hauptfenster().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
Thanks for your help!

