Javafx原生的TabPane没有配置标签页的管理菜单。由于项目需要对打开的标签页进行部分关闭和全部关闭的操作,因此使用BorderPane、StackPane和TabPane进行组合封装了一个控件。 效果图如下:
控件代码:
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
public class CloseableTabPane extends BorderPane {
private TabPane tabPane;
public CloseableTabPane() {
this.tabPane = new TabPane();
StackPane sp = new StackPane();
sp.getChildren().add(tabPane);
MenuButton menuButton = new MenuButton("关闭选项");
menuButton.setVisible(false);
StackPane.setMargin(menuButton,new Insets(5));
sp.setAlignment(Pos.TOP_RIGHT);
//Tab全部关闭后,则不显示关闭菜单按钮
tabPane.getTabs().addListener(new ListChangeListener<Tab>() {
@Override
public void onChanged(Change<? extends Tab> c) {
if(tabPane.getTabs().size()==0){
menuButton.setVisible(false);
}else {
menuButton.setVisible(true);
}
}
});
//关闭选中的Tab
MenuItem closeSelected = new MenuItem("关闭选中");
closeSelected.setOnAction(e->{
for (Tab tab:tabPane.getTabs()){
if(tab.selectedProperty().getValue()){
tabPane.getTabs().remove(tab);
break;
}
}
});
menuButton.getItems().add(closeSelected);
//关闭除选中的其他Tab
MenuItem closeOthers = new MenuItem("关闭其他");
menuButton.getItems().add(closeOthers);
closeOthers.setOnAction(e->{
for (Tab tab:tabPane.getTabs()){
if(tab.selectedProperty().getValue()){
tabPane.getTabs().clear();
tabPane.getTabs().add(tab);
break;
}
}
});
//关闭所有的Tab
MenuItem closeAll = new MenuItem("关闭所有");
closeAll.setOnAction(e->{
tabPane.getTabs().clear();
});
menuButton.getItems().add(closeAll);
sp.getChildren().add(menuButton);
super.setCenter(sp);
}
//添加单个Tab
public void addTab(Tab tab){
tab.setClosable(true);
tabPane.getTabs().add(tab);
}
//添加Tab集合
public void addAll(ObservableList<Tab> tabs){
for (Tab tab:tabs){
tab.setClosable(true);
}
tabPane.getTabs().addAll(tabs);
}
}
var foo = 'bar';
测试代码:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.concurrent.atomic.AtomicInteger;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
CloseableTabPane closeableTabPane = new CloseableTabPane();
BorderPane root = new BorderPane();
Button addTabBtn = new Button("添加Tab");
AtomicInteger count = new AtomicInteger(1);
addTabBtn.setOnAction(e->{
Tab tab = new Tab("Tab"+count);
tab.setContent(new Label("这是Tab"+count+"的内容"));
count.getAndIncrement();
closeableTabPane.addTab(tab);
});
BorderPane.setMargin(addTabBtn,new Insets(20));
root.setTop(addTabBtn);
root.setCenter(closeableTabPane);
primaryStage.setTitle("可自定义关闭标签的TabPane控件(qq 1761552314)");
primaryStage.setScene(new Scene(root,800,600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}