- 我开发了一个javafx应用程序。
- 在我的应用程序中有两个场景和一个阶段。
- 在应用程序中,两个场景的高度和宽度相同或不变。
- 因此,根据我的研究,场景的高度和宽度保持不变,这在构造函数中提到,但场景根据舞台的高度和宽度进行调整。
- 当我在舞台的高度和宽度不同于场景的恒定高度和宽度时,随着舞台的场景调整,我的午餐应用。
但是在运行时我应用第二场景时场景并不随舞台的高度和宽度而调整,场景的高度和宽度保持不变。在javafx中设置舞台和场景的高度和宽度
那么任何解决方案?
6
A
回答
4
据我了解上面发布的问题。我认为这个阶段足够好,可以根据侦听器设置首选高度和宽度,以获得应用于窗口大小的较新请求。但它有一些限制,如果您最大化或最小化javaFX屏幕并尝试导航到其他屏幕,则其他屏幕将具有相同的窗口大小,但场景内容将失真为其默认的高度和宽度,例如进行登录和javafx中的主场景(所有场景都使用fxml进行渲染)。 Login.fxml由其控制器初始化。正如你所提到的,场景是在构造函数中初始化的,所以它必须是相关fxml的控制器(截至目前FXML与控制器紧密结合)。您将在构造函数中设置场景大小(高度为&宽度)。
1)的LoginController为login.fxml
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
class LoginController {
private Stage stage;
private Scene scene;
private Parent parent;
@FXML
private Button gotoHomeButton;
public LoginController() throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/login.fxml"));
fxmlLoader.setController(this);
try {
parent = (Parent) fxmlLoader.load();
// set height and width here for this login scene
scene = new Scene(parent, 1000, 800);
} catch (IOException ex) {
System.out.println("Error displaying login window");
throw new RuntimeException(ex);
}
}
// create a launcher method for this. Here I am going to take like below--
public void launchLoginScene(Stage stage) {
this.stage = stage;
stage.setScene(scene);
stage.setResizable(true);
stage.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
setCurrentWidthToStage(number2);
}
});
stage.heightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
setCurrentHeightToStage(number2);
}
});
//Don't forget to add below code in every controller
stage.hide();
stage.show();
}
@FXML
public void authenticateUser(ActionEvent actionEvent) {
// write your logic to authenticate user
//
new HomeController().displayHomeScreen(stage);
}
private void setCurrentWidthToStage(Number number2) {
stage.setWidth((double) number2);
}
private void setCurrentHeightToStage(Number number2) {
stage.setHeight((double) number2);
}
}
2)同为HomeController的 -
public class HomeController {
private Parent parent;
private Stage stage;
private Scene scene;
public HomeController(){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/home.fxml"));
fxmlLoader.setController(this);
try {
parent = (Parent) fxmlLoader.load();
// set height and width here for this home scene
scene = new Scene(parent, 1000, 800);
} catch (IOException e) {
// manage the exception
}
}
public void displayHomeScreen(Stage stage){
this.stage = stage;
stage.setScene(scene);
// Must write
stage.hide()
stage.show();
}
}
3)主类
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
new LoginController().launchLoginScene(primaryStage);
}
public static void main(String[] args) {
launch(args);
}
}
只是试图把每个控制器中的Stage.show()之前的Stage.hide()。我希望这会帮助你。
6
这是因为舞台调整其大小到现场,除非明确指示diferently ...
到这里是一个解决方案:
stage.setScene(scene2);
stage.setHeight(1000);
stage.setWidth(1000);
和示例应用程序:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Test extends Application {
@Override
public void start(final Stage stage) throws Exception {
AnchorPane anchor1 = new AnchorPane();
final Scene scene1 = new Scene(anchor1, 250, 250);
Button boton1 = new Button();
anchor1.getChildren().add(boton1);
AnchorPane anchor2 = new AnchorPane();
final Scene scene2 = new Scene(anchor2, 500, 500);
Button boton2 = new Button();
anchor2.getChildren().add(boton2);
boton2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
stage.setScene(scene1);
stage.setHeight(1000);
stage.setWidth(1000);
}
});
boton1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
stage.setScene(scene2);
stage.setHeight(1000);
stage.setWidth(1000);
}
});
stage.setScene(scene1);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
1
我知道这是一个老话题,但是这个(bug?)仍然存在于Java7u75中。
我遇到了同样的问题...上述由shambhu提供的解决方案的工作,但结果在可见的“窗口切换”的效果:
stage.hide();
stage.show();
我发现下面的代码解决了这个问题(获取阶段“刷新”)没有任何明显的影响:
final boolean resizable = stage.isResizable();
stage.setResizable(!resizable);
stage.setResizable(resizable);
相关问题
- 1. 设置场景的宽度和高度
- 2. 设置上传的舞台图像的宽度和高度
- 3. 设置舞台以允许任何舞台宽度或高度
- 4. JavaFX StageScene调用后的舞台宽度/高度
- 5. Flash Pro cc为什么我手机上的舞台宽度和高度设置为Flash Pro内的舞台宽度和高度?
- 6. flex 4 swfloader:SWFLoader.content.stage.width和height返回主舞台宽度和高度
- 7. 如何设置javafx TextArea的高度和宽度?
- 8. JavaFX中的隐形舞台场景
- 9. 调整JavaFX图的高度和宽度
- 10. 闪光舞台/根部宽度和高度始终为100?
- 11. 设置链接的高度和宽度
- 12. tableGrob:设置grid.table的高度和宽度
- 13. 设置图像的宽度和高度
- 14. 设置UIWebView的宽度和高度
- 15. 设置视频的宽度和高度
- 16. 设置高度和宽度的边距
- 17. jQuert断头台插件 - 在哪里设置高度和宽度
- 18. addchild设置宽度和高度
- 19. 将高度和宽度设置为img
- 20. 设置宽度和高度jsp页面
- 21. 设置rChart宽度和高度
- 22. UIPopoverPresentationController宽度和高度没有设置
- 23. 将图像的高度和宽度设置为WPF背景
- 24. JavaFX - 每个舞台的独特场景
- 25. UITextView在高度设置宽度和高度ForRowAtIndexPath
- 26. 设置背景是全宽度和高度
- 27. 如何设置背景大小(高度和宽度)?
- 28. 如何使用JavaFX和Scene Builder修复或设置GridPane行高和列宽的标签高度和宽度?
- 29. JavaFX传递舞台/切换场景
- 30. 如何在这里设置ImageView和TextView的宽度和高度?
该阶段需要刷新,但如何? – Anvay