私が作成したグラフィカルGUIパネル(JPanel)のボタンをクリックすると、キーボードのコントロールが機能しなくなります。以下は私のメインクラスのコードです。キークラスターコードをコントローラークラスに入れました。私はキーボードのフォーカスを設定しようとしましたが、動作しません。 JPanelのクラスコードも追加しました。私はそれを虚偽に焦点を当ててみましたが、それはまだ動作していません。誰かがなぜフォーカスがボタンからシフトしていないのか説明してください。JPanelのボタンをクリックすると、キーボードコントロールが機能しなくなりますか?
メインクラス
Graphical gui = new Graphical(view, getPlayer());
frame.add(gui, BorderLayout.SOUTH);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
// Get keyboard focus.
frame.requestFocus();
view.addMouseListener(new MouseHandler(view));
controller = new Controller(world.getPlayer());
frame.addKeyListener(controller);
のJPanelクラス
public class Graphical extends javax.swing.JPanel {
private UserView view;
private Snowman snowman;
private Game game;
public Graphical(UserView view, Snowman snowman) {
this.view = view;
this.snowman = snowman;
initComponents();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("You are now playing as a snowman!");
snowman.changeCharacter1();
jButton1.setFocusable(false);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("You are now playing as a jellyfish!");
snowman.changeCharacter2();
jButton2.setFocusable(false);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new Game();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
// End of variables declaration
}