方法一:在主窗口上层放置一个大小相同且半透明的Shape
private final Rectangle rectangle = new Rectangle();
//虚化主界面
public void setGray() {
rectangle.setWidth(rootLayout.getWidth());
rectangle.setHeight(rootLayout.getHeight());
rectangle.setFill(Paint.valueOf("#666666"));
rectangle.setOpacity(0.3);
rootLayout.getChildren().add(rectangle);
rectangle.toFront();
}
//恢复主界面
public void setTransparent() {
rootLayout.getChildren().remove(rectangle);
}
完整代码:
public class MainController implements Initializable, PaneShownEventHandler {
private final Rectangle rectangle = new Rectangle();
//虚化主界面
public void setGray() {
rectangle.setWidth(rootLayout.getWidth());
rectangle.setHeight(rootLayout.getHeight());
rectangle.setFill(Paint.valueOf("#666666"));
rectangle.setOpacity(0.3);
rootLayout.getChildren().add(rectangle);
rectangle.toFront();
}
//恢复主界面
public void setTransparent() {
rootLayout.getChildren().remove(rectangle);
}
public void systemSettingClicked() {
String fxml = "/ui/fxml/utils/system-setting.fxml";
showStage(fxml);
}
public void showStage(String fxml) {
Stage stage = new Stage();
Parent root;
try {
FXMLLoader loader = new FXMLLoader(UIUtils.class.getResource(fxml));
if (controllerFactory != null) {
loader.setControllerFactory(controllerFactory);
}
root = loader.load();
Scene newScene = new Scene(root);
stage.setScene(newScene);
//窗口弹出时虚化主窗口
stage.addEventHandler(WindowEvent.WINDOW_SHOWN, event -> setGray());
//窗口关闭时恢复主窗口
stage.addEventHandler(WindowEvent.WINDOW_HIDING, event -> setTransparent());
stage.show();
} catch (IOException e) {
logger.warn(fxml + " file load failed, fxml = " + fxml + ", e = " + e);
e.printStackTrace();
}
}
}
方法二:将主窗口透明化
//fxml的控件,用于获取当前stage
@FXML
private BorderPane rootLayout;
//虚化主界面
public void setGray() {
Stage mainStage = (Stage)rootLayout.getScene().getWindow();
mainStage.setOpacity(0.7);
}
//恢复主界面
public void setTransparent() {
Stage mainStage = (Stage)rootLayout.getScene().getWindow();
mainStage.setOpacity(1);
}
完整代码:
public class MainController implements Initializable, PaneShownEventHandler {
//fxml的控件,用于获取当前stage
@FXML
private BorderPane rootLayout;
//虚化主界面
public void setGray() {
Stage mainStage = (Stage)rootLayout.getScene().getWindow();
mainStage.setOpacity(0.7);
}
//恢复主界面
public void setTransparent() {
Stage mainStage = (Stage)rootLayout.getScene().getWindow();
mainStage.setOpacity(1);
}
public void systemSettingClicked() {
String fxml = "/ui/fxml/utils/system-setting.fxml";
showStage(fxml);
}
public void showStage(String fxml) {
Stage stage = new Stage();
Parent root;
try {
FXMLLoader loader = new FXMLLoader(UIUtils.class.getResource(fxml));
if (controllerFactory != null) {
loader.setControllerFactory(controllerFactory);
}
root = loader.load();
Scene newScene = new Scene(root);
stage.setScene(newScene);
//窗口弹出时虚化主窗口
stage.addEventHandler(WindowEvent.WINDOW_SHOWN, event -> setGray());
//窗口关闭时恢复主窗口
stage.addEventHandler(WindowEvent.WINDOW_HIDING, event -> setTransparent());
stage.show();
} catch (IOException e) {
logger.warn(fxml + " file load failed, fxml = " + fxml + ", e = " + e);
e.printStackTrace();
}
}
}