在JavaFX中拖动未装饰的阶段

 2023-02-15    327  

问题描述

我想设置一个”未装饰”的舞台,使拖曳和最小化.问题是我找不到一种方法,因为我来的示例是通过插入主要方法中的方法来做到这一点的示例.

我想通过在控制器类中声明的方法完成此操作,例如我如何使用下面的” windowclose()”方法.

在JavaFX中拖动未装饰的阶段

这是我第二天与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();
    }
}

推荐答案

策略

您已经对开始方法中的阶段有参考.

您需要的是能够将舞台传递给您的控制器,以便控制器可以通过给定的节点使阶段可拖动.

  1. “直接从呼叫者传递参数到控制器”方法可用于传递对您的控制器的阶段参考:传递参数javafx fxml .

  2. 使用 MakedRaggable方法从此示例代码被给定节点拖动.

示例实现

应用程序开始方法的一些psuedo代码如下:

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();

以及控制器的新寄存器阶段方法:

@FXML private Rectangle dragNode;

public void registerStage(Stage stage) {
  EffectUtilities.makeDraggable(stage, dragNode)
}

EffectUtilities.makeDraggable()来自我之前链接的示例代码.

更新您的fxml_tableview.fxml文件以包含控制器中引用的必需的新dragNode.

替代实现

在控制器的初始化方法中,在Dragnode的场景上添加一个更改侦听器和更改的Scene的窗口属性,以通知舞台更改,以便您调用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;
  }
}

其他推荐答案

来自FXML设置您要使用的控制器或组件的ID

<Pane onMousePressed="#panePressed" onMouseDragged="#paneDragged"/>

class Controller.java

@FXML public void panePressed(MouseEvent me){
        delta.x = getStage().getX() - me.getScreenX();
        delta.y = getStage().getY() - me.getScreenY();
    }

    @FXML public void paneDragged(MouseEvent me){
        getStage().setX(delta.x + me.getScreenX());
        getStage().setY(delta.y + me.getScreenY());
    }

您必须拥有Delta类,该类将存储Delta X和Y

的值

public class Delta{
        public double x,y;
    }

要获得舞台,只需创建突变器和登录器,有关铸造的更多信息,请在这里

其他推荐答案

创建此Delta类.

public class Delta {
    double x,y;
}

在控制器类中,进行Delta类的实例并添加这两种方法.

public void panepressed(MouseEvent me)
    {
     stage = (Stage)((Node)me.getSource()).getScene().getWindow();
     delta.x= stage.getX()- me.getScreenX();
     delta.y= stage.getY()- me.getScreenY();


    }
    public void panedraged(MouseEvent me)
    {
     stage = (Stage)((Node)me.getSource()).getScene().getWindow();
     stage.setX(delta.x+me.getScreenX());
     stage.setY(delta.y+me.getScreenY());


    }

接下来,分别在fxml文件或场景构建器中添加这两种方法,分别用于慕斯和慕斯.

<AnchorPane  onMouseDragged="#panedraged" onMousePressed="#panepressed" prefHeight="274.0" prefWidth="250.0" />

以上所述是小编给大家介绍的在JavaFX中拖动未装饰的阶段,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33797.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。