public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SendToBack();
this.Text = string.Empty;
this.ControlBox = false;
this.FormBorderStyle = FormBorderStyle.None;
//this.BackColor = Color.Transparent;
}
}
,然后将其大小调整为场景大小。primaryStage.setMaximized(true);
。虽然它可以保持舞台最大化,但是在每次改变场景之后,对先前聚焦的gui控制的关注都会丢失(在切换之后,仅关注场景层次结构中的第一个gui控件)。我真的需要场景,所以任何专注的gui控制仍然会在舞台改变场景后集中注意力。primaryStage.getScene().setRoot(<the root node of scene>)
答案 0 :(得分:3)
经过几次尝试后,我终于想出了如何轻松解决这个问题,以保持最大化的屏幕,同时保留每个场景上的聚焦节点。希望它能帮助社区:根据显示器的屏幕尺寸创建场景
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class NewFXMain extends Application {
@Override
public void start(Stage primaryStage) {
// get screensize of monitor
Rectangle2D screenSize = Screen.getPrimary().getVisualBounds();
// init buttons
Button btn1 = new Button("switch to next scene >>");
Button btn2 = new Button("<< switch to previous scene");
// first rootNode
StackPane root1 = new StackPane();
root1.getChildren().add(btn1);
Scene scene1 = new Scene(root1, screenSize.getWidth(), screenSize.getHeight());
// second rootNode
StackPane root2 = new StackPane();
root2.getChildren().add(btn2);
Scene scene2 = new Scene(root2, screenSize.getWidth(), screenSize.getHeight());
// button actions
btn1.setOnAction((ActionEvent event) -> {
primaryStage.setScene(scene2);
});
btn2.setOnAction((ActionEvent event) -> {
primaryStage.setScene(scene1);
});
primaryStage.setMaximized(true); // keep this since otherwise the titlebar is bit overlapped
primaryStage.setScene(scene1);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 1 :(得分:0)
我认为这是一个错误。您可以在http://bugreport.java.com报告。
与此同时,作为一种解决方法,您可能只需要为共享场景设置窗格,替换其内容而不是替换场景。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class NewFXMainFixed extends Application {
@Override
public void start(Stage primaryStage) {
// init buttons
Button btn1 = new Button("switch to next scene >>");
Button btn2 = new Button("<< switch to previous scene");
// first scene
StackPane root1 = new StackPane();
root1.getChildren().add(btn1);
// second scene
StackPane root2 = new StackPane();
root2.getChildren().add(btn2);
// button actions
btn1.setOnAction((ActionEvent event) ->
primaryStage.getScene().setRoot(root2)
);
btn2.setOnAction((ActionEvent event) ->
primaryStage.getScene().setRoot(root1)
);
Scene scene = new Scene(root1);
primaryStage.setMaximized(true);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}