JAVAの初心者です。説明が下手ですいませんが、練習でJavaFXを使ってボタンを押して選択ダイアログを出して、画像を出力するプログラムを作っているのですが、
このように、startメソッドに書くと、↓
Java
1 2package windowkarasakusei; 3 4import java.io.File; 5import javafx.application.Application; 6import javafx.event.ActionEvent; 7import javafx.event.EventHandler; 8import javafx.geometry.Insets; 9import javafx.geometry.Pos; 10import javafx.scene.Scene; 11import javafx.scene.control.Button; 12import javafx.scene.layout.StackPane; 13import javafx.scene.layout.VBox; 14import javafx.stage.FileChooser; 15import javafx.stage.Stage; 16 17public class Windowkarasakusei extends Application { 18 19 @Override 20 public void start(Stage stage) { 21 stage.setTitle("タイトル"); 22 stage.setHeight(900); 23 stage.setWidth(800); 24 Button botan = new Button("画像を開く"); 25 botan.setMaxSize(440, 400); 26 botan.setOnAction(new EventHandler<ActionEvent>() { 27 /* 28 ここら辺に書くとこの部分がエラーになる 29 */ 30 FileChooser fileChooser = new FileChooser();//FileChooserクラスを実体化 31 File sentakuFile = fileChooser.showOpenDialog(null); 32 fileChooser.setTitle("画像ファイル選択"); 33 fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("イメージファイル", "*.jpg", "*.png")); 34 35 /* 36 ここまで 37 */ 38 39 40 @Override 41 public void handle(ActionEvent event) { 42 43 } 44 }); 45 46 VBox root = new VBox(); 47 root.setAlignment(Pos.BOTTOM_CENTER); 48 root.setPadding(new Insets(40, 60, 60, 40)); 49 stage.setScene(new Scene(root)); 50 root.getChildren().addAll(botan); 51 stage.show(); 52 } 53 54 /** 55 * @param args the command line arguments 56 */ 57 public static void main(String[] args) { 58 launch(args); 59 } 60 61} 62
ある部分が「<identifier>がありません。型の開始が不正です」というエラーが出てしまいますが、
handleメソッドの下に書くと↓
Java
1 2package windowkarasakusei; 3 4import java.io.File; 5import javafx.application.Application; 6import javafx.event.ActionEvent; 7import javafx.event.EventHandler; 8import javafx.geometry.Insets; 9import javafx.geometry.Pos; 10import javafx.scene.Scene; 11import javafx.scene.control.Button; 12import javafx.scene.layout.StackPane; 13import javafx.scene.layout.VBox; 14import javafx.stage.FileChooser; 15import javafx.stage.Stage; 16 17public class Windowkarasakusei extends Application { 18 19 @Override 20 public void start(Stage stage) { 21 stage.setTitle("タイトル"); 22 stage.setHeight(900); 23 stage.setWidth(800); 24 Button botan = new Button("画像を開く"); 25 botan.setMaxSize(440, 400); 26 botan.setOnAction(new EventHandler<ActionEvent>() { 27 28 29 @Override 30 public void handle(ActionEvent event) { 31 /* 32 ここら辺に書くとこの部分はエラーにならない 33 */ 34 FileChooser fileChooser = new FileChooser();//FileChooserクラスを実体化 35 File sentakuFile = fileChooser.showOpenDialog(null); 36 fileChooser.setTitle("画像ファイル選択"); 37 fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("イメージファイル", "*.jpg", "*.png")); 38 39 /* 40 ここまで 41 */ 42 43 } 44 }); 45 46 VBox root = new VBox(); 47 root.setAlignment(Pos.BOTTOM_CENTER); 48 root.setPadding(new Insets(40, 60, 60, 40)); 49 stage.setScene(new Scene(root)); 50 root.getChildren().addAll(botan); 51 stage.show(); 52 } 53 54 /** 55 * @param args the command line arguments 56 */ 57 public static void main(String[] args) { 58 launch(args); 59 } 60 61} 62
エラーになりません。
これはなぜなんでしょうか?
わかりやすく教えてください。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー