JFrame
パックマン ゲームを作成しましたが、ゲーム自体のブートロードとしてを表示したいと考えていました。そこで私は、ゲームのイメージを表示JFrame
する単一のクラスを含む launcher.java extends を作成しました。JPanel
Iv'e を拡張する Game.java と呼ばれる別のクラスでJFrame
、タイマーを設定し、GUI でゲームをビルドしsetVisible(false)
ます。
ゲームを起動すると、ランチャーが 3 秒間表示された後、タイマーがトリガーされてゲームが実行されますが、キーリスナーは応答しません。
その理由を理解するのを手伝ってください。
ランチャークラス
package PacMan;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Launcher extends JFrame {
private static final long serialVersionUID = -7949539876450449197L;
private JPanel launchImage;
public Launcher() {
launchImage = new JPanel();
launchImage.add(new JLabel(new ImageIcon("main.png")));
launchImage.setVisible(true);
setUndecorated(true);
setSize(560,620);
setLocationRelativeTo(null);
getContentPane().add(launchImage);
setVisible(true);
}
}
package PacMan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Game extends JFrame implements MouseListener, KeyListener, ActionListener{
private static final long serialVersionUID = -7132848196338970451L;
int posX=0,posY=0;
private Controller upper;
private JLabel[] lives;
public static Timer timer;
private int livesCounter, lifeIndex;
private JPanel livesPanel;
private double secondsSaver;
private Launcher launcher;
public Game() {
timer = new Timer(50,this);
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new BorderLayout());
setUndecorated(true);
setSize(560,620);
setLocationRelativeTo(null);
this.upper = new Controller();
this.addKeyListener(upper);
upper.setBorder(BorderFactory.createEmptyBorder(-5, -5, -5, -5));
upper.setBounds(-5, -5, -5, -5);
JPanel lower = new JPanel();
lower.setBackground(Color.BLACK);
lower.setLayout(new GridLayout(1,4));
lower.setSize(560, 100);
JLabel livesLabel = new JLabel(new ImageIcon("lives.png"));
livesLabel.setHorizontalTextPosition(new Integer(0));
lower.add(livesLabel);
livesPanel = new JPanel();
livesPanel.setLayout(new GridLayout(1,3));
lives = new JLabel[3];
for(int i=0; i<3; i++){
lives[i] = new JLabel(new ImageIcon("life.png"));
livesPanel.add(lives[i]);
}
livesPanel.setOpaque(false);
lower.add(livesPanel);
lifeIndex = 2;
livesCounter=3;
JButton restart = new JButton(new ImageIcon("Restart.png"));
restart.setBackground(Color.black);
restart.setBorderPainted(false);
lower.add(restart);
restart.addActionListener(new ActionListener() { // action for the quit button
public void actionPerformed(ActionEvent arg0) {
getContentPane().remove(upper);
upper = new Controller();
}
});
JButton quit = new JButton(new ImageIcon("quit.png"));
quit.setBackground(Color.black);
quit.setBorderPainted(false);
lower.add(quit);
quit.addActionListener(new ActionListener() { // action for the quit button
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
upper.getComponentListeners();
gamePanel.add(upper, BorderLayout.NORTH);
gamePanel.add(lower, BorderLayout.SOUTH);
getContentPane().add(gamePanel);
pack();
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
posX=e.getX();
posY=e.getY();
}
});
this.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent evt) {
//sets frame position when mouse dragged
setLocation (evt.getXOnScreen()-posX,evt.getYOnScreen()-posY);
}
});
timer.start();
launcher = new Launcher();
}
public Controller getController(){
return upper;
}
public static void main(String args[]) {
new Game();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (secondsSaver==3) {
this.setVisible(true);
launcher.setVisible(false);
}
if ((livesCounter>upper.getLives()) && (secondsSaver>=3)) {
lives[lifeIndex].setVisible(false);
lifeIndex--;
}
this.livesCounter=upper.getLives();
secondsSaver+=0.05;
}
}