如何在 JavaFX 中建立 SplitPane?


SplitPane 是一個 UI 元件,包含兩個或多個面,中間有一個分隔符。此分隔符可移動;你可以使用它來減少/增加一面的區域。你可以透過例項化 javafx.scene.control.SplitPane 類來建立分割窗格。

SplitPane 的邊可以水平或垂直排列。預設情況下,建立的 SplitPane 是水平的,你可以使用 setOrientation() 方法來更改其方向。

示例

以下示例演示瞭如何建立 SplitPane

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SplitPaneExample 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(250);
      view1.setFitHeight(150);
      //Creating ImageView object2
      Image img2 = new Image(new FileInputStream("D:\images\boy.jpg"));
      ImageView view2 = new ImageView(img2);
      view2.setFitWidth(250);
      view2.setFitHeight(150);
      //Creating a SplitPane
      SplitPane splitPane = new SplitPane();
      //Creating stack panes holding the ImageView objects
      StackPane stackPane1 = new StackPane(view1);
      StackPane stackPane2 = new StackPane(view2);
      //Adding the stackpanes to the splitpane
      splitPane.getItems().addAll(stackPane1, stackPane2);
      //Setting anchor pane as the layout
      AnchorPane pane = new AnchorPane();
      AnchorPane.setTopAnchor(splitPane, 15.0);
      AnchorPane.setRightAnchor(splitPane, 15.0);
      AnchorPane.setBottomAnchor(splitPane, 15.0);
      AnchorPane.setLeftAnchor(splitPane, 15.0);
      pane.getChildren().addAll(splitPane);
      pane.setStyle("-fx-background-color: BEIGE");
      //Setting the stage
      Scene scene = new Scene(pane, 595, 300);
      stage.setTitle("Split Pane");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新時間:2020 年 5 月 20 日

659 次瀏覽

開啟你的 職業生涯

透過完成該課程獲得認證

開始學習
廣告