私は2つのクラスを持っています。
1 つはキャンバスを拡張し、内部で jframe を作成し、その jframe にキャンバスを追加し、キー イベントを受け取る別のキーアダプター クラスを追加します。コードをテストするためのメイン関数もあります。メインから実行すると、フォームが表示され、重要なイベントも受け取ります。
ここで、jframe を拡張し、keylistener を実装してこの形式でイベントを受信する別のクラスを作成します。
2 番目のクラスで機能が完了したら、2 番目のフォームを閉じて最初のフォームを表示します。2 番目のクラスのキー イベント関数から表示すると、1 番目のクラスのキー リスナーが機能しません。
私のコードをちらっと見て、私の問題を修正する方法を教えてください。あなたの時間と貴重な提案をありがとう.
クラス1
public class Test extends Canvas {
private JFrame container;
public Test() {
container = new JFrame("Space Invaders");
JPanel panel = (JPanel) container.getContentPane();
panel.setPreferredSize(new Dimension(screenSize.width, screenSize.height));
panel.setLayout(null);
setBounds(0, 0, screenSize.width, screenSize.height);
panel.add(this);
container.pack();
container.setResizable(false);
container.setVisible(true);
try {
addKeyListener(new KeyInputHandler(this));
} catch (Exception e) {
e.printStackTrace();
}
requestFocus();
}
private class KeyInputHandler extends KeyAdapter {
public void keyPressed(KeyEvent e) {
//Some Action
}
public void keyReleased(KeyEvent e) {
//Some Action
}
public void keyTyped(KeyEvent e) {
//Some Action
}
}
public static void main(String args[]){
//Running this canvas here works perfectly with all added keylisteners
}
}
クラス2
public class Sample extends JFrame implements KeyListener {
public Sample() {
init();
this.setSize(100, 100);
this.setVisible(true);
Sample.this.dispose();
// Created a window here and doing some operation and finally redirecting
// to the previous test window. Even now the test window works perfectly
// with all keylisteners
new Test();
}
public static void main(String[] args) {
new Sample();
}
private void init() {
addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
removeKeyListener(this);
Sample.this.dispose();
// But when calling the previous Test window here, the window
// gets displayed but the keylistener is not added to the
// window. No keys are detected in test window.
new Test();
}
@Override
public void keyReleased(KeyEvent e) {
}
}