Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaFX中更改舞台背景色_Java_Javafx_Stage - Fatal编程技术网

在JavaFX中更改舞台背景色

在JavaFX中更改舞台背景色,java,javafx,stage,Java,Javafx,Stage,我正在尝试更改舞台背景色,而不是默认的白色。 下图显示了一个包含节点/窗格的场景的简单舞台 我自己制作了一个简单的top实用程序版本,被视为stage中的一个节点,但这在调整大小、拖动窗口等方面造成了更大的困难,因此寻找一个解决方案,让我可以编辑和设置默认stage本身的样式,通过直接引用舞台。要完全控制舞台,显然您必须使用StageStyle.TRANSPARENT实现自己的行为 下面是一个透明阶段的快速演示,挂钩调整大小逻辑并将拖动设置为标题。我希望这能帮助您了解如何满足您的要求 impo

我正在尝试更改舞台背景色,而不是默认的白色。 下图显示了一个包含节点/窗格的场景的简单舞台


我自己制作了一个简单的top实用程序版本,被视为stage中的一个节点,但这在调整大小、拖动窗口等方面造成了更大的困难,因此寻找一个解决方案,让我可以编辑和设置默认stage本身的样式,通过直接引用舞台。

要完全控制舞台,显然您必须使用StageStyle.TRANSPARENT实现自己的行为

下面是一个透明阶段的快速演示,挂钩调整大小逻辑并将拖动设置为标题。我希望这能帮助您了解如何满足您的要求

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 ResizableCustomStage_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 Custom");
        button2.setOnAction(e -> {
            StackPane node = new StackPane(new Label("CUSTOM"));
            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:pink;");

            Stage stg = new Stage();
            stg.initStyle(StageStyle.UNDECORATED);
            stg.setScene(new Scene(root, 300, 300, Color.PINK));
            // EXPLICITLY INCLUDING THE RESIZE LOGIC !!!
            stg.getScene().addEventFilter(MouseEvent.ANY, new ResizeHandler(stg));
            stg.show();
        });

        Button button3 = new Button("Open Transparent");
        button3.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);
        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);
    }
}
导入javafx.application.application;
导入javafx.beans.property.DoubleProperty;
导入javafx.beans.property.SimpleDoubleProperty;
导入javafx.event.EventHandler;
导入javafx.event.EventType;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.Cursor;
导入javafx.scene.Group;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.*;
导入javafx.scene.paint.Color;
导入javafx.stage.stage;
导入javafx.stage.StageStyle;
导入javafx.stage.Window;
public类resizebleCustomStage_演示扩展了应用程序{
@凌驾
public void start(Stage)引发异常{
Button button1=新按钮(“开放式装饰”);
按钮1.设置操作(e->{
StackPane节点=新的StackPane(新标签(“装饰”));
node.setPadding(新插图(15));
node.setStyle(“-fx背景色:#99999;-fx字体大小:16px;”);
StackPane body=新StackPane(新组(节点));
阶段stg=新阶段();
stg.initStyle(舞台风格装饰);
stg.setScene(新场景(主体,300,300,彩色,透明));
stg.show();
});
按钮2=新按钮(“打开自定义”);
按钮2.设置操作(e->{
StackPane节点=新StackPane(新标签(“自定义”));
node.setPadding(新插图(15));
node.setStyle(“-fx背景色:红色;-fx字体大小:16px;”);
StackPane body=新StackPane(新组(节点));
VBox.setVgrow(body,Priority.ALWAYS);
VBox root=新的VBox(buildDragableHeader(),body);
root.setStyle(“-fx边框宽度:1px;-fx边框颜色:红色;-fx背景颜色:粉色;”);
阶段stg=新阶段();
标准初始样式(未装饰的舞台样式);
stg.setScene(新场景(根,300,300,彩色,粉色));
//显式包括调整大小逻辑!!!
stg.getScene().addEventFilter(MouseEvent.ANY,新的ResizeHandler(stg));
stg.show();
});
按钮按钮3=新按钮(“透明打开”);
按钮3.设置操作(e->{
StackPane节点=新的StackPane(新标签(“透明”));
node.setPadding(新插图(15));
node.setStyle(“-fx背景色:绿色;-fx字体大小:16px;”);
StackPane body=新StackPane(新组(节点));
VBox.setVgrow(body,Priority.ALWAYS);
VBox root=新的VBox(buildDragableHeader(),body);
root.setStyle(“-fx边框宽度:1px;-fx边框颜色:绿色;-fx背景颜色:透明;”);
阶段stg=新阶段();
stg.initStyle(StageStyle.TRANSPARENT);
stg.setScene(新场景(根,300,300,彩色,透明));
//显式包括调整大小逻辑!!!
stg.getScene().addEventFilter(MouseEvent.ANY,新的ResizeHandler(stg));
stg.show();
});
VBox根=新的VBox(按钮1、按钮2、按钮3);
根部设置对齐(位置中心);
根起搏(10);
场景sc=新场景(根,400400);
第二阶段(sc);
stage.setTitle(“舞台风格演示”);
stage.show();
}
私有节点buildDragableHeader(){
BorderPane标头=新建BorderPane();
收割台设置最小高度(24);
header.setRight(新的HBox(新按钮(“-”),新按钮(“0”),新按钮(“X”));
StackPane标题=新的StackPane(新标签(“可拖动标题”);
title.setStyle(“-fx字体大小:粗体;”);
标题.设置对齐(位置中心左);
header.setLeft(标题);
header.setStyle(“-fx背景颜色:#999999;-fx边框宽度:0 0 2 0;-fx边框颜色:#555555;”);
setPadding(新的插入(0,0,0,3));
//显式包含拖动逻辑!!!
DoubleProperty x=新的SimpleDoubleProperty();
DoubleProperty y=新的SimpleDoubleProperty();
header.setOnMousePressed(e->{
x、 set(如getSceneX());
y、 set(如getSceneY());
});
header.setonMouseDrawed(e->{
header.getScene().getWindow().setX(例如getScreenX()-x.get());
header.getScene().getWindow().setY(例如getScreenY()-y.get());
});
返回头;
}
/**
*处理给定阶段的大小调整的处理程序。
*/
类ResizeHandler实现EventHandler{
/**
*在舞台边框周围考虑调整大小
*/
私有静态最终双边界=6;
/**
*在调整大小时考虑边框宽度因子
*/
专用静态最终双边框_宽度_系数=6;
私人最终窗口;
/**
*当前光标对场景的引用
*/
private Cursor=Cursor.DEFAULT;
/**
*拖动起点的X位置
*/
专用双星tx=0;
/**
*拖动sta的Y位置