我正在尝试向多个节点添加一个事件处理程序,这是onDragDetected事件。但是当我在一个循环中分配它时,它似乎没有注册它。我正在使用的代码如下所示:
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class DragAndDrop extends Application {
Group page1,page2;
ArrayList<Group> pages;
FlowPane bottom;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage stage) throws Exception {
pages = new ArrayList<Group>();
stage.setTitle("scale test");
BorderPane border = new BorderPane();
Group root = new Group();
bottom = new FlowPane();
bottom.setMinSize(Screen.getPrimary().getVisualBounds().getWidth(), 200);
//bottom.setStyle("-fx-background-color: grey");
page1 = new Group();
page2 = new Group();
Rectangle r = new Rectangle(0,0,200, 200);
Rectangle r2 = new Rectangle(205,0,200,200);
Text t = new Text(
"asodjoijdjiasjdoijaisjd\nijasijdiajsidjoias\njiodjiajosijdiojaisjdoijaoisjdiojaoisjdjaoisjdiojsoidj\njdoasijdoajsoidjoaisjoidjaoisjdiojs");
t.setFill(Color.FLORALWHITE);
t.setWrappingWidth(180);
t.setLayoutX(10);
t.setLayoutY(20);
Text t2 = new Text(
"asodjoijdjiasjdoijaisjdijasijdiajsidjoiasjiodjiajosijdiojaisjdoijaoisjdiojaoisjdjaoisjdiojsoidj\njdoasijdoajsoidjoaisjoidjaoisjdiojs");
t2.setFill(Color.FLORALWHITE);
t2.setWrappingWidth(180);
t2.setX(r2.getX()+10);
t2.setY(r2.getY()+20);
page1.getChildren().addAll(r, t);
page2.getChildren().addAll(r2,t2);
setDrag();
root.getChildren().addAll(page1,page2);
pages.add(page1);
pages.add(page2);
border.setCenter(root);
border.setBottom(bottom);
Scene scene = new Scene(border, 800, 700);
stage.setScene(scene);
stage.show();
}
private void createPage(String s){
Group page = new Group();
Rectangle rect = new Rectangle(200,200);
Text text = new Text(
"asodjoijdjiasjdoijaisjdijasijdiajsidjoiasjiodjiajosijdiojaisjdoijaoisjdiojaoisjdjaoisjdiojsoidj\njdoasijdoajsoidjoaisjoidjaoisjdiojs");
text.setFill(Color.FLORALWHITE);
text.setText(s);
text.setWrappingWidth(180);
text.setX(rect.getX()+10);
text.setY(rect.getY()+20);
page.getChildren().addAll(rect,text);
bottom.getChildren().add(page);
}
private void setDrag(){
int pagenum = 0;
while(pagenum<pages.size()){
final Group page = pages.get(pagenum);
page.setOnDragDetected(new EventHandler <MouseEvent>() {
public void handle(MouseEvent event) {
Dragboard drag = page.startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
content.putString(((Text) page.getChildren().get(1)).getText());
drag.setContent(content);
event.consume();
}
});
pagenum++;
}/*
page1.setOnDragDetected(new EventHandler <MouseEvent>() {
public void handle(MouseEvent event) {
Dragboard drag = page1.startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
content.putString(((Text) page1.getChildren().get(1)).getText());
drag.setContent(content);
event.consume();
}
});
page2.setOnDragDetected(new EventHandler <MouseEvent>() {
public void handle(MouseEvent event) {
Dragboard drag = page2.startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
content.putString(((Text) page2.getChildren().get(1)).getText());
drag.setContent(content);
event.consume();
}
});*/
bottom.setOnDragOver(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
/* data is dragged over the target */
/* accept it only if it is not dragged from the same node
* and if it has a string data */
if (event.getGestureSource() != bottom &&
event.getDragboard().hasString()) {
/* allow for both copying and moving, whatever user chooses */
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
event.consume();
}
});
bottom.setOnDragEntered(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
/* show to the user that it is an actual gesture target */
if (event.getGestureSource() != bottom &&
event.getDragboard().hasString()) {
bottom.setStyle("-fx-background-color: green");
}
event.consume();
}
});
bottom.setOnDragExited(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
/* mouse moved away, remove the graphical cues */
bottom.setStyle("-fx-background-color: grey");
event.consume();
}
});
bottom.setOnDragDropped(new EventHandler <DragEvent>() {
public void handle(DragEvent event) {
/* if there is a string data on dragboard, read it and use it */
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasString()) {
createPage(db.getString());
success = true;
}
/* let the source know whether the string was successfully
* transferred and used */
event.setDropCompleted(success);
event.consume();
}
});
}
}
任何帮助将不胜感激。 感谢
答案 0 :(得分:2)
在将网页添加到列表setDrag()
之前,您正在调用ArrayList<Group> pages
方法。在行pages.add(page2);
答案 1 :(得分:0)
您不是创建一个事件处理程序,而是多个事件处理程序。 new
运算符创建一个新对象。
while(pagenum<pages.size()){
final Group page = pages.get(pagenum);
page.setOnDragDetected(new EventHandler <MouseEvent>() {
...
在while
循环
EventHandler eh = new EventHandler <MouseEvent>() {
...
};
并应用它
while(pagenum<pages.size()){
final Group page = pages.get(pagenum);
page.setOnDragDetected(eh);
...