当我在这个对象的类中时,JavaFx 从窗格对象中删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20912575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:16:30  来源:igfitidea点击:

JavaFx removing from pane object when I am in this object's class

javajavafx

提问by Dess

I don't know if anyone could understand my problem from the title, but here's more specific description. I have class, in which I created a FlowPane, where I added objects of another class(images packed inside VBoxes). Each VBox have ContextMenu, where is MenuItem "Remove File". My problem is, how to remove this object while beeing inside the VBox class. Here is a little part of my code:

我不知道是否有人能从标题中理解我的问题,但这里有更具体的描述。我有一个类,我在其中创建了一个 FlowPane,我在其中添加了另一个类的对象(打包在 VBox 中的图像)。每个 VBox 都有 ContextMenu,其中 MenuItem 为“删除文件”。我的问题是,如何在 VBox 类中删除此对象。这是我的代码的一小部分:

//removed, entire code is below after edit

//已删除,编辑后完整代码如下

The code where I'm accessing my CustomPane (my class of FlowPane, with specified attributes) works, because I can remove object if I'm doing it by their indexes, but when I remove one of them, other's indexes changes, so I'm looking for another solution. I need to specifically remove the object of the class in the code.

我访问我的 CustomPane(我的 FlowPane 类,具有指定属性)的代码有效,因为如果我通过他们的索引来删除对象,我可以删除对象,但是当我删除其中一个时,其他人的索引会发生变化,所以我正在寻找另一种解决方案。我需要在代码中专门删除类的对象。

Okay so here is so called sscce, which of I had no idea, since now:

好的,所以这里是所谓的 sscce,我不知道,因为现在:

package sscce;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class Sscce extends Application {

@Override
public void start(Stage primaryStage) {
    CustomPane root = new CustomPane();
    root.setPadding(new Insets(20));
    root.setHgap(10);
    root.setVgap(10);
    for (int i = 0; i < 10; i++) {
        RectangleBox recB = new RectangleBox();
        root.getChildren().add(recB);
    }

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}

}

class RectangleBox extends VBox {

static int index = 0;

public RectangleBox() {
    Rectangle rec = new Rectangle(150, 150);
    rec.setFill(Color.GREEN);

    StackPane sp = new StackPane();
    sp.getChildren().add(rec);
    Label label = new Label(Integer.toString(index));
    index++;
    sp.getChildren().add(label);
    getChildren().add(sp);
    final ContextMenu cm = new ContextMenu();
    MenuItem removeRec = new MenuItem("Remove Rectangle");
    removeRec.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {

            ((CustomPane) getParent()).getPane().getChildren().remove(0); //here is the problem, I want this line to remove the rectangle I clicked on(now it's removing first element in the pane).

        }
    });

    cm.getItems().add(removeRec);
    createContextMenuEvent(cm, rec);

}

private void createContextMenuEvent(final ContextMenu cm, final Rectangle rec) {
    addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent t) {
            if (t.getButton() == MouseButton.SECONDARY) {
                cm.show(rec, t.getScreenX(), t.getScreenY());
            }
        }
    });
}

}

class CustomPane extends FlowPane {

public CustomPane() {
    //setAlignment(Pos.CENTER);
    setHgap(25);
    setVgap(25);
    setPadding(new Insets(20));
}

public CustomPane getPane() {
    return this;
}
}

It should work after copy/paste this entire code to java project. So I removed everything that is not neccessary, and I have replaced images to rectangles, now this program looks kind of stupid;p I added comment to a line I have problem with. I hope now it's a lot clearer than before.

它应该在将整个代码复制/粘贴到 java 项目后工作。所以我删除了所有不必要的东西,并将图像替换为矩形,现在这个程序看起来有点愚蠢;p 我在我有问题的行中添加了注释。我希望现在比以前清楚多了。

采纳答案by doomsdaymachine

try this:

尝试这个:

((CustomPane) RectangleBox.this.getParent()).getChildren().remove(RectangleBox.this);

hope it helps.

希望能帮助到你。