Tenho um problema estranho com o posicionamento ImageView
de s em a Scene
no JavaFX.
Quero que três imagens do mesmo tamanho (200 x 200) apareçam uma ao lado da outra na mesma altura. Então, especifico o mesmo valor em setTranslateY()
para cada imagem. Mas, por algum motivo, elas aparecem em alturas diferentes: Cada imagem seguinte parece 200 pixels mais baixa que a anterior.
Aqui está o código:
public class Main extends Application {
@Override
public void start(Stage myStage) throws IOException {
myStage.setTitle("title");
myStage.setScene(getScene());
stage = myStage;
myStage.show();
}
public static void main(String[] args) {
launch();
}
private Scene getScene() {
List<Node> nodes = new ArrayList<>();
Image i1 = loadFromPath("... some path");
Image i2 = loadFromPath("... some other path");
Image i3 = loadFromPath("... again some other path");
ImageView v1 = new ImageView(i1);
ImageView v2 = new ImageView(i2);
ImageView v3 = new ImageView(i3);
v1.setTranslateX(100);
v1.setTranslateY(100);
v2.setTranslateX(320);
v2.setTranslateY(100);
v3.setTranslateX(540);
v3.setTranslateY(100);
nodes.add(v1);
nodes.add(v2);
nodes.add(v3);
VBox box = new VBox(nodes.toArray(new Node[0]));
box.setMinWidth(800);
box.setMaxWidth(800);
box.setMinHeight(400);
box.setMaxHeight(400);
return new Scene(box);
}
private Image loadFromPath(String path) {
FileInputStream input = null;
try {
input = new FileInputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
return new Image(input);
}
}
O resultado fica assim:
Quando uso setLayoutX()
and setLayoutY()
em vez de setTranslateX()
and setTranslateY()
, o resultado é ainda pior:
Alguém pode explicar esse resultado?