拖动未修整的舞台时,如何强制光标在窗口上保持原位 [英] How can i force cursor to stay in place on window when dragging an Undecorated Stage

查看:133
本文介绍了拖动未修整的舞台时,如何强制光标在窗口上保持原位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用未装饰的舞台制作gui计算器。我添加了一个Hbox作为标题栏,并设置了一个onClicked / OnDragged方法,以便在拖动时移动主要舞台,但它似乎并不完美。因为

I'm trying to create gui calculator with an undecorated stage. I added an Hbox as the title bar and set gave it an onClicked/OnDragged methods to move the primary stage around when dragged, however it doesn't seem to work perfectly. because

1)当我按下并开始拖动时,鼠标光标移动到窗口的左上角,如下所示。我使用的方法来自这里

1) When i press and start dragging, the mouse cursor moves to the top left corner of the window as you can see below. The method i used is from here

X 图片:

当我点击Hbox中间

当我开始拖动时光标移动的位置

X 这是我的主要课程

public void start(Stage primaryStage) throws Exception {


    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));


    Scene mainSCENE = new Scene(root);
    mainSCENE.getStylesheets().add(this.getClass().getResource("calculator.css").toExternalForm());
    mainSCENE.setFill(Color.TRANSPARENT);

    primaryStage.initStyle(StageStyle.TRANSPARENT);
    primaryStage.setResizable(false);
    primaryStage.setScene(mainSCENE);


    mainWindow = primaryStage;

    primaryStage.show();
}

X 以下是我用过的方法我的Controller类添加可拖动效果

X and here are the methods i used in my Controller class to add the draggable effect

public class Controller {

@FXML
Circle btnCLOSE;

@FXML
Circle btnMINIMIZE;

@FXML
HBox hboxTitleBar;
private double xOffset = 0;
private double yOffset = 0;


public void handle(MouseEvent event) throws IOException, LineUnavailableException, UnsupportedAudioFileException {

    // Plays click audio when buttons are clicked
    AudioInputStream audioIn = AudioSystem.getAudioInputStream(getClass().getResource("click.wav"));
    Clip clip = AudioSystem.getClip();
    clip.open(audioIn);
    clip.start();

    // Add functionality to minimize/close buttons
    btnCLOSE.addEventHandler(MouseEvent.MOUSE_CLICKED, event1 -> System.exit(0));
    btnMINIMIZE.addEventHandler(MouseEvent.MOUSE_CLICKED, event1 -> Main.mainWindow.setIconified(true));
}


// Makes the UNDECORATED window draggable from the title bar Hbox
public void setOnClicked(MouseEvent event) {

    System.out.println("CLICKED");
    xOffset = Main.mainWindow.getX() - event.getScreenX();
    yOffset = Main.mainWindow.getY() - event.getScreenY();
}

public void setOnDragged(MouseEvent event) {

    Main.mainWindow.setX(event.getScreenX() + xOffset);
    Main.mainWindow.setY(event.getScreenY() + yOffset);
}
}

我如何将光标锁定到位拖动时?

2)当我点击HBox内的关闭/最小化按钮时,它也会拖动窗口。有没有办法防止这种情况?

2) When i click the close/minimize buttons that are inside the HBox, it also drags the window. Is there a way to prevent that?

推荐答案

为了方便整个窗口的移动,你需要两个事件,(按/拖动),当你按下栏时,通过初始化( xOffset,yOffset )的位置开始动作,我认为如果我没有弄错您所犯的错误是您使用的是屏幕而不是场景这是您第一个问题的解决方案:

To facilitate the movement of your entire window you need two events, (Press/ Drag), the action starts when you press the bar, by initializing the position of (xOffset, yOffset), I think if i'm not mistaken the error you have made Is that you used the screen rather than the scene Here is the solution to your first problem :

//Use Press Event instead of Click Event
xOffset = event.getSceneX();
yOffset = event.getSceneY();

//Drag Event
window.setX(event.getScreenX() - xOffset);
window.setY(event.getScreenY() - yOffset);

对于第二个问题,您可以在布局外添加关闭按钮,这对我来说是 最简单的解决方案。

for the second problem, you can add your close button outside the layout this is for me the simplest solution.

这篇关于拖动未修整的舞台时,如何强制光标在窗口上保持原位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆