且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的 JavaFx 应用程序在我的 RaspberryPi 上运行时没有框架?

更新时间:2023-01-26 10:53:24

JavaFX 开发人员 Daniel Blaukopf 在 树莓派论坛:

Raspberry Pi 上的 JavaFX 使用帧缓冲区,而不是 X11 桌面.它占据了整个屏幕.请参阅 a open-jfx wiki page on the树莓派.

在 Raspberry Pi 上具有加速图形的 JavaFX 仅处于全屏模式.可以在窗口中做非加速图形,但性能会很差.Pi 上的 EGL 实现不支持 X11 窗口中的 OpenGL,至少在我检查过的时候是这样.

我还将在这里从 open-jfx wiki 复制链接信息,以防 wiki 页面消失:

Raspberry Pi 上的 JavaFX 接管整个屏幕并捕获所有 Linux 输入设备.虽然这通常是您在部署的应用程序中想要的行为,但它对开发来说不太方便,因为您无法使用 control-C 停止应用程序,除非 JavaFX 应用程序有一个 KeyEvent 处理程序来侦听control-C 并调用 Platform.exit().这并没有什么不寻常之处——许多 Linux 全屏控制台应用程序具有相同的行为——但在开发过程中拥有一种快速结束应用程序而不更改应用程序代码的方法通常很有用.

有两种方法可以运行能够被 control-C 终止的应用程序:

  1. 通过 SSH 连接从 PC 运行应用程序.这提供了对设备的最大控制,因为一旦您设置了 SSH 连接,您也可以将它们用于其他目的.

  2. 或者,您可以使用内置调试功能来捕获 control-C.如果您在启动 Java 之前设置了环境变量 JAVAFX_DEBUG=1,那么当您按下 control-C 时 JavaFX 将退出.例如:

JAVAFX_DEBUG=1/opt/jdk1.8.0/bin/java -cp Stopwatch.jar stopwatch.MainScreen

JAVAFX_DEBUG 环境变量仅用于开发,您不应依赖它来部署您的应用程序.将来,此功能可能会以不同方式指定或被删除.

其他问题的答案

是否有***实践"?允许用户退出以全屏模式运行的 JavaFX 应用程序?我希望我的用户能够启动我的应用,使用它,然后关闭它.

您应该对您的应用程序进行编码,使其包含一个相当明显的 UI 元素,以允许用户关闭应用程序.例如,通过在主应用程序屏幕上放置关闭按钮或提供关闭菜单选项:

Button close = new Button("Close");close.setOnAction(new EventHandler() {@覆盖公共无效句柄(ActionEvent 事件){primaryStage.close();//或 Platform.exit();}});

如果您还希望应用程序响应并退出组合键,例如 Ctrl+C,那么您可以另外包含组合键加速器:

primaryStage.getScene().getAccelerators().put(KeyCombination.keyCombination(CTRL+C"),新的可运行(){@覆盖公共无效运行(){Platform.exit();}});

I installed JDK 8 on my Raspberry Pi and am trying to run a javaFx sample application. My OS is Raspbian. I am using the "DigitalClock" sample application that comes with Netbeans 8. The application launches and runs on the Pi, but when it does it is almost full screen and has this large black border around the main view. Also, there is no frame on the app. No exit or minimize buttons. Frankly, I don't know how to shutdown the app or just run it in a smaller window. To resume using my Pi, I have to unplug it and reboot.

What am I doing wrong here?

A JavaFX developer, Daniel Blaukopf, explained this in a Raspberry Pi forum:

JavaFX on the Raspberry Pi uses the framebuffer, not the X11 desktop. It takes over the whole screen. See a open-jfx wiki page on the Raspberry Pi.

JavaFX with accelerated graphics on the Raspberry Pi is only in full-screen mode. It would be possible to do non-accelerated graphics in a window, but the performance of that would be quite bad. The EGL implementation on the Pi doesn't support OpenGL in an X11 window, at least not least time I checked.

I'm also going to copy the linked information from the open-jfx wiki here in case the wiki page dies:

JavaFX on the Raspberry Pi takes over the whole screen and captures all Linux input devices. While this will generally be the behavior you want in a deployed application, it is less convenient for development because you can't stop an application using control-C unless the JavaFX application has a KeyEvent handler that listens for control-C and calls Platform.exit(). There's nothing unusual about this - many Linux full-screen console applications have the same behavior - but it is often useful to have a quick way to end an application during development without changing the application code.

There are two ways to run applications with the ability to be terminated by control-C:

  1. Run applications over an SSH connection from a PC. This gives most control over the device, because once you have SSH connections set up then you can use them for other purposes as well.

  2. Alternatively, you can use a built-in debugging feature to trap control-C. If you set the environment variable JAVAFX_DEBUG=1 before starting Java then JavaFX will exit when you press control-C. For example:

JAVAFX_DEBUG=1
/opt/jdk1.8.0/bin/java -cp Stopwatch.jar stopwatch.MainScreen

The JAVAFX_DEBUG environment variable is only for use in development and you shouldn't rely on it for deployment of your application. In the future this functionality might be specified differently or be removed.

Answers to additonal questions

Is there a "best practice" for allowing a user to exit a JavaFX application that is running in full screen mode? I would want my users to be able to start my app, use it, and then close it.

You should code your application to include a reasonably obvious UI element that will allow the user to close the application. For example, by placing a close button on your main application screen or providing a close menu option:

Button close = new Button("Close");
close.setOnAction(
    new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            primaryStage.close();
            // or Platform.exit();
        }
    }
);

If you also want the application to respond and exit on a key combination, such as Ctrl+C, then you could additionally include a key combination accelerator:

primaryStage.getScene().getAccelerators().put(
    KeyCombination.keyCombination("CTRL+C"),
    new Runnable() {
        @Override
        public void run() {
            Platform.exit();
        }
    }
);