我正在用NetBeans中的Java编程一个Enigma Machine模拟器,而我想要做的是当用户按下一个键时,Keyboard类上的JLabel的相应图标会变为另一个,但KeyListener事件却没有工作,我在很多网站上进行了调查,发现这是一个焦点问题,但是我尝试了所有方法,但仍然无法正常工作,我有三个类:KeyBoard(JPanel),Machine(JPanel)和EnigmaMain(JFrame),并且keylistener在键盘类。这是代码:
键盘:
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Keyboard extends javax.swing.JPanel {
/**
* Creates new form Keyboard
*/
public Keyboard() {
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
keyA = new javax.swing.JLabel();
setMinimumSize(new java.awt.Dimension(688, 253));
setOpaque(false);
setPreferredSize(new java.awt.Dimension(688, 253));
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
});
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
keyA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/keys/keyAreleased.png"))); // NOI18N
add(keyA, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 70, -1, -1));
}// </editor-fold>
private void formKeyPressed(java.awt.event.KeyEvent evt) {
ImageIcon image = new ImageIcon(getClass().getResource("/resources/keys/keyApressed.png"));
keyA.setIcon(image);
}
public JLabel getKeyA(){
return this.keyA;
}
// Variables declaration - do not modify
private javax.swing.JLabel keyA;
// End of variables declaration
}
机器:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
public class Machine extends javax.swing.JPanel{
/**
* Creates new form Machine
*/
public Machine() {
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
keyboard1 = new GUI.Keyboard();
backgroundImage = new javax.swing.JLabel();
setPreferredSize(new java.awt.Dimension(688, 800));
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
add(keyboard1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 540, -1, -1));
backgroundImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/enigmaMachine.png"))); // NOI18N
add(backgroundImage, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 690, -1));
}// </editor-fold>
public Keyboard getKeyBoard(){
return this.keyboard1;
}
// Variables declaration - do not modify
private javax.swing.JLabel backgroundImage;
private GUI.Keyboard keyboard1;
// End of variables declaration
}
EnigmaMain:
import javax.swing.ImageIcon;
公共类EnigmaMain扩展了javax.swing.JFrame {
/**
* Creates new form EnigmaMain
*/
public EnigmaMain() {
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
machine1 = new GUI.Machine();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setFocusableWindowState(false);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
getContentPane().add(machine1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(EnigmaMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EnigmaMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EnigmaMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EnigmaMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
EnigmaMain window = new EnigmaMain();
window.setVisible(true);
}
});
}
// Variables declaration - do not modify
private GUI.Machine machine1;
// End of variables declaration
}
答案 0 :(得分:0)
你能解释为什么吗
setFocusableWindowState(false);
对于您要执行的操作是必需的吗?
您是否尝试过不使用它?