![](/img/trans.png)
[英]Problems getting the width and height of a pane from a class object - Javafx
[英]JavaFx removing from pane object when I am in this object's class
我不知道是否有人能從標題中理解我的問題,但這里有更具體的描述。 我有一個類,我在其中創建了一個FlowPane,其中我添加了另一個類的對象(在VBoxes中打包的圖像)。 每個VBox都有ContextMenu,其中是MenuItem“Remove File”。 我的問題是,如何在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);
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.