Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否在vbox javafx中移动卡?_Java_Javafx - Fatal编程技术网

是否在vbox javafx中移动卡?

是否在vbox javafx中移动卡?,java,javafx,Java,Javafx,我有一个vbox,其中包含一个卡片列表(锚定窗格)。我试着让每张卡片都可以拖动,这样我就可以随时更改列表的顺序。我正在使用场景生成器 但是,在我尝试拖放之后,我一直收到以下消息 Java Messsge:class javafx.scene.layout.VBox cannot be cast to class javafx.scene.layout.AnchorPane(javafx.scene.layout.VBox and javafx.scene.layout.AnchorPane ar

我有一个vbox,其中包含一个卡片列表(锚定窗格)。我试着让每张卡片都可以拖动,这样我就可以随时更改列表的顺序。我正在使用场景生成器

但是,在我尝试拖放之后,我一直收到以下消息

Java Messsge:class javafx.scene.layout.VBox cannot be cast to class javafx.scene.layout.AnchorPane(javafx.scene.layout.VBox and javafx.scene.layout.AnchorPane are in module javafx.graphics of loader 'app') 
我的代码:

提供的最小代码仅包含两张卡,其中一张卡仅在我的代码中使用

编辑:我不再收到错误消息,但是,我没有得到我想要的结果,因为卡没有改变位置

mport javafx.beans.property.ObjectProperty;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TitledPane;
import javafx.scene.image.WritableImage;
import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Controller
{
    @FXML public AnchorPane card1, card2;
    @FXML public AnchorPane current;
    @FXML public GridPane gridPane;
    @FXML public GridPane column1;
    @FXML public AnchorPane col1;
    @FXML public Button button1, button2;
    @FXML public Button currentButton;
    @FXML public VBox v1;
 @FXML
    public void detect() {  //detect card
            card1.setOnDragDetected((MouseEvent event) -> {  //card1 is the card
                Dragboard db = card1.startDragAndDrop(TransferMode.MOVE);
                ClipboardContent content = new ClipboardContent();
                content.putString(card1.getId());
                WritableImage snapshot = card1.snapshot(new SnapshotParameters(), null);
                db.setDragView(snapshot);
                db.setContent(content);
                event.consume();
            });
    }

    @FXML
    public void accept() {  //add to vbox
        v1.addEventHandler(DragEvent.DRAG_OVER, (DragEvent event) -> { //v1 is the vbox
            if (event.getGestureSource() != v1
                    && event.getDragboard().hasString()) {
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }
            event.consume();
        });
    }

    @FXML
    public void drop() {
       v1.addEventHandler(DragEvent.DRAG_DROPPED, (DragEvent event) -> {
                    Dragboard db = event.getDragboard();
                    boolean success = false;
                    if (db.hasString()) {
                        VBox parent = (VBox) card1.getParent();
                        //int targetIndex = parent.getChildren().indexOf(card1);
                        List<Node> nodes = new ArrayList<Node>(parent.getChildren());
                        parent.getChildren().clear();
                        parent.getChildren().addAll(nodes);
                        success = true;
                    }
                    event.setDropCompleted(success);
                    event.consume();
                });
    }

}
有人能帮我一下吗。

ancorpane parent=(ancorpane)card1.getParent()(在代码底部的drop()方法中)


card1.getParent()
类型为
VBox
的无法强制转换为
AnchorPane

请向导致错误的这些行添加指针。(谢天谢地,我有CTRL+F)它并没有说是哪一行导致了错误,只是当我拖动卡片并试图放下它时,我得到了上面显示的消息。你在哪里看到这条消息?您的IDE应该有一个显示控制台输出的位置,并且在该输出中应该有一个与
ClassCastException
相关的堆栈跟踪。堆栈跟踪将告诉您抛出错误的行,请参见。请正如反复提出的那样。你为什么不干脆提供呢?这是一种强大的调试策略,通常会帮助您自己找到原因,如果不是,这是其他人看到问题的唯一方式。@kleopatra真的很抱歉,已经提供了它。对不起,您能再详细说明一下吗?把它想象成试图将
java.lang.String
转换为
javax.swing.JFrame
一个更好的例子:
Integer
Byte
都扩展了java.lang.Number。它们可以相互铸造<代码>字符串
不可用。因此,
String s=5
会导致错误。很抱歉,我仍然不太明白确切的问题是什么,也不知道如何解决。我是否应该将vbox更改为其他内容?@rex如果您试图从其父卡中删除该卡,并且您知道其父卡是
vbox
,为什么不直接执行
vbox parent=(vbox)card1.getParent()?或者您的代码中是否有更多内容使该解决方案无效(如果有,请在问题中添加一个)?
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="772.0" prefWidth="1295.0" style="-fx-background-color: #C5F5D4;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="KanbanBoard.Controller">
   <children>
      <AnchorPane layoutY="47.0" prefHeight="687.0" prefWidth="1295.0">
         <children>
            <AnchorPane fx:id="col1" layoutX="-1.0" prefHeight="724.0" prefWidth="216.0">
               <children>
                  <GridPane fx:id="column1" layoutX="33.0" layoutY="15.0" onMouseDragOver="#accept" prefHeight="724.0" prefWidth="209.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="0.0">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints maxHeight="353.8666748046875" minHeight="10.0" prefHeight="103.20001831054685" vgrow="SOMETIMES" />
                      <RowConstraints maxHeight="621.5999816894531" minHeight="10.0" prefHeight="621.5999816894531" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <VBox fx:id="v1" onDragDropped="#drop" onDragOver="#accept" prefHeight="200.0" prefWidth="210.0" GridPane.rowIndex="1">
                           <children>
                              <AnchorPane fx:id="card1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#detect" onMouseDragged="#handleLabel" prefHeight="125.0" prefWidth="198.0" style="-fx-background-color: #E5EAE6;">
                                 <children>
                                    <TextField layoutX="5.0" layoutY="3.0" prefHeight="26.0" prefWidth="65.0" promptText="ID" />
                                    <TextField layoutX="72.0" layoutY="3.0" prefHeight="26.0" prefWidth="124.0" promptText="Title" />
                                    <TextArea layoutX="5.0" layoutY="32.0" prefHeight="50.0" prefWidth="190.0" promptText="Description" />
                                    <TextField layoutX="5.0" layoutY="85.0" prefHeight="26.0" prefWidth="124.0" promptText="Story point" />
                                    <Button fx:id="button1" layoutX="164.0" layoutY="85.0" mnemonicParsing="false" onMouseClicked="#removeCard" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="-" />
                                    <Button layoutX="129.0" layoutY="85.0" mnemonicParsing="false" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="+" />
                                 </children>
                              </AnchorPane>
                              <AnchorPane fx:id="card2" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#detect" onMouseClicked="#handleLabel" prefHeight="125.0" prefWidth="198.0" style="-fx-background-color: #E5EAE6;">
                                 <children>
                                    <TextField layoutX="5.0" layoutY="3.0" prefHeight="26.0" prefWidth="65.0" promptText="ID" />
                                    <TextField layoutX="72.0" layoutY="3.0" prefHeight="26.0" prefWidth="124.0" promptText="Title" />
                                    <TextArea layoutX="5.0" layoutY="32.0" prefHeight="50.0" prefWidth="190.0" promptText="Description" />
                                    <TextField layoutX="5.0" layoutY="85.0" prefHeight="26.0" prefWidth="124.0" promptText="Story point" />
                                    <Button fx:id="button2" layoutX="164.0" layoutY="85.0" mnemonicParsing="false" onMouseClicked="#removeCard" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="-" />
                                    <Button layoutX="129.0" layoutY="85.0" mnemonicParsing="false" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="+" />
                                 </children>
                              </AnchorPane>
                           </children>
                        </VBox>
                        <Pane prefHeight="98.0" prefWidth="226.0" style="-fx-background-color: #D1D3D1;">
                           <children>
                              <Button layoutX="124.0" layoutY="64.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="26.0" text="-" />
                              <TextArea layoutY="39.0" prefHeight="50.0" prefWidth="124.0" promptText="Role" />
                              <TextField layoutX="1.0" layoutY="5.0" prefHeight="26.0" prefWidth="124.0" promptText="Name" />
                              <Button fx:id="addColumnButton" layoutX="124.0" layoutY="36.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="26.0" text="+" />
                           </children>
                        </Pane>
                     </children>
                  </GridPane>
               </children>
            </AnchorPane>
            <AnchorPane layoutX="1082.0" prefHeight="726.0" prefWidth="213.0">
               <children>
                  <Pane prefHeight="50.0" prefWidth="214.0" style="-fx-background-color: #8F9691;">
                     <children>
                        <Label layoutX="49.0" layoutY="7.0" prefHeight="36.0" prefWidth="46.0" text="Log" textAlignment="CENTER" textOverrun="WORD_ELLIPSIS">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
                  <Pane layoutY="49.0" prefHeight="676.0" prefWidth="214.0" style="-fx-background-color: #C6C8C6;" />
               </children>
            </AnchorPane>
            <GridPane fx:id="gridPane" layoutX="228.0" onDragDropped="#drop" onDragOver="#accept" prefHeight="726.0" prefWidth="856.0">
              <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints maxHeight="627.1999938964843" minHeight="10.0" prefHeight="622.4000061035156" vgrow="SOMETIMES" />
              </rowConstraints>
            </GridPane>
         </children>
      </AnchorPane>
      <Label layoutX="381.0" layoutY="-4.0" prefHeight="50.0" prefWidth="180.0" text="Kanban Board">
         <font>
            <Font size="28.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.stage.Stage;
import java.io.IOException;

public class Kanban extends Application {
    //private Button b;
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("Kanban.fxml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Controller controller = new Controller();
        FXMLLoader loader = new FXMLLoader();
        loader.setController(controller);
        //Controller c = loader.getController();

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}