使用FXML在JavaFX下全屏显示

use*_*276 3 javafx javafx-2

我已经问了这个问题,但没有回答.

如何通过按下按钮在JavaFX中切换到全屏模式?但该按钮是使用FXML(JavaFX Scene Builder)创建的.(找不到符号(舞台))如果手动创建按钮,则可以正常工作.

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)

    }
Run Code Online (Sandbox Code Playgroud)

作品:

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);
            }

        });

    }
Run Code Online (Sandbox Code Playgroud)

不起作用(当然?):

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


    }
Run Code Online (Sandbox Code Playgroud)

您还可以查看此链接.这个问题在这里更详细:

stackoverflow.com/questions/22820049/full-screen-under-javafx-with-fxml-does-not-work

我怎样才能在任何地方使用舞台变量?还是有其他解决方案吗?请帮我.

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

我是Java初学者.:-)

谢谢你的帮助

Jam*_*s_D 7

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

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

public class MyController {
    @FXML
    private Button fullScreenButton ;

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