gpt4 book ai didi

java - 如何在小程序中的 JPanel 之间切换?

转载 作者:行者123 更新时间:2023-11-30 08:21:34 25 4
gpt4 key购买 nike

如何在小程序中的 JPanel 之间切换?在 JFrame 中的 JPanel 之间切换非常容易,我是这样做的:

        frame.remove(mainMenu); //remove the old jpanel
frame.add(game); //add the new jpanel
game.createImage(); //runs createVolatileImage(width, height)
//in the other class (JPanel)
frame.revalidate();
frame.repaint();
requestFocus();

但是,如何在小程序中复制它?当我在小程序中尝试此操作时,当我尝试对我在新 JPanel 中创建的图像运行 image.getGraphics() 时,我得到了一个 nullPointerException。

最佳答案

  1. 切换 JPanel 的最佳方法是使用 CardLayout ( tutorial )。如果操作正确,无论您的 GUI 是 JFrame、JDialog、JApplet 等,这都将起作用。
  2. 您的 NullPointerException 可能与您的交换 View 无关。要对此进行调试,您需要更多信息。
  3. 这可能是因为您的图像变量为空,可能是因为试图将其作为文件而不是资源读入。

关于java - 如何在小程序中的 JPanel 之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25125848/

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