제목과 마찬가지로 : JFrame에서 JPanel을 바꿀 때 포커스가 사라집니다. 게임, 액션, 버튼 클래스가 있습니다. 게임 스테이지를 그릴 때 클래스 스테이지도 있습니다.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());
}
}
각 구성 요소마다'setVisible (true/false) '가 표시되는 이유는 무엇입니까? 수동으로 추가/제거하는 대신 [__CardLayout__] (http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)을 사용하는 것이 어떻습니까? 이 '스레드'는 무엇입니까? 이 '스테이지'클래스는 어디에 있습니까? 문제의 상기 'SHIP'을 제어하기 위해'KeyListener'를 사용하고 있습니까? –
이 스레드는 애니메이션 용이며 Stage Stage는 Runnable을 구현합니다. Stage 클래스에 추가 된 키 바인딩을 사용하고 있습니다. CardLayout은 "새로운 게임", "종료"와 같은 버튼이 포함 된 메뉴 만 수행하기 때문에 최상의 솔루션이 아닙니다. 밀린 후 "새로운 게임"마누가 사라지고 무대가 나타납니다. 무대는 게임에서 모든 것이 그려지는 보드와 같습니다. – user3191398
1) 한 공간에 여러 구성 요소가있는 경우이 [짧은 설명에 나오는 ['CardLayout'] (http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html)을 사용하십시오 예] (http://stackoverflow.com/a/5786005/418556). 2) 더 빨리 도움을 받으려면 [MCVE] (http://stackoverflow.com/help/mcve) (최소 완료 및 검증 가능한 예)를 게시하십시오. –