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

时间:2014-06-16 17:58:00

标签: java swing jframe jpanel jbutton

在主题中:当我在JFrame中更改JPanel时,我会失去焦点。我有课程游戏,动作,按钮。 在绘制游戏阶段时我也有阶段阶段。

首先在游戏中我有动作面板,其中包含按钮,在按钮NewGame后我在游戏中更改了面板,但我无法控制船只,我正在飞行。

如何修复它或如何做到与众不同?

Action.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

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

@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());
    }

}

1 个答案:

答案 0 :(得分:2)

  但是我无法控制船只,我正在飞行。

KeyEvents仅传递给具有焦点的组件。当您交换面板时,面板不再具有焦点。

不要使用KeyListener。相反,您应该使用Key Bindings。然后,即使组件没有焦点,您也可以处理该事件。

有关更多信息和工作示例,请参阅Motion Using the Keyboard

编辑:

  1. 不要使用" =="用于字符串比较。使用String.equals(...)方法。

  2. 变量名称不应以大写字母开头。