通过在JavaFX中单击按钮来移除它。

huangapple go评论74阅读模式
英文:

Remove a button by clicking on it in JavaFX

问题

我正在使用 JavaFX 构建一个日历/计划应用程序。该应用程序包含一个单独的 GridPane,其中有 35 个 (7x5) VBox。在这些 VBox 中有 TaskButton(在下面实现)。当我右键点击一个 TaskBox 时,它将把文本变成灰色,而当我左键点击 TaskButton 时,我希望能够删除该按钮。我已经了解的内容如下:

  1. AnchorPaneNode(扩展自 VBox)没有静态的 getChildren() 方法。
  2. 我不能为 pane 创建单独的实例变量,因为我不知道会有多少个实例。
  3. getParent().getChildren() 不起作用,因为父类的 getChildren() 方法不可见。
  4. VBox 有一个公共的 getChildren() 方法,但它不是静态的。
  5. 我尝试创建一个静态访问器来获取 getChildren(),但未成功。

还有什么其他方法可以尝试,在右键点击时删除该按钮?感谢您的帮助!

英文:

I am building a calander/planner application using JavaFX. The app consists of a single GridPane with 35 (7x5) VBox's. Within these VBox's are TaskButtons (implemented below). When I right click a TaskBox it will turn the text to grey and when I left click the TsskButton I want it to delete the button. Things I know already.

  1. AnchorPaneNode (extends VBox) doesn't have a static getChildren() method.
  2. I cannot create a separate instance variable for pane since I will not know how many I will have.
  3. getParent().getChildren() doesn't work because the parents method of getChildren() is not visible.
  4. VBox has a public getChildren(), but it is not static.
  5. I tried to make a static accessor to getChildren(), but was unable to.

What else can I try to get this button removed when right clicking? Thank you for your help!

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;");
		    }		    
		});
	}
}

答案1

得分: 2

找到了我自己问题的答案!
对于那些遇到相同问题的人,这是我解决它的方法:

setOnMouseClicked(e -> {
    if (e.getButton() == MouseButton.SECONDARY) {
        //我想要将此按钮从VBox中移除,但以下两种方法都不起作用
		//AnchorPaneNode.getChildren().remove(this);
		//或者
		VBox vbox = (VBox) getParent();
		vbox.getChildren().remove(this);
    } else if (e.getButton() == MouseButton.PRIMARY) {
        setStyle("-fx-background-color: transparent; -fx-text-fill: #666666;");
	}		    
});

我需要访问 VBox 提供的 public getChildren() 方法,我通过将 (this)getParent() 强制转换为 VBox 来实现。然后,我可以使用 getChildren() 来移除 "this"。

英文:

Found the answer to my own question!
For those that run into the same issue this is what I did to solve it:

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
		VBox vbox = (VBox) getParent();
		vbox.getChildren().remove(this);
    } else if (e.getButton() == MouseButton.PRIMARY) {
        setStyle("-fx-background-color: transparent; -fx-text-fill: #666666;");
	}		    
});

I needed to access the public getChildren() that VBox provides and I did so by casting (this)getParent() to a VBox. From there I was able to getChildren() and remove "this".

huangapple
  • 本文由 发表于 2020年4月7日 12:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61072703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定