TabPane是一个GUI组件,使用它可以在单个窗口中加载多个文档。选项卡窗格具有标题区域和内容区域,您可以通过单击各个选项卡的标题在它们之间切换。您可以通过实例化javafx.scene.control.TabPane类来创建选项卡窗格。
选项卡窗格中的每个选项卡均由javafx.scene.control.Tab类表示,您可以分别使用此类的setText()和setContent()方法来设置选项卡的标题和内容。
创建所有必需的选项卡后,需要将它们添加为窗格中的-
tabPane.getTabs().addAll(tab1, tab2);
以下JavaFX程序演示了TabTane的创建。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class TabPaneExample extends Application {
public void start(Stage stage) throws FileNotFoundException {
//Creating ImageView object1
Image img1 = new Image(new FileInputStream("D:\\images\\elephant.jpg"));
ImageView view1 = new ImageView(img1);
view1.setFitWidth(595);
view1.setFitHeight(270);
//Creating ImageView object2
Image img2 = new Image(new FileInputStream("D:\\images\\boy.jpg"));
ImageView view2 = new ImageView(img2);
view2.setFitWidth(595);
view2.setFitHeight(270);
//Creating a TabPane
TabPane tabPane = new TabPane();
//Creating the first tab
Tab tab1 = new Tab();
//Setting the text
tab1.setText("Elephant");
//Setting the content
tab1.setContent(view1);
//Creating the second tab
Tab tab2 = new Tab();
//Setting the text
tab2.setText("Boy");
//Setting the content
tab2.setContent(view2);
//Adding tabs to the tab pane
tabPane.getTabs().addAll(tab1, tab2);
//Setting anchor pane as the layout
AnchorPane pane = new AnchorPane();
AnchorPane.setTopAnchor(tabPane, 15.0);
AnchorPane.setRightAnchor(tabPane, 15.0);
AnchorPane.setBottomAnchor(tabPane, 15.0);
AnchorPane.setLeftAnchor(tabPane, 15.0);
pane.getChildren().addAll(tabPane);
pane.setStyle("-fx-background-color: BEIGE");
//Setting the stage
Scene scene = new Scene(pane, 595, 300);
stage.setTitle("Tab Pane");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
输出结果
男孩-
大象-