gpt4 book ai didi

Java切换jpanels和新面板没有响应

转载 作者:行者123 更新时间:2023-12-02 05:47:32 25 4
gpt4 key购买 nike

我使用启动 JPanel 的 JFrame 来启动游戏。当 JPanel 准备就绪时,该 JPanel 将转到下一个 JPanel。我可以切换到新的 JPanel,但它不响应我的按键。第二个 JPanel 以前工作正常,所以我认为问题在于它们之间的切换。 (我删除了一些不相关的方法)

public class GameRunner extends JFrame
{
public GameRunner()
{
super("Scrolling Shooter");

Toolkit tk = Toolkit.getDefaultToolkit();
int width = ((int) tk.getScreenSize().getWidth());
int height = ((int) tk.getScreenSize().getHeight());
setSize(width, height);

TitleScreen title = new TitleScreen(this);

((Component)title).setFocusable(true);
getContentPane().add(title);

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String args[])
{new GameRunner();}
}

public class TitleScreen extends JPanel implements KeyListener, Runnable
{
private JFrame frame; //the JFrame
private ImageItem background; //the background
private String name = ""; //the player's name

/**
* Constructs a title screen
* @param f the JFrame
*/
public TitleScreen (JFrame f)
{
frame = f;

ImageItem fix = new ImageItem();
fix.fixMainPath(); //fixes the main path for all the ImageItems
background = new ImageItem(0, 0, frame.getWidth(), frame.getHeight(), "splashscreen.png"); //makes background stretch between the top and bottom walls

setVisible(true);
addKeyListener(this);
new Thread(this).start();
}

/**
* Draws the stuff on the screen
* @param g some Graphics object
*/
protected void paintComponent(Graphics g)
{
background.draw(g);
if (!(name.equals("")))
{
g.setFont(g.getFont().deriveFont(80f));
g.drawString(name, frame.getWidth() / 3, frame.getHeight() - 60);
}
}

/**
* When a key is typed it is added to the name
* @param key the key typed
*/
public void keyTyped(KeyEvent key)
{
if (!(key.getKeyCode() == KeyEvent.VK_ENTER))
name += key.getKeyChar();
}

/**
* Enter makes moves to the game if the name is ok
* @param key the key pressed
*/
public void keyPressed(KeyEvent key)
{
if (key.getKeyCode() == KeyEvent.VK_ENTER)
{
boolean isValid = false;

if (name.length() > 20)
JOptionPane.showMessageDialog(frame, "That name is too long. Try again.");

if (!name.equals(""))
{
try {if (!hasBannedWord(name)) isValid = true;} //breaks the loop if there are no banned words in the name
catch (FileNotFoundException e) {e.printStackTrace();}
}

if (!isValid)
{
JOptionPane.showMessageDialog(frame, "Bad name. Try again.");
name = "";
}

if (isValid)
{
ScrollingShooter game = new ScrollingShooter(frame, name);
((Component)game).setFocusable(true);
frame.getContentPane().removeAll();
frame.getContentPane().add(game);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}

public void run()
{
try
{
while(true)
{
Thread.currentThread().sleep(0);
repaint();
}
}
catch(Exception e){e.printStackTrace();}
}

最佳答案

使用the key bindings API它代替 KeyListener,可以更好地控制生成按键事件所需的焦点级别

为了让 KeyListener 生成 KeyEvent,它所注册的组件必须是可聚焦的并且具有焦点。没有“简单”的方法来保证组件可以获得/捕获焦点。

您还应该考虑使用 CardLayout在屏幕之间移动,这正是它的设计目的,将使生活变得更加轻松

您还应该调用 super.paintComponent 以确保组件/Graphics 上下文已准备好进行绘制

关于Java切换jpanels和新面板没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23902449/

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