简体   繁体   English

当我在JFrame中更改JPanel时我失去了焦点

[英]When i change JPanel in JFrame i lose focus

As in subject: When i change JPanel in JFrame i lose focus. 如主题所示:当我在JFrame中更改JPanel时,我会失去焦点。 I have class Game, Action, Button. 我上课有游戏,动作,按钮。 I have also class Stage when drawing the game stage. 在绘制游戏舞台时,我也上了舞台。

At first in Game i have Action panel, which contains buttons, after push button NewGame i change panel in Game to Stage, but i cant control ship, which i am flying. 首先,在游戏中,我有“操作”面板,其中包含按钮;在按下NewGame按钮之后,我将“游戏”中的面板更改为舞台,但我无法控制飞船。

How to fix it or how to do it different? 如何解决它或如何做不同?

Action.java 动作.java

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.FileNotFoundException;

import javax.swing.*;


@SuppressWarnings("serial")
public class Action extends JPanel {
    public static final int xxx = 800;
    public static final int yyy = 600;
    private Button buttonPanel;

    public Action(Game game) {
        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(xxx, yyy));

        buttonPanel = new Button(game);
        add(buttonPanel);

        setVisible(true);
    }


}

Button.java Button.java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Button extends JPanel implements ActionListener{

    public static final int xxx = 100;
    public static final int yyy = 300;

    private JButton NewGame;
    private JButton Scores;
    private JButton Exit;

    private Game game;


    public Button(Game game) {
        NewGame = new JButton("NewGame");
        Scores = new JButton("Scores");
        Exit = new JButton("Wyjście");
        this.game=game;

        NewGame.addActionListener(this);


        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(xxx, yyy));
        add(NewGame);
        add(Scores);
        add(Exit);
        setVisible(true);             
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if(source == NewGame){
            game.setPanel("stage");
        }
       ;
    }
}

Game.java Game.java

@SuppressWarnings("serial")
public class Game extends JFrame {
    private Action menu;
    private static Stage stage = new Stage();
    public Game() {
        super("Lunar Lander");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(Stage.WIDTH,Stage.HEIGHT);
        setMinimumSize(new Dimension(400,300));
        this.setResizable(true);

        //stage = new Stage(0);
        menu = new Action(this);
        add(menu);
        pack();
        //setPanel("stage");
        setVisible(true);   
    }
    public void setPanel(String panel) {
        if (panel=="stage") {
            remove(menu);
            add(stage);
            pack();
        } else if (panel=="menu") {
            remove(stage);
            add(menu);
            pack();
        }
    }
    public static void main(String[] args) throws FileNotFoundException {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Game();
            }
        });
        Thread a = new Thread(stage);
        a.start();
        while (a.isAlive()) {}
        Scores scores = new Scores();
        scores.changeScores(stage.getPlayer().getName(),stage.getPlayer().getPoints());
    }

}

but i cant control ship, which i am flying. 但是我不能控制正在飞的船。

KeyEvents are only passed to the component with focus. KeyEvent仅传递给具有焦点的组件。 When you swap panels the panel no longer has focus. 交换面板时,面板不再具有焦点。

Don't use a KeyListener. 不要使用KeyListener。 Instead you should be using Key Bindings . 相反,您应该使用Key Bindings Then you can handle the event even if the component doesn't have focus. 这样,即使组件没有焦点,您也可以处理事件。

See Motion Using the Keyboard for more information and working examples. 有关更多信息和工作示例,请参见使用键盘运动

Edit: 编辑:

  1. Don't use "==" for String comparisons. 请勿使用“ ==”进行字符串比较。 Use the String.equals(...) method. 使用String.equals(...)方法。

  2. Variable names should NOT start with an upper case character. 变量名称不应以大写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM