Buttonについての考察
Buttonコントロールについての考察です
Buttonにイメージを設定する
Buttonにイメージを設定することができます。設定方法は「コンストラクタで設定する方法」と「setGraphic」で設定する方法がある。また文字列に対してのアイコン位置を上下左右で設定することが可能です。
- 1 :
 2 :
 3 :
 4 :
 5 :
 6 :
 7 :
 8 :
 9 :
 10 :
 11 :
 12 :
 13 :
 14 :
 15 :
 16 :
 17 :
 18 :
 19 :
 20 :
 21 :
 22 :
 23 :
 24 :
 25 :
 26 :
 27 :
 28 :
 29 :
 30 :
 31 :
 32 :
 33 :
 34 :
 35 :
 36 :
 37 :
 38 :
 39 :
 40 :
 41 :
 42 :
 43 :
 44 :
 45 :
 46 :
 47 :
 48 :
 49 :
 50 :
 51 :
 52 :
 53 :
 54 :
 55 :
 56 :
 57 :
 58 :
 59 :
 60 :
 61 :
 62 :
- 
import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ContentDisplay; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class JavaFX_Button1 extends Application { public static void main(String... args){ Application.launch(args); } @Override public void start(Stage stage) throws Exception { // アイコンとする画像を取得 Image image = new Image(getClass().getResourceAsStream("image/mark1.png")); ImageView imageView1 = new ImageView(); imageView1.setImage( image ); ImageView imageView2 = new ImageView(); imageView2.setImage( image ); // ボタン作成 Button button1 = new Button( "ボタン1" , imageView1); // コンストラクタでイメージを設定 button1 . setPrefWidth( 100 ); button1 . setPrefHeight( 50 ); // ボタン作成 Button button2 = new Button( "ボタン2"); button2 . setPrefWidth( 100 ); button2 . setPrefHeight( 50 ); button2 . setGraphic( imageView2 ); // イメージの設定 button2 . setContentDisplay(ContentDisplay.TOP); // イメージの位置を右側に設定 // レイアウト VBox vbox = new VBox(); vbox . setAlignment( Pos . CENTER ); vbox . setPadding(new Insets(20,20,20,20)); vbox . setSpacing(20.0); vbox . getChildren() . addAll( button1,button2 ); // ステージ調整 stage . setTitle( "Button" ); stage . setWidth( 300 ); stage . setHeight( 200 ); stage . setScene( new Scene(vbox) ); stage . show(); } }
【 サンプルソース No.1 Button 】
実行結果
 
	イメージ位置の設定は下記のコードで設定する
- 45 :
 45 :
 45 :
 45 :
- 
		setContentDisplay(ContentDisplay.TOP); // アイコンの位置を上に設定 setContentDisplay(ContentDisplay.BOTTOM); // アイコンの位置を下に設定 setContentDisplay(ContentDisplay.LEFT); // アイコンの位置を左に設定 setContentDisplay(ContentDisplay.RIGHT); // アイコンの位置を右に設定 
 
