【问题标题】:LwjglCanvas with JFrame resizingLwjglCanvas 与 JFrame 调整大小
【发布时间】:2016-03-06 19:08:00
【问题描述】:

我有一个桌面 LibGDX 项目,并且我的主 (DesktopLauncher) 类扩展了 JFrame(它将是未修饰且不可调整大小的)。我在 LwjglCanvas 中渲染 LibGDX OpenGL 画布,该画布作为组件添加到容器中。现在这是一个奇怪的问题:有时我可以看到画布周围有 3-4-5px 的边框,这会导致一些可疑的调整大小。

这是我的课:

    public DesktopLauncher() {

    this.setUndecorated(true);
    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.setTitle("Evil Engine (v. Lambda)");

    Container container = this.getContentPane();
    this.canvas = new LwjglCanvas(new Main(DesktopLauncher.inst));

    // this.canvas.getCanvas().setLocation(200, -200);

    // height of the task bar
    int taskBarSize = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration()).bottom;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    this.canvas.getCanvas().setSize(screenSize.width, screenSize.height - taskBarSize);

    container.add(this.canvas.getCanvas(), BorderLayout.CENTER);

    this.pack();

    this.setVisible(true);
}

当 setUndecorated 为 true 时可以看出问题。我试图移动画布(setLocation() 行),我看到它周围有一个边框:

作为一个未装饰的窗口:

我尝试了很多东西,但没有任何效果:

  • 我更改了布局类型
  • 我创建了一个新的 JPanel,将画布添加到其中并设置为 setContentPane()
  • 试过setPreferedSize、setMaximumSize、setMinimumSize
  • 搜索 ...setBorder() 或 ..setResizable() 方法没有成功

我怎样才能摆脱画布周围的可怕边框?

【问题讨论】:

  • 删除 jframe 扩展并在启动之前将其放在您的代码中 System.setProperty("org.lwjgl.opengl.Window.undecorated", "true");

标签: java swing opengl libgdx jframe


【解决方案1】:

你的 DesktopLauncher 应该是这样的:

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.height =1024;
        config.width= 768;
        config.resizable = false;
        config.title= "My App Title";
        System.setProperty("org.lwjgl.opengl.Window.undecorated", "true"); 
        new LwjglApplication(new Game(), config);
    }
}

所以现在你有一个无边框、不可调整大小的游戏窗口。

对于您的情况,如果您仍想将其与 swing 一起使用,请改为:

public DesktopLauncher() {

    this.setUndecorated(true);
    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.setTitle("Evil Engine (v. Lambda)");

    Container container = this.getContentPane();
    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    config.resizable = false;
    System.setProperty("org.lwjgl.opengl.Window.undecorated", "true"); 
    this.canvas = new LwjglCanvas(new Main(DesktopLauncher.inst),config);

    // this.canvas.getCanvas().setLocation(200, -200);

    // height of the task bar
    int taskBarSize = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration()).bottom;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    this.canvas.getCanvas().setSize(screenSize.width, screenSize.height - taskBarSize);

    container.add(this.canvas.getCanvas(), BorderLayout.CENTER);

    this.pack();

    this.setVisible(true);
}

【讨论】:

  • 酷,但它不会做这项工作 - 这将禁用 Java Swing,我需要它(我需要使用一些 Swing 功能)......
  • 它仍然适用于您的情况,只需创建配置并将其设置在 this.canvas = new LwjglCanvas(new Main(DesktopLauncher.inst),config);和之前设置 System.setProperty("org.lwjgl.opengl.Window.undecorated", "true");它应该像一个魅力。
猜你喜欢
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
相关资源
最近更新 更多