2

我想在 JFrame 中集成 slick。但我面临以下问题: 1.当我尝试关闭框架时,它会停止流畅的游戏而不是框架。

请找到我的代码如下:

CanvasGameContainer canvasGameContainer1 = new CanvasGameContainer(new MainGame("Game 1"));
canvasGameContainer1.setBounds(20, 20, 400, 400);
newFrame = new JFrame("With JFrame");
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
newFrame.add(canvasGameContainer1);
newFrame.setVisible(true);
canvasGameContainer1.start();

我在光滑的论坛上发布了相同的内容,但没有解决方案。这是相同的链接...

4

2 回答 2

1

我知道这是一篇旧帖子,但如果有些人仍在为框架无法关闭而苦苦挣扎,这里有一个适用于 Windows 的解决方案:

  • 将你的框架设置defaultCloseOperationDISPOSE_ON_CLOSE
  • WindowClosing你的框架的情况下,处置GameContainer
  • 如果WindowClosed你的框架,调用System.exit(0)
于 2015-12-18T15:44:36.077 回答
0

I don't know why you need to wrap it in a JFrame. Are you also adding menus to the app (instead of rendering them inside the game container, for example)? I guess my question is, why are you wrapping it in a JFrame to start with - maybe there's a specific use case I'm not thinking of? Can you post a screenshot by chance?

I think mixing the windowing kits might be problematic. I just create an instance of an AppGameContainer, and set the fullscreen option to false. As a result, Slick just gives me a window with a close button. When I click the close button, the app closes normally.

I've only officially tested this on Mac OS X 10.6, with JDK 6, but theoretically it should work elsewhere.

Here's the file and code where I initialize my container:

https://github.com/normalocity/pedestrians/blob/master/src/StartPedestrians.java

AppGameContainer container = new AppGameContainer(pedSim);
container.setDisplayMode(800, 600, false);
于 2012-02-23T14:59:10.437 回答