JAVAの初心者です。練習でJavaFXを使ってボタンを押して選択ダイアログを出して、画像を表示するプログラムを作りたいのですが、一応コードは画像を表示するところまで書いたのですが、画像をボタンの上に表示させたいのに、ボタンの下に表示されます。↓
画像をボタンの上に表示させるにはどうすればいいのでしょうか?
Java
1 2package rensyuuyou; 3 4import java.io.*; 5 6import javafx.application.Application; 7import javafx.event.ActionEvent; 8import javafx.event.EventHandler; 9import javafx.geometry.Insets; 10import javafx.geometry.Pos; 11import javafx.scene.Scene; 12import javafx.scene.control.Button; 13import javafx.scene.layout.VBox; 14import javafx.stage.Stage; 15import javafx.stage.FileChooser; 16import javafx.scene.image.Image; 17import javafx.scene.image.ImageView; 18 19 20public class Rensyuuyou extends Application { 21 22 @Override 23 public void start(Stage stage) { 24 stage.setTitle("練習用"); 25 stage.setWidth(2000); 26 stage.setHeight(1000); 27 28 /* 29 画像を表示する 30 */ 31 Image image = new Image(new File 32 ("画像ファイルのパス名").toURI().toString()); 33 ImageView imageView = new ImageView(image); 34 imageView.setFitHeight(500); 35 imageView.setFitWidth(1000); 36 37 /* 38 ボタンの表示 39 */ 40 41 Button botan = new Button("画像表示"); 42 botan.setPrefSize(900, 30); 43 44 45 46 /* 47 ボタンを押したときの動作 48 */ 49 botan.setOnAction(new EventHandler<ActionEvent>() { 50 @Override 51 public void handle(ActionEvent event) { 52 FileChooser fileChooser = new FileChooser(); 53 fileChooser.setTitle("画像を開く"); 54 fileChooser.getExtensionFilters().addAll( 55 new FileChooser.ExtensionFilter("イメージファイル", "*.jpg", "*.png")); 56 fileChooser.showOpenDialog(null); 57 } 58 }); 59 /* 60 画像を表示するためのレイアウトコード 61 */ 62 63 VBox imageBox = new VBox(); 64 imageBox.setAlignment(Pos.TOP_CENTER); 65 imageBox.setPadding(new Insets(30, 30, 30, 30)); 66 imageBox.getChildren().addAll(imageView); 67 68 /* 69 ボタン表示するためのレイアウトコード 70 */ 71 VBox hyouji = new VBox(); 72 hyouji.setAlignment(Pos.BOTTOM_CENTER);//中央下に表示 73 hyouji.setPadding(new Insets(30, 30, 30, 30)); 74 hyouji.getChildren().addAll(botan,imageBox); 75 stage.setScene(new Scene(hyouji)); 76 stage.show(); 77 } 78 79 /** 80 * @param args the command line arguments 81 */ 82 public static void main(String[] args) { 83 launch(args); 84 } 85 86} 87
(追記)
すいません。botanとimageBOxの順番を入れ替えたら、ボタンの上のほうに表示することができましたが、今度は上の部分に空白が空いてしまいました。これを埋めるにはどうすればいいのでしょうか?
回答2件
あなたの回答
tips
プレビュー