如何在 JavaFX 中建立 TabPane?
TabPane 是 GUI 元件,使用該元件可以在一個視窗中載入多個文件。選項卡窗格具有標題區域和內容區域,你可以透過單擊各個標題在選項卡之間切換。你可以透過例項化 javafx.scene.control.TabPane 類來建立選項卡窗格。
建立選項卡
選項卡窗格中的每個選項卡都由 javafx.scene.control.Tab 類表示,你可以使用此類的 setText() 和 setContent() 方法分別設定選項卡的標題和內容。
一旦你建立了所需的所有選項卡,你需要將它們新增到窗格中,如下所示 −
tabPane.getTabs().addAll(tab1, tab2);
示例
以下 JavaFX 程式演示瞭如何建立 TabPane。
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);
}
}輸出
男孩 −

大象 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP