我正在使用javafx 2.0和netbean 7.1,在通過ImageView拖放圖像時遇到了問題,.i將圖像保存爲源(一個圖像)和2個目標點(2個盒子作爲目標點)。當第一次嘗試拖動圖像時,它的工作正常,並且在源圖像輸入到目標box.and後再次嘗試拖動圖像,下面的錯誤被修改爲「java.lang .IllegalArgumentException:錯誤的字節緩衝區大小18x15 [1080]!= 0「在Javafx 2.0中拖放事件
一旦圖像移動到目標對象,我需要設置偵聽器將其更改爲源,我覺得它在這個地方拋出錯誤..
正在使用的代碼
public class DragandDropEx extends Application {
/**
* @param args the command line arguments
*/
GridPane Board;
ImageView deactivateImageView = new ImageView();
ImageView newImageView = new ImageView();
final Rectangle target = new Rectangle(0, 0, 50, 50);
final Rectangle target2 = new Rectangle(0, 0, 50, 50);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
target.setFill(Color.CHOCOLATE);
target2.setFill(Color.BLUE);
Image image = new Image(getClass().getResourceAsStream("triangle.png"));
getDeactivateImageView().setImage(image);
Board = new GridPane();
primaryStage.setTitle("Drag and Drop");
createSource(getDeactivateImageView());
target.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent events) {
events.acceptTransferModes(TransferMode.MOVE);
events.consume();
createTargetDrop(target,0,8);
}
});
target2.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent events) {
events.acceptTransferModes(TransferMode.MOVE);
events.consume();
createTargetDrop(target2,0,9);
}
});
Board.add(getDeactivateImageView(), 0, 1);
Board.add(target, 0, 8);
Board.add(target2, 0, 9);
StackPane root = new StackPane();
root.getChildren().add(Board);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private void createSource(final ImageView imageView) {
imageView.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent events) {
Dragboard storeImage =imageView.startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
content.putImage(imageView.getImage());
storeImage.setContent(content); **// here i am getting error**
events.consume();
}
});
}
private void createTargetDrop(final Rectangle target,final int xCordination,final int yCordination) {
target.setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
Image dragedImage = db.getImage();
getNewImageView().setImage(dragedImage);
getDeactivateImageView().setVisible(false);
setDeactivateImageView(getNewImageView());
Board.add(getDeactivateImageView(),xCordination,yCordination);
event.consume();
createSource(getDeactivateImageView()); // setting listener to new image
}
});
}
}
嗨,我試過2.1也,但我仍然面臨同樣的問題。 – 2012-03-08 07:22:15