如何将imageView添加到javafx区域元素?

时间:2012-06-24 20:47:57

标签: imageview javafx-2 region

我想知道如何将ImageView元素添加到JavaFx 2.1中的Region元素。

也许我得到这个元素的用法错了,但是AFAIK它也是子元素的容器。

背景是我需要一个定义大小的区域,它应该独立于区域上的视口显示图像元素,所以我不能将Group元素用作容器。

1 个答案:

答案 0 :(得分:7)

使用Pane或Pane子类。

您可以使用Region api向其添加子项的getChildren()个窗格。窗格与Group非常相似;例如有一个简单的API用于添加孩子,并没有明确布局孩子的位置。它还有一个地区的方面;例如css styleable,resize able等。Region只有一个不可修改的子列表通过他们的公共API,这意味着添加子项的唯一方法是将它们子类化(就像Pane已经为你做的那样)。 Region类本身实际上只是控件创建者的构建块类,而不是在正常开发期间实例化的东西。

以下是将ImageView个节点添加到窗格的示例。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.*;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class RegionSample extends Application {
  public static void main(String[] args) throws Exception { launch(args); }
  public void start(Stage stage) throws Exception {
    Pane pane = new Pane();
    pane.setStyle("-fx-background-color: linear-gradient(to bottom right, derive(goldenrod, 20%), derive(goldenrod, -40%));");
    ImageView iv1 = new ImageView(new Image("http://icons.iconarchive.com/icons/kidaubis-design/cool-heroes/128/Ironman-icon.png"));  // Creative commons with attribution license for icons: No commercial usage without authorization. All rights reserved. Design (c) 2008 - Kidaubis Design http://kidaubis.deviantart.com/  http://www.kidcomic.net/ All Rights of depicted characters belong to their respective owners.
    ImageView iv2 = new ImageView(new Image("http://icons.iconarchive.com/icons/kidaubis-design/cool-heroes/128/Starwars-Stormtrooper-icon.png"));
    iv1.relocate(10, 10);
    iv2.relocate(80, 60);
    pane.getChildren().addAll(iv1, iv2);
    stage.setScene(new Scene(pane));
    stage.show();
  }
}
相关问题