zoukankan      html  css  js  c++  java
  • JavaFX 之窗口拖动(三)

    一、问题场景

      在上一篇中,我们将窗口的默认标题栏隐藏从而导致鼠标点击窗体无法进行拖动。

    二、解决思路

      给组件添加鼠标按下事件监听器和鼠标拖动事件监听器。

    三、代码实现

    /**
     * 程序入口
     * @author Light
     */
    public class JavaFXTest extends Application {
        
        @Override
        public void start(Stage stage) {
            
            stage.initStyle(StageStyle.TRANSPARENT);
            
            VBox root = new VBox();
            root.setId("root");
            // 引入样式
            root.getStylesheets().add(JavaFXTest.class.getResource("/resources/style.css").toString());
            
            //顶部
            VBox top = new VBox();
            top.setId("top");
            top.setPrefSize(300,26);
            // 标题栏
            AnchorPane title = new AnchorPane();
            Label close = new Label();
            close.setPrefWidth(33);
            close.setPrefHeight(26);
            close.setId("winClose");//winClose css样式Id
            title.getChildren().add(close);
            AnchorPane.setRightAnchor(close, 0.0);
            AnchorPane.setTopAnchor(close, 5.0);
            top.getChildren().add(title);
            
            // 内容
            VBox content = new VBox();
            content.setPrefWidth(300);
            content.setMinHeight(200);
            // 组装
            root.getChildren().addAll(top, content);
            Scene scene = new Scene(root);        
            stage.setScene(scene);
            // 拖动监听器
            DragUtil.addDragListener(stage, top);
            // 显示
            stage.show();
        }
        
        
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
        
    }
    /**
     * 工具类
     * @author Light
     */
    public class DragUtil {
         public static void addDragListener(Stage stage,Node root) {
            new DragListener(stage).enableDrag(root);
        }
    }
    /**
     * 拖拽监听器
     * @author Light
     */
    public class DragListener implements EventHandler<MouseEvent> {
    
        private double xOffset = 0;
        private double yOffset = 0;
        private final Stage stage;
    
        public DragListener(Stage stage) {
            this.stage = stage;
        }
    
        @Override
        public void handle(MouseEvent event) {
            event.consume();
            if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            } else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                stage.setX(event.getScreenX() - xOffset);
                if(event.getScreenY() - yOffset < 0) {
                    stage.setY(0);
                }else {
                    stage.setY(event.getScreenY() - yOffset);
                }
            }
        }
    
        public void enableDrag(Node node) {
            node.setOnMousePressed(this);
            node.setOnMouseDragged(this);
        }
    }

    效果演示图:

  • 相关阅读:
    HDU 2767 Proving Equivalences(强连通缩点)
    HDU 3836 Equivalent Sets(强连通缩点)
    POJ 2762 Going from u to v or from v to u?(强连通+缩点+拓扑排序)
    织梦dedecms中自定义表单必填项的设置方法
    dedecms中调用隐藏栏目的方法
    去掉dedecms友情链接中的LI标签的方法
    Mysql修改端口号 织梦DedeCMS设置教程
    织梦DedeCMS文章标题自动增加长尾关键词的方法
    DEDECMS短标题标签调用与字数修改的方法
    dedecms批量替换文章中超链接的方法
  • 原文地址:https://www.cnblogs.com/moonlightL/p/5978978.html
Copyright © 2011-2022 走看看