如何在JavaFX中创建SplitPane?
以下示例演示了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);
}
}
输出结果
以上是 如何在JavaFX中创建SplitPane? 的全部内容, 来源链接: utcz.com/z/351564.html