【发布时间】:2019-01-04 02:52:01
【问题描述】:
我正在用netbeans中的Java编写一个Enigma Machine模拟器,我想做的是当用户按下一个键时,键盘类上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
}
【问题讨论】:
-
与所有与
KeyListener问题相关的问题一样,请改用Key bindings API - 任何其他解决方案都是不可靠的黑客攻击 -
我在许多网站和 stackoverflow 中搜索过,但没有人指出我的问题,但谢谢。
标签: java swing keylistener