【发布时间】:2015-04-08 19:15:40
【问题描述】:
问题:我有一个 GDX 桌面应用程序,我想在 JFrame 中运行,这样我就可以生成其他 JFrame 为其父级,这样当我关闭主游戏时,其他窗口也会关闭。
尝试的解决方案:我在 JFrame 应用程序中运行 GDX 应用程序,然后获取其画布并将其添加到内容窗格。
以前的代码,运行良好(跨平台):
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = 800; config.height = 600;
new LwjglApplication(new GDXRoot(), config);
}
}
新的基于 JFrame 的方法(到目前为止,我只让它在 Windows 8 上运行。它不适用于 OS X、Windows 7 甚至 Windows 10 技术预览版):
public class EditModeLauncher extends JFrame {
final LwjglCanvas canvas;
LwjglApplicationConfiguration config;
public EditModeLauncher() {
config = new LwjglApplicationConfiguration();
config.width = 800; config.height = 600;
config.resizable = false;
canvas = new LwjglCanvas(new GDXRoot(), config);
canvas.getCanvas().setSize(800, 600);
getContentPane().setPreferredSize(new Dimension(800,600));
final JFrame test = new TestUI(this);
test.setLocation(800,300);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Runtime.getRuntime().halt(0); // because of deadlocks with shut down
}
});
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
getContentPane().add(canvas.getCanvas());
setVisible(true);
canvas.getCanvas().requestFocus();
}
});
test.setVisible(true);
}
private class TestUI extends JFrame {
private TestUI(final EditModeLauncher parent) {
// code omitted ...
}
}
public static void main (String[] arg) {
new EditModeLauncher();
}
}
任何想法可能是什么问题?
【问题讨论】: