内容简介:由于去掉了平台自带了标题栏,窗口就无法移动和缩放了,需要我们自己来实现。首先确定在Controller中获取到你的跟布局对象,如下:然后可以在
JavaFx中Controller获取Stage并自定义窗口移动与缩放逻辑
由于去掉了平台自带了标题栏,窗口就无法移动和缩放了,需要我们自己来实现。
去除窗口标题栏
public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); primaryStage.setTitle("main"); // 主要是这一句 primaryStage.initStyle(StageStyle.TRANSPARENT); primaryStage.setScene(new Scene(root)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
在Controller中获取Stage
首先确定在Controller中获取到你的跟布局对象,如下:
public class Controller implements Initializable { public BorderPane root; private Stage stage; }
<BorderPane fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@../css/main.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="line.main.Controller"> <!--其他布局元素--> </BorderPane>
然后可以在 public void initialize(URL url, ResourceBundle resourceBundle)
方法内获取Stage
public class Controller implements Initializable { public BorderPane root; private Stage stage; @Override public void initialize(URL url, ResourceBundle resourceBundle) { var stage = getStage(); } private Stage getStage() { if (stage == null) { stage = (Stage) root.getScene().getWindow(); } return stage; } }
自定义窗体移动与缩放逻辑
-
记录鼠标坐标
root.setOnMousePressed(event -> { event.consume(); xOffset = event.getSceneX(); yOffset = event.getSceneY(); });
-
自定义移动控制区域
dragArea.setOnMouseDragged(event -> { event.consume(); var stage = getStage(); stage.setX(event.getScreenX() - xOffset); stage.setY(event.getScreenY() - yOffset); });
-
判断鼠标坐标位置,并处理光标变换
root.setOnMouseMoved(this::mouseMoveHandle); private void mouseMoveHandle(MouseEvent event) { event.consume(); double x = event.getSceneX(); double y = event.getSceneY(); var stage = getStage(); double width = stage.getWidth(); double height = stage.getHeight(); Cursor cursorType = Cursor.DEFAULT; bit = 0; if (y >= height - RESIZE_WIDTH) { if (x <= RESIZE_WIDTH) { bit |= 1 << 3; } else if (x >= width - RESIZE_WIDTH) { bit |= 1; bit |= 1 << 2; cursorType = Cursor.SE_RESIZE; } else { bit |= 1; cursorType = Cursor.S_RESIZE; } } else if (x >= width - RESIZE_WIDTH) { bit |= 1 << 2; cursorType = Cursor.E_RESIZE; } root.setCursor(cursorType); }
-
处理窗口缩放
root.setOnMouseDragged(this::mouseDraggedHandle); private void mouseDraggedHandle(MouseEvent event) { event.consume(); var primaryStage = getStage(); double x = event.getSceneX(); double y = event.getSceneY(); double nextX = primaryStage.getX(); double nextY = primaryStage.getY(); double nextWidth = primaryStage.getWidth(); double nextHeight = primaryStage.getHeight(); if ((bit & 1 << 2) != 0) { nextWidth = x; } if ((bit & 1) != 0) { nextHeight = y; } if (nextWidth <= MIN_WIDTH) { nextWidth = MIN_WIDTH; } if (nextHeight <= MIN_HEIGHT) { nextHeight = MIN_HEIGHT; } primaryStage.setX(nextX); primaryStage.setY(nextY); primaryStage.setWidth(nextWidth); primaryStage.setHeight(nextHeight); }
完整代码
package line.main; import javafx.fxml.Initializable; import javafx.scene.Cursor; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; import java.net.URL; import java.util.ResourceBundle; public class Controller implements Initializable { public BorderPane root; public Pane dragArea; private Stage stage; private double xOffset = 0; private double yOffset = 0; private int bit = 0;//left,right,top,bottom private static final double RESIZE_WIDTH = 5.00; private static final double MIN_WIDTH = 600.00; private static final double MIN_HEIGHT = 400.00; @Override public void initialize(URL url, ResourceBundle resourceBundle) { dragArea.setOnMouseDragged(event -> { event.consume(); var stage = getStage(); stage.setX(event.getScreenX() - xOffset); stage.setY(event.getScreenY() - yOffset); }); root.setOnMousePressed(event -> { event.consume(); xOffset = event.getSceneX(); yOffset = event.getSceneY(); }); root.setOnMouseMoved(this::mouseMoveHandle); root.setOnMouseDragged(this::mouseDraggedHandle); } private Stage getStage() { if (stage == null) { stage = (Stage) root.getScene().getWindow(); } return stage; } private void mouseMoveHandle(MouseEvent event) { event.consume(); double x = event.getSceneX(); double y = event.getSceneY(); var stage = getStage(); double width = stage.getWidth(); double height = stage.getHeight(); Cursor cursorType = Cursor.DEFAULT; bit = 0; if (y >= height - RESIZE_WIDTH) { if (x <= RESIZE_WIDTH) { bit |= 1 << 3; } else if (x >= width - RESIZE_WIDTH) { bit |= 1; bit |= 1 << 2; cursorType = Cursor.SE_RESIZE; } else { bit |= 1; cursorType = Cursor.S_RESIZE; } } else if (x >= width - RESIZE_WIDTH) { bit |= 1 << 2; cursorType = Cursor.E_RESIZE; } root.setCursor(cursorType); } private void mouseDraggedHandle(MouseEvent event) { event.consume(); var primaryStage = getStage(); double x = event.getSceneX(); double y = event.getSceneY(); double nextX = primaryStage.getX(); double nextY = primaryStage.getY(); double nextWidth = primaryStage.getWidth(); double nextHeight = primaryStage.getHeight(); if ((bit & 1 << 2) != 0) { nextWidth = x; } if ((bit & 1) != 0) { nextHeight = y; } if (nextWidth <= MIN_WIDTH) { nextWidth = MIN_WIDTH; } if (nextHeight <= MIN_HEIGHT) { nextHeight = MIN_HEIGHT; } primaryStage.setX(nextX); primaryStage.setY(nextY); primaryStage.setWidth(nextWidth); primaryStage.setHeight(nextHeight); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- [译] 为什么要做特征缩放,怎么做特征缩放,什么时候做特征缩放?特征缩放三连了解一下
- golang使用nfnt缩放图片
- 特征工程之特征缩放 & 特征编码
- [译] 你必知的 Kubernetes 自动缩放
- JavaScript DOM元素长宽等比例缩放
- 计算MKMapView的zoomLevel(地图缩放等级)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python金融大数据分析
[德] 伊夫·希尔皮斯科 / 姚军 / 人民邮电出版社 / 2015-12 / CNY 99.00
唯一一本详细讲解使用Python分析处理金融大数据的专业图书;金融应用开发领域从业人员必读。 Python凭借其简单、易读、可扩展性以及拥有巨大而活跃的科学计算社区,在需要分析、处理大量数据的金融行业得到了广泛而迅速的应用,并且成为该行业开发核心应用的首选编程语言。《Python金融大数据分析》提供了使用Python进行数据分析,以及开发相关应用程序的技巧和工具。 《Python金融大......一起来看看 《Python金融大数据分析》 这本书的介绍吧!