【发布时间】:2020-04-07 04:10:38
【问题描述】:
我正在使用 JavaFX 构建一个日历/计划器应用程序。该应用程序由一个带有 35 (7x5) 个 VBox 的 GridPane 组成。在这些 VBox 中是 TaskButtons(在下面实现)。当我右键单击任务框时,它会将文本变为灰色,当我左键单击 TsskButton 时,我希望它删除按钮。我已经知道的事情。
- AnchorPaneNode(扩展 VBox)没有静态 getChildren() 方法。
- 我无法为窗格创建单独的实例变量,因为我不知道我将拥有多少个。
- getParent().getChildren() 不起作用,因为 getChildren() 的 parents 方法不可见。
- VBox 有一个公共的 getChildren(),但它不是静态的。
- 我尝试为 getChildren() 创建一个静态访问器,但没有成功。
右键单击时,我还能尝试什么方法来移除此按钮?感谢您的帮助!
public class TaskButton extends Button {
protected int buttonNum = AnchorPaneNode.listIndex;
public TaskButton(String str)
{
super(str);
setStyle("-fx-background-color: transparent;");
setOnMouseClicked(e -> {
if(e.getButton() == MouseButton.SECONDARY)
{
//I want to remove this button from the VBox, neither of these work
AnchorPaneNode.getChildren().remove(this);
//or
getParent().getChildren().remove(this);
}
else if(e.getButton() == MouseButton.PRIMARY)
{
setStyle("-fx-background-color: transparent; -fx-text-fill: #666666;");
}
});
}
}
【问题讨论】:
-
我认为这会有所帮助。 stackoverflow.com/questions/29722232/…
-
@KumarAnilChaurasiya 我已经看到了这个帖子,不幸的是它没有帮助。这个解决方案为 VBox 设置了一个实例变量,我想不惜一切代价避免,因为我的应用程序中很可能有数百个甚至数千个 VBox。不过还是谢谢你的建议!