【问题标题】:How to switch between JPanels in an applet?如何在小程序中的 JPanel 之间切换?
【发布时间】:2014-08-04 19:11:25
【问题描述】:

如何在小程序中的 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。

【问题讨论】:

    标签: java swing applet jpanel japplet


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      相关资源
      最近更新 更多