gpt4 book ai didi

javafx - JavaFX 下的全屏与 FXML

转载 作者:行者123 更新时间:2023-12-01 09:26:28 24 4
gpt4 key购买 nike

我已经问过这个问题,但没有得到回答。

如何在 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)

}

作品:

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/full-screen-under-javafx-with-fxml-does-not-work

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

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

我是 Java 初学者。 :-)

感谢您的帮助

最佳答案

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

Controller 中,只需注入(inject)按钮(或任何节点,但按钮是显而易见的),然后调用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 - JavaFX 下的全屏与 FXML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22860204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com