问题是当删除旧标签时,它似乎不是垃圾收集。
这是一个简单的例子
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Tabs example");
// create window holder
final TabPane tabPane = new TabPane();
tabPane.setSide(Side.BOTTOM);
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.SELECTED_TAB);
tabPane.getTabs().add(new Tab("First Tab"));
// create buttons
Button add = new Button("Add Tab");
Button remove = new Button("Remove Tab");
HBox hBox = new HBox(8);
hBox.getChildren().add(add);
hBox.getChildren().add(remove);
add.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
tabPane.getTabs().add(new Tab("New Tab"));
}
});
remove.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
if (!tabPane.getSelectionModel().isEmpty()){
tabPane.getTabs().remove(tabPane.getSelectionModel().getSelectedIndex());
}
}
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(tabPane);
borderPane.setTop(hBox);
// add components to screen
Group root = new Group();
Scene scene = new Scene(root);
borderPane.setPrefSize(500, 500);
root.getChildren().add(borderPane);
stage.setScene(scene);
stage.centerOnScreen();
stage.show();
}
问题是:当我决定关闭标签时,除了remove()之外,我还想做更多的事情吗?或者这仅仅是内存泄漏?
谢谢,