在MouseDragEvent上调整窗格大小

时间:2017-05-15 00:04:16

标签: java events javafx mouseevent

我正在Java中创建一个桌面模拟器游戏。

我有一个工作桌面,我可以在其中生成并销毁各种游戏内应用程序。

我正在尝试找到一种方法来调整每个应用的大小。

这是我的QuickPad(记事本)游戏内应用程序的轻微编辑(减去CSS样式和允许Pane拖动的自定义BorderPane类)。

// The Node Window (In-Game App)
BorderPane node = new BorderPane();
node.setPrefSize(300, 225); // Window Size
// position the node
node.setLayoutX(10 + node.getPrefWidth());

//TitleBar
HBox title_bar = new HBox();
title_bar.setPrefHeight(10);
title_bar.setPadding(new Insets(0,0,0,5));

//Name
Label win_name = new Label("Quickpad");

//Spacer
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);

//Quit
Button win_close = new Button("X");
win_close.setOnAction(close ->  {
    root.getChildren().remove(node);
});
title_bar.getChildren().addAll(win_name, spacer, win_close);
title_bar.setAlignment(Pos.CENTER_RIGHT);
node.setTop(title_bar);

//App
TextArea textArea = new TextArea();
textArea.setWrapText(true);
node.setCenter(textArea);
textArea.setText("Enter some text...");

//Resize
HBox bottom_bar = new HBox();
bottom_bar.setPrefHeight(10);
bottom_bar.setAlignment(Pos.CENTER_RIGHT);
Label resize_text = new Label("||||");
bottom_bar.getChildren().add(resize_text);
node.setBottom(bottom_bar);

    // DragEvent linked to 'resize_text'

// add the node to the root pane 
root.getChildren().add(node);

我可以通过更改node.setPrefSize()来更改应用的大小。

我做了一些研究,发现了这个:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/MouseDragEvent.html

我的目标是当我点击resize_text(或类似对象)时,它会更改node.setPrefSize()(就像你要抓住桌面窗口的一角来调整大小一样)直到我发布了它。

我查看了其他选项,例如DragResizeModhttps://github.com/varren/JavaFX-Resizable-Draggable-Node/blob/master/src/sample/DragResizeMod.java。此解决方案在工作时会消耗除当前应用程序之外的所有鼠标事件(您无法与游戏中的任何其他内容进行交互)。

0 个答案:

没有答案