gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:54 26 4
gpt4 key购买 nike

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

//已删除,修改后完整代码在下方

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

好吧,这就是所谓的 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;
}
}

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

最佳答案

试试这个:

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

希望对您有所帮助。

关于当我在这个对象的类中时,JavaFx 从 Pane 对象中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20912575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com