考虑一下:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.layout.FlowPaneBuilder;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageBuilder;
public class DualMonitorProblem extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(final Stage stage) throws Exception {
Label label = LabelBuilder.create().text("I seem to be lost").build();
Scene dlgScene = SceneBuilder.create().root(label).build();
final Stage dlgStage = StageBuilder.create().scene(dlgScene).resizable(false).build();
dlgStage.initModality(Modality.APPLICATION_MODAL);
dlgStage.initOwner(stage);
Button btn = ButtonBuilder.create().text("put me on secondary monitor before clicking me").build();
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dlgStage.showAndWait();
}
});
Button btn2 = ButtonBuilder.create().text("me too").build();
btn2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dlgStage.setX(stage.getX() + stage.getWidth() / 2);
dlgStage.setY(stage.getY() + stage.getHeight() / 2);
dlgStage.showAndWait();
}
});
Button btn3 = ButtonBuilder.create().text("me too").build();
btn3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dlgStage.centerOnScreen();
dlgStage.showAndWait();
}
});
Pane p = FlowPaneBuilder.create().children(btn, btn2, btn3).build();
Scene scene = SceneBuilder.create().root(p).width(800).height(600).build();
stage.setScene(scene);
stage.show();
}
}
将舞台拖到辅助监视器,然后单击其中一个按钮。当您什么都不做时,模式对话框将在主监视器上打开。在父阶段的中心打开模态对话框的最简单方法是什么?Stage.centerOnScreen() 在考虑双显示器设置时并不相同。我应该认为这是一个错误并将其归档吗?还是有另一种方法可以实现这一目标?