首页 / JAVA / 在JavaFX中拖动未修饰的阶段
在JavaFX中拖动未修饰的阶段
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在JavaFX中拖动未修饰的阶段,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4520字,纯文字阅读大概需要7分钟。
内容图文
我希望将Stage设置为“ UNECORATED”,使其可拖动和最小化.问题是我找不到方法,因为我遇到的示例都是通过插入main方法内部的方法来完成此操作的.
我想通过在控制器类中声明的方法来完成此操作,例如我如何使用下面的“ WindowClose()”方法.
如果这似乎不是一个常识性问题,这是我第二天与JavaFX合作.谢谢大家.
// Main Class/ Method
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Fxmltableview extends Application {
public static String pageSource = "fxml_tableview.fxml";
public static Scene scene;
@Override
public void start(Stage stage) throws Exception {
stage.initStyle(StageStyle.UNDECORATED);
stage.initStyle(StageStyle.TRANSPARENT);
Parent root = FXMLLoader.load(getClass().getResource(pageSource));
scene = new Scene(root, Color.TRANSPARENT);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
..
// The Controller
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
public class FXMLTableViewController {
@FXML private TableView<Person> tableView;
@FXML private TextField firstNameField;
@FXML private TextField lastNameField;
@FXML private TextField emailField;
@FXML
protected void addPerson (ActionEvent event) {
ObservableList<Person> data = tableView.getItems();
data.add(new Person(
firstNameField.getText(),
lastNameField.getText(),
emailField.getText()
));
firstNameField.setText("");
lastNameField.setText("");
emailField.setText("");
}
public void WindowClose (ActionEvent event) {
Platform.exit();
}
}
解决方法:
战略
您已经在start方法中引用了该阶段.
您需要的是能够将舞台传递到控制器,以便控制器可以使舞台可被给定节点拖动.
>“直接从调用者向控制器传递参数”方法可用于将阶段引用传递给控制器??:Passing Parameters JavaFX FXML.
>使用makeDraggable method from this sample code允许舞台被给定节点拖动.
样例实施
应用程序启动方法的一些伪代码如下:
stage.initStyle(StageStyle.UNDECORATED);
stage.initStyle(StageStyle.TRANSPARENT);
FXMLLoader loader = new FXMLLoader(
getClass().getResource(
"fxml_tableview.fxml"
)
);
stage.setScene(
new Scene(
(Parent) loader.load()
)
);
FXMLTableViewController controller =
loader.<FXMLTableViewController>getController();
controller.registerStage(stage);
stage.show();
对于控制器的新registerStage方法:
@FXML private Rectangle dragNode;
public void registerStage(Stage stage) {
EffectUtilities.makeDraggable(stage, dragNode)
}
EffectUtilities.makeDraggable()来自我之前链接的示例代码.
更新您的fxml_tableview.fxml文件,以包括控制器中引用的必需的新dragNode.
替代实施
在控制器的initialize方法中,在dragNode的sceneProperty和更改的场景的window属性上添加一个更改侦听器,以获取有关阶段更改的通知,以便您可以调用makeDraggable.
示例代码EffectUtilities.makeDraggable(stage,byNode)
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.util.Duration;
/** Various utilities for applying different effects to nodes. */
public class EffectUtilities {
/** makes a stage draggable using a given node */
public static void makeDraggable(final Stage stage, final Node byNode) {
final Delta dragDelta = new Delta();
byNode.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
// record a delta distance for the drag and drop operation.
dragDelta.x = stage.getX() - mouseEvent.getScreenX();
dragDelta.y = stage.getY() - mouseEvent.getScreenY();
byNode.setCursor(Cursor.MOVE);
}
});
byNode.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
byNode.setCursor(Cursor.HAND);
}
});
byNode.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
stage.setX(mouseEvent.getScreenX() + dragDelta.x);
stage.setY(mouseEvent.getScreenY() + dragDelta.y);
}
});
byNode.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
byNode.setCursor(Cursor.HAND);
}
}
});
byNode.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
byNode.setCursor(Cursor.DEFAULT);
}
}
});
}
/** records relative x and y co-ordinates. */
private static class Delta {
double x, y;
}
}
内容总结
以上是互联网集市为您收集整理的在JavaFX中拖动未修饰的阶段全部内容,希望文章能够帮你解决在JavaFX中拖动未修饰的阶段所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。