java - 如何在不影响标题栏的情况下设置 JavaFX 窗口/舞台的不透明度?
问题描述
我已经发现我可以在 JavaFX 中设置舞台的不透明度。但是当我这样做时,带有调整大小/关闭按钮的标题栏也会受到影响。但我只希望窗口的“主体”是透明的,这样我就可以通过窗口看到我的桌面/其他应用程序。有人知道在 JavaFX 中实现这一点的方法吗?
解决方案
我猜你是在要求一个选项,为 DECORATED 窗口的内容部分设置透明背景,同时保持其他部分(标题栏、按钮、边框)不变。默认 StageStyle(DECORATED)无法做到这一点。
要自定义您的舞台/窗口,您必须使用 StageStyle.UNDECORATED 或 StageStyle.TRANSPARENT。对于您的用例,您需要使用 TRANSPARENT(因为 UNDECORATED 将有一个没有平台装饰的默认背景)。然后用样式模仿标题栏,使其看起来像你想要的样子。
当然,使用 TRANSPARENT 将需要大量的样板代码来拖动和调整窗口大小。但这一切都是定制舞台的一部分。我相信您已经从@Matt 提供的链接中获得了大部分内容。
供您参考,下面是不同舞台风格的快速演示,以及如何将拖动和调整大小功能设置为未装饰/透明舞台。
注意:这是一个粗略的代码/逻辑。根据您的要求对其进行优化。
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
public class StageStyles_Demo extends Application {
@Override
public void start(Stage stage) throws Exception {
Button button1 = new Button("Open Decorated");
button1.setOnAction(e -> {
StackPane node = new StackPane(new Label("DECORATED"));
node.setPadding(new Insets(15));
node.setStyle("-fx-background-color:#999999;-fx-font-size:16px;");
StackPane body = new StackPane(new Group(node));
Stage stg = new Stage();
stg.initStyle(StageStyle.DECORATED);
stg.setScene(new Scene(body, 300, 300, Color.TRANSPARENT));
stg.show();
});
Button button2 = new Button("Open Utility");
button2.setOnAction(e -> {
StackPane node = new StackPane(new Label("UTILITY"));
node.setPadding(new Insets(15));
node.setStyle("-fx-background-color:#999999;-fx-font-size:16px;");
StackPane body = new StackPane(new Group(node));
Stage stg = new Stage();
stg.initStyle(StageStyle.UTILITY);
stg.setScene(new Scene(body, 300, 300, Color.TRANSPARENT));
stg.show();
});
Button button3 = new Button("Open Undecorated");
button3.setOnAction(e -> {
StackPane node = new StackPane(new Label("UNDECORATED"));
node.setPadding(new Insets(15));
node.setStyle("-fx-background-color:red;-fx-font-size:16px;");
StackPane body = new StackPane(new Group(node));
VBox.setVgrow(body, Priority.ALWAYS);
VBox root = new VBox(buildDraggableHeader(), body);
root.setStyle("-fx-border-width:1px;-fx-border-color:red;-fx-background-color:transparent;");
Stage stg = new Stage();
stg.initStyle(StageStyle.UNDECORATED);
stg.setScene(new Scene(root, 300, 300, Color.TRANSPARENT));
// EXPLICITLY INCLUDING THE RESIZE LOGIC !!!
stg.getScene().addEventFilter(MouseEvent.ANY, new ResizeHandler(stg));
stg.show();
});
Button button4 = new Button("Open Transparent");
button4.setOnAction(e -> {
StackPane node = new StackPane(new Label("TRANSPARENT"));
node.setPadding(new Insets(15));
node.setStyle("-fx-background-color:green;-fx-font-size:16px;");
StackPane body = new StackPane(new Group(node));
VBox.setVgrow(body, Priority.ALWAYS);
VBox root = new VBox(buildDraggableHeader(), body);
root.setStyle("-fx-border-width:1px;-fx-border-color:green;-fx-background-color:transparent;");
Stage stg = new Stage();
stg.initStyle(StageStyle.TRANSPARENT);
stg.setScene(new Scene(root, 300, 300, Color.TRANSPARENT));
// EXPLICITLY INCLUDING THE RESIZE LOGIC !!!
stg.getScene().addEventFilter(MouseEvent.ANY, new ResizeHandler(stg));
stg.show();
});
VBox root = new VBox(button1, button2, button3, button4);
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
Scene sc = new Scene(root, 400, 400);
stage.setScene(sc);
stage.setTitle("Stage Styles Demo");
stage.show();
}
private Node buildDraggableHeader() {
BorderPane header = new BorderPane();
header.setMinHeight(24);
header.setRight(new HBox(new Button("-"), new Button("0"), new Button("X")));
StackPane title = new StackPane(new Label("Draggable Header"));
title.setStyle("-fx-font-weight:bold;");
title.setAlignment(Pos.CENTER_LEFT);
header.setLeft(title);
header.setStyle("-fx-background-color:#999999;-fx-border-width: 0 0 2 0;-fx-border-color:#555555;");
header.setPadding(new Insets(0, 0, 0, 3));
// EXPLICITLY INCLUDING THE DRAG LOGIC !!!
DoubleProperty x = new SimpleDoubleProperty();
DoubleProperty y = new SimpleDoubleProperty();
header.setOnMousePressed(e -> {
x.set(e.getSceneX());
y.set(e.getSceneY());
});
header.setOnMouseDragged(e -> {
header.getScene().getWindow().setX(e.getScreenX() - x.get());
header.getScene().getWindow().setY(e.getScreenY() - y.get());
});
return header;
}
/**
* Handler to process the resizing of the the given stage.
*/
class ResizeHandler implements EventHandler<MouseEvent> {
/**
* Space to consider around the stage border for resizing
*/
private static final double BORDER = 6;
/**
* Space to consider the border width factor while resizing
*/
private static final double BORDER_WIDTH_FACTOR = 6;
private final Window window;
/**
* Current cursor reference to the scene
*/
private Cursor cursor = Cursor.DEFAULT;
/**
* X position of the drag start
*/
private double startX = 0;
/**
* Y position of the drag start
*/
private double startY = 0;
ResizeHandler(final Window window) {
this.window = window;
}
@Override
public void handle(final MouseEvent event) {
final EventType<? extends MouseEvent> eventType = event.getEventType();
final Scene scene = window.getScene();
final double mouseEventX = event.getSceneX();
final double mouseEventY = event.getSceneY();
final double sceneWidth = scene.getWidth();
final double sceneHeight = scene.getHeight();
final boolean isResizable = true;
if (isResizable) {
if (MouseEvent.MOUSE_MOVED.equals(eventType)) {
assignCursor(scene, event, mouseEventX, mouseEventY, sceneWidth, sceneHeight);
} else if (MouseEvent.MOUSE_PRESSED.equals(eventType)) {
startX = window.getWidth() - mouseEventX;
startY = window.getHeight() - mouseEventY;
consumeEventIfNotDefaultCursor(event);
} else if (MouseEvent.MOUSE_DRAGGED.equals(eventType) && !Cursor.DEFAULT.equals(cursor)) {
consumeEventIfNotDefaultCursor(event);
if (!Cursor.W_RESIZE.equals(cursor) && !Cursor.E_RESIZE.equals(cursor)) {
handleHeightResize(event);
}
if (!Cursor.N_RESIZE.equals(cursor) && !Cursor.S_RESIZE.equals(cursor)) {
handleWidthResize(event);
}
}
}
}
private void assignCursor(final Scene scene, final MouseEvent event, final double mouseEventX,
final double mouseEventY, final double sceneWidth, final double sceneHeight) {
final Cursor cursor1;
if (mouseEventX < BORDER && mouseEventY < BORDER) {
cursor1 = Cursor.NW_RESIZE;
} else if (mouseEventX < BORDER && mouseEventY > sceneHeight - BORDER) {
cursor1 = Cursor.SW_RESIZE;
} else if (mouseEventX > sceneWidth - BORDER
&& mouseEventY < BORDER) {
cursor1 = Cursor.NE_RESIZE;
} else if (mouseEventX > sceneWidth - BORDER && mouseEventY > sceneHeight - BORDER) {
cursor1 = Cursor.SE_RESIZE;
} else if (mouseEventX < BORDER) {
cursor1 = Cursor.W_RESIZE;
} else if (mouseEventX > sceneWidth - BORDER) {
cursor1 = Cursor.E_RESIZE;
} else if (mouseEventY < BORDER) {
cursor1 = Cursor.N_RESIZE;
} else if (mouseEventY > sceneHeight - BORDER) {
cursor1 = Cursor.S_RESIZE;
} else {
cursor1 = Cursor.DEFAULT;
}
cursor = cursor1;
scene.setCursor(cursor);
}
/**
* Consumes the mouse event if the cursor is not the DEFAULT cursor.
*
* @param event MouseEvent instance
*/
private void consumeEventIfNotDefaultCursor(final MouseEvent event) {
if (!cursor.equals(Cursor.DEFAULT)) {
event.consume();
}
}
/**
* Processes the vertical drag movement and resizes the window height.
*
* @param event MouseEvent instance
*/
private void handleHeightResize(final MouseEvent event) {
final double mouseEventY = event.getSceneY();
final double minHeight = 30;
if (Cursor.NW_RESIZE.equals(cursor)
|| Cursor.N_RESIZE.equals(cursor)
|| Cursor.NE_RESIZE.equals(cursor)) {
if (window.getHeight() > minHeight || mouseEventY < 0) {
final double newHeight = window.getY() - event.getScreenY() + window.getHeight();
window.setHeight(max(newHeight, minHeight));
window.setY(event.getScreenY());
}
} else if (window.getHeight() > minHeight || mouseEventY + startY - window.getHeight() > 0) {
final double newHeight = mouseEventY + startY;
window.setHeight(max(newHeight, minHeight));
}
}
/**
* Processes the horizontal drag movement and resizes the window width.
*
* @param event MouseEvent instance
*/
private void handleWidthResize(final MouseEvent event) {
final double mouseEventX = event.getSceneX();
final double minWidth = 100;
if (Cursor.NW_RESIZE.equals(cursor)
|| Cursor.W_RESIZE.equals(cursor)
|| Cursor.SW_RESIZE.equals(cursor)) {
if (window.getWidth() > minWidth || mouseEventX < 0) {
final double newWidth = window.getX() - event.getScreenX() + window.getWidth();
window.setWidth(max(newWidth, minWidth));
window.setX(event.getScreenX());
}
} else if (window.getWidth() > minWidth || mouseEventX + startX - window.getWidth() > 0) {
final double newWidth = mouseEventX + startX;
window.setWidth(max(newWidth, minWidth));
}
}
/**
* Determines the max value among the provided two values.
*
* @param value1 First value
* @param value2 Second value
* @return Maximum value of the given two values.
*/
private double max(final double value1, final double value2) {
return value1 > value2 ? value1 : value2;
}
}
public static void main(String... a) {
Application.launch(a);
}
}
推荐阅读
- javascript - 旋转后,我的 div 移动位置并且永远不会返回
- javascript - 带有来自数据库的数量的Javascript条形图未显示在php网页中
- php - 异步 file_put_contents/file_get_contents?
- multithreading - 无法使用 keras 腌制 _thread.RLock 对象
- javascript - 如何向 FeathersJS 套接字连接添加参数
- highcharts - 当所有值都为 0 时隐藏堆积条上的列
- keycloak - 我们如何从本地 Keycloak Distribution 文件夹创建 docker 映像?
- javascript - 如何在 Cordova 中使用 Javascript 打开多个 URL
- linux - Linux 中的任务调度程序和 CPU 隔离
- swift - 为什么完成不起作用?完成问题