【问题标题】:How to delete the instance of the button from UI view in javafx如何从 javafx 的 UI 视图中删除按钮的实例
【发布时间】:2015-06-25 15:56:31
【问题描述】:

我在 UI 的 HBox 中存在的 java fx 中有一个按钮,我想在单击同一个按钮时删除该按钮。我已经编写了相同的操作事件:

@FXML
private HBOx projectlist;
String buttonid;

字符串按钮名; /** * 初始化控制器类。 */ @FXML 公共按钮删除;

button.setOnMouseClicked(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent t) {
            buttonid= button.getId();
            buttoname = button.getText();
            projectlist.getChildren().remove(buttoname);
        }
    });

实际上问题是我在此函数中从其他控制器获取按钮,我无法在另一个按钮(删除)操作事件中使用该按钮,尽管我可以在删除操作事件中使用 buttonid 变量:public void setButton(Button button,String buttonname) {

projectlist.setSpacing(10);

button.setOnMouseClicked(new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent t) {
        buttonid= button.getId();
        buttoname = button.getText();


    }
});

projectlist.getChildren().add(button);

}                                                                                                  @FXML
private void gotoDelete(ActionEvent event) throws IOException{
     ProjectModule.DeleteProject(Integer.parseInt(buttonid));

projectlist.getChildren().remove(button);
}                                 

【问题讨论】:

    标签: button javafx


    【解决方案1】:

    试试

    button.setOnMouseClicked(new EventHandler<MouseEvent>() {
    
        @Override
        public void handle(MouseEvent t) {
            buttonid= button.getId();
            buttoname = button.getText();
    
            projectList.getChildren().remove(button);
    
        }
    });
    

    【讨论】:

    • 对不起,我不明白编辑。您可以修复格式并使其更清晰吗?您只需要在按钮的处理程序方法中调用projectList.getChildren().remove(button);。 (顺便说一句,你真的应该使用setOnAction,而不是setOnMouseClicked。)
    • 谢谢詹姆斯......实际上我找到了解决方案......可能不是最好的方法,但仍然对我有用............跨度>
    猜你喜欢
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多