提问者:小点点

JavaFX 下全屏


我已经问过这个问题了,但是没有人回答。

如何通过按下按钮在JavaFX中切换到全屏模式?但是按钮是用FXML(JavaFX场景生成器)创建的。(找不到符号(阶段))如果按钮是手动创建的,则它可以工作。

public class Buch extends Application implements Initializable
{

@ Override
    public void start (Stage primaryStage) throws IOException
    {

        Stage stage = primaryStage;

        Parent root = FXMLLoader.load (getClass () getResource ("Buch.fxml").);

        Scene scene = new Scene (root);

        stage.setTitle ("Buch");
        stage.setScene (scene);
        stage.getIcons () add (new Image ("Icon.png"));
        / / stage.setFullScreen (true) / / Works
        stage.show ();



    }

    @ FXML
    public void fullscreen (ActionEvent event)
    {

        / / stage.setFullScreen (true) / / Does not work
        / / cannot find symbol (stage)

    }

作品:

public class Buch extends Application implements Initializable
{

@ Override
    public void start (Stage primaryStage) throws IOException
    {

        Stage stage = primaryStage;

        Parent root = FXMLLoader.load (getClass () getResource ("Buch.fxml").);

        Scene scene = new Scene (root);

        stage.setTitle ("Buch");
        stage.setScene (scene);
        stage.getIcons () add (new Image ("Icon.png"));
        stage.show ();

        btn.setOnAction (new EventHandler <ActionEvent> ()
        {
            public void handle (ActionEvent evt)
            {
                stage.setFullScreen (true);
            }

        });

    }

不起作用(当然?):

public class Buch extends Application implements Initializable
{

@ Override
    public void start (Stage primaryStage) throws IOException
    {

        Stage stage = primaryStage;

        Parent root = FXMLLoader.load (getClass () getResource ("Buch.fxml").);

        Scene scene = new Scene (root);

        stage.setTitle ("Buch");
        stage.setScene (scene);
        stage.getIcons () add (new Image ("Icon.png"));
        stage.show ();

* /
       @ FXML
    public void fullscreen (ActionEvent event)
    {

        stage.setFullScreen (true) / / Does not work
        / / cannot find symbol (stage)

    }
/ * / / Will not work


    }

你也可以看看这个链接。这个问题在这里更详细:

stackoverflow.com/questions/22820049/全屏幕-在-javafx-with-fxml-does-not-work下

如何在任何地方使用阶段变量?还是有其他解决方案?请帮助我。

在互联网上,我的问题没有答案!?!

我是Java初学者。:-)

谢谢你的帮助


共2个答案

匿名用户

为什么您的应用程序也是您的控制器?这似乎不起作用,全屏功能或没有全屏功能。

在控制器中,只需注入按钮(或任何节点,但按钮将是显而易见的一个),并在事件处理程序中调用getScene()getWindow()

public class MyController {
    @FXML
    private Button fullScreenButton ;

    @FXML
    private void fullScreen(ActionEvent event) {
       Stage stage = (Stage) fullScreenButton.getScene().getWindow();
       stage.setFullScreen(true);
    }
}

匿名用户

我不是JavaFX专家,但这在纯Java中也不起作用。DI容器注入不知道stage的方法fullscreen(event),该方法在start()中声明。

你有没有尝试将其移动到类成员?公共类Buch{私人舞台舞台;…}