0

私は3つのパネルを持つJFrameを持っています。App クラスを拡張し、次のように 3 つのパネルを追加します。

JPanel controlButtonsPanel = new GameControlButtons();
        controlButtonsPanel.setPreferredSize(new Dimension(801,60));
        controlButtonsPanel.setBorder(new LineBorder(Color.white, 1));
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.weightx = 2;
        constraints.weighty = 0.3;
        this.add(controlButtonsPanel, constraints);

        JPanel tempP = new JPanel();     
/`/ *** I'm going to create a Jpanel subclass for this, too, I just haven't yet.`
        tempP.setPreferredSize(new Dimension(500,838));
        tempP.setBorder(new LineBorder(Color.white, 2));
        constraints.anchor = GridBagConstraints.NORTHEAST;
        constraints.weightx = 1;
        constraints.weighty = 2;
        this.add(tempP, constraints);

        JPanel graphicsPanel = new RoofRunnerGame("Ken");
        constraints.anchor = GridBagConstraints.SOUTHWEST;
        constraints.weightx = 2;
        constraints.weighty = 1;
        graphicsPanel.setBorder(new LineBorder(Color.white, 1));
        graphicsPanel.setPreferredSize(new Dimension(800,800));
        graphicsPanel.requestFocus();
        this.add(graphicsPanel, constraints);       

JPanel を GameControlButtons クラスと RoofRunnerGame クラス用に拡張しました。前者にマウスリスナーを追加しました。そして、マウス リスナーとキー リスナーを後者に追加しました。

** 問題: マウス リスナーは両方で正常に動作しますが、キー リスナーは私の RoofRunnerGame パネルでリッスンしていないようです。**

オンラインで2つの可能な修正を見つけましたが、最初に質問したかった.

1) 1 つは、RoofRunnerGame サブクラスで requestFocus() を呼び出していました。これの問題は、別のパネルをクリックすると、フォーカスが失われることです。(これは短期的な修正です。)

2)言及されたもう1つのことは、keyBindingsを使用することでした。私は以前にそれらを使用したことがありません。あなたがそれを推奨するなら私はそうしますが、可能であればkeyListenerを使い続けたいと思います.

それで、あなたはどう思いますか?RoofRunnerGame パネルを KEY リッスン全体に維持する方法はありますか?

4

1 に答える 1

1

他のパネルをフォーカス不可にすることもできますが、それらのパネルのすべてのコンポーネントをフォーカス不可にする必要がある場合もあります。

ActionMap を介してキー リスナーを追加する方法については、 this および this の例参照てください。JComponent.WHEN_IN_FOCUSED_WINDOWflag in getInputMap()method を使用すると、パネルがフォーカスされていない場合でも、パネルが入力イベントを受け取ることができるようになります。

于 2010-10-04T04:52:32.910 に答える