请查看代码段:
import java.io. *;
* import java.sql.;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
javafx.animation import *.;
import javafx.animation.PathTransition.OrientationType;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.animation.TranslateTransitionBuilder;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathBuilder;
import javafx.scene.shape.Rectangle;
import javafx.stage.Modality;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
public class Book 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 ("book");
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
/ / Can not find symbol (stage)
}
如果我不使用FXML,那么它可以工作(:-D):
import java.io. *;
* import java.sql.;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
javafx.animation import *.;
import javafx.animation.PathTransition.OrientationType;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.animation.TranslateTransitionBuilder;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathBuilder;
import javafx.scene.shape.Rectangle;
import javafx.stage.Modality;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
public class Book 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 ("book");
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);
}
});
}
此解决方案的原因是该按钮的事件处理程序现在位于start方法中。
在start方法中使用的@ FXML注释不起作用(当然):
public class Book 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 ("book");
stage.setScene (scene);
. stage.getIcons () add (new Image ("icon.png"));
stage.show ();
* /
@ FXML
public void fullscreen (ActionEvent event)
{
stage.setFullScreen (true) / / Does not work
/ / Can not find symbol (stage)
}
/ * / / Will not work
}
我的问题是:我如何在任何地方使用舞台变量? 还是有其他解决方案吗? JavaFX很酷,但遗憾的是没有一个好的IDE(使用GUI设计器)。 您可以忘记并使用FXML的JavaFX Scene Builder很傻! :-( 但是,我不知道JavaFX Scene Builder的替代方法。 没有创建GUI设计器的GUI对于大型程序来说是愚蠢的: - (。
请帮帮我。
在互联网上,我的问题没有答案,这令我感到惊讶。 (既不是德语也不是英语教程)
抱歉我的英语不好。 我学习语言:-)。 而且我也是Java初学者: - )。
我希望我没有忘记任何事情:-D。
谢谢: - )
答案 0 :(得分:2)
在类中声明一个静态Stage变量,并将primaryStage分配给它。
public class Book extends Application implements Initializable
{
public static Stage stage;
@ Override
public void start (Stage primaryStage) throws IOException
{
stage = primaryStage;
...
}
...
@ FXML
public void fullscreen (ActionEvent event)
{
stage.setFullScreen(true);
}
}