无法找到使用JavaFX类中的按钮切换场景的方法

时间:2018-01-08 02:01:28

标签: java class javafx scene stage

当我点击我的按钮时,我正试图让我的场景切换到另一个场景 我的问题是在我的班级的第二个按钮我不知道该放入什么,我无法从该按钮内部设置舞台,因为它是另一种方法我需要设置大约10个不同的场景,非常困惑任何帮助感谢它的学校达到高潮-_-。 (如果有更好的方法来制作改变图片的程序,请通知我,谢谢!)

public class MainStage extends Application  {
    SceneSwitch switcher1= new SceneSwitch();

      MediaPlayer mp3;

      String i="file:///C://NetbeanFiles//plane.jpg";

      Image pic=new Image(i); 


    @Override
    public void start(Stage primaryStage) {

        Media musicfile=new Media("file:///C://NetbeanFiles//Stereo.mp3");
        mp3= new MediaPlayer(musicfile);
        mp3.setAutoPlay(true);
        mp3.setVolume(0.05);


       ImageView view = new ImageView(pic);

       Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

               primaryStage.setScene(switcher1.Switcher());

            }
        });


        BorderPane pane = new BorderPane();
        pane.setCenter(view);

        btn.setPadding(new Insets(10 ,10, 10, 10));
        pane.setTop(btn);

        Scene scene = new Scene(pane, 1920, 1080);

        //how to keep button centered last number is X

        pane.setMargin(btn, new Insets(10, 10, 10, 875));
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }


}





//End of MAINSTAGE

//Start of SCENESWITCH class





 public class SceneSwitch {
        int j=0;
         String i="file:///C://NetbeanFiles//planet.jpg";
    ArrayList<String> Hello= new ArrayList<String>(); 


    public SceneSwitch(){
        Hello.add("file:///C://NetbeanFiles//planet.jpg");
        `Hello.add("file:///C://NetbeanFiles//download.jpg");
     }




    public Scene Switcher(){


      ImageView view = new ImageView(Hello.get(j));

       Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

            //**HERE IS MY PROBLEM NOT SURE WHAT TO PUT IN HERE**
            }
        });


        BorderPane pane = new BorderPane();

        pane.setCenter(view);

        btn.setPadding(new Insets(10 ,10, 10, 10));
        pane.setTop(btn);

        Scene scene = new Scene(pane, 1920, 1080);

        //how to keep button centered last number is X

        pane.setMargin(btn, new Insets(10, 10, 10, 875));


     return scene; 
    }




}

0 个答案:

没有答案