gpt4 book ai didi

java - 如何在 JFrame 之间切换时保持 KeyListener 处于 Activity 状态

转载 作者:行者123 更新时间:2023-12-02 06:57:28 24 4
gpt4 key购买 nike

我制作了一个 pacman 游戏,我想显示一个 JFrame 作为游戏本身的引导加载。因此,我创建了一个类 launcher.java 扩展了 JFrame ,其中包含一个 JPanel 来呈现游戏的图像。

在另一个名为 Game.java 的类中,该类也扩展了 JFrame,我设置了一个计时器并使用 setVisible(false) 在 GUI 中构建游戏。

激活游戏时,启动器显示三秒钟,然后触发计时器并运行游戏,尽管按键监听器没有响应。

请帮我理解为什么。

启动器类

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;
}

}

最佳答案

我建议不要使用 KeyListener。它有一个特殊的问题,即它注册的组件不仅必须是可聚焦的,而且必须在触发关键事件之前获得焦点。

相反,我建议使用 Key Bindings API

我还建议您在关闭启动屏幕时在 actionListener 内的游戏框架中调用 requetsFocusInWindow

关于java - 如何在 JFrame 之间切换时保持 KeyListener 处于 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17127862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com