所以我有问题使textarea可拖动,使用相同的事件代码我可以没有问题拖动文本字段,但当我尝试使用textarea我没有得到System.out.println的响应(“是拖“);在textarea上,但当我点击textarea然而我得到反馈它被点击。
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TextAreaDraggableDemo extends Application {
double orgSceneX, orgSceneY;
double orgTranslateX, orgTranslateY;
AnchorPane anchorpane = new AnchorPane();
TextArea textarea = new TextArea();
TextField textfield = new TextField();
BorderPane root = new BorderPane();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(root, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
textarea.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
orgSceneX = e.getSceneX();
orgSceneY = e.getSceneY();
orgTranslateX = ((TextArea) e.getSource()).getTranslateX();
orgTranslateY = ((TextArea) e.getSource()).getTranslateY();
System.out.println("is clicked");
((TextArea) (e.getSource())).toFront();
});
textarea.addEventHandler(MouseEvent.MOUSE_DRAGGED, e -> {
System.out.println("is dragged");
double offsetX = e.getSceneX() - orgSceneX;
double offsetY = e.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
((TextArea) (e.getSource())).setTranslateX(newTranslateX);
((TextArea) (e.getSource())).setTranslateY(newTranslateY);
});
// same code as the TextArea but this works
textfield.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
orgSceneX = e.getSceneX();
orgSceneY = e.getSceneY();
orgTranslateX = ((TextField) e.getSource()).getTranslateX();
orgTranslateY = ((TextField) e.getSource()).getTranslateY();
System.out.println("is clicked");
((TextField) (e.getSource())).toFront();
});
textfield.addEventHandler(MouseEvent.MOUSE_DRAGGED, e -> {
System.out.println("is dragged");
double offsetX = e.getSceneX() - orgSceneX;
double offsetY = e.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
((TextField) (e.getSource())).setTranslateX(newTranslateX);
((TextField) (e.getSource())).setTranslateY(newTranslateY);
});
anchorpane.getChildren().addAll(textarea, textfield);
root.setCenter(anchorpane);
}
}
答案 0 :(得分:0)
TextArea有点特殊。您需要在TextArea的内容上添加事件处理程序,然后对TextArea本身而不是事件的源进行操作。
以下是您的代码的修改版本:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class TextAreaDraggableDemo extends Application {
double orgSceneX, orgSceneY;
double orgTranslateX, orgTranslateY;
@Override
public void start(Stage primaryStage) throws Exception {
TextArea textarea = new TextArea();
Group group = new Group();
group.getChildren().addAll(textarea);
Scene scene = new Scene(group, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
Node textAreaContent = textarea.lookup(".content");
textAreaContent.addEventHandler(MouseEvent.MOUSE_PRESSED, e -> {
System.out.println("is clicked");
orgSceneX = e.getSceneX();
orgSceneY = e.getSceneY();
orgTranslateX = textarea.getTranslateX();
orgTranslateY = textarea.getTranslateY();
textarea.toFront();
});
textAreaContent.addEventHandler(MouseEvent.MOUSE_DRAGGED, e -> {
System.out.println("is dragged");
double offsetX = e.getSceneX() - orgSceneX;
double offsetY = e.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
textarea.setTranslateX(newTranslateX);
textarea.setTranslateY(newTranslateY);
});
}
public static void main(String[] args) {
launch(args);
}
}