2017-02-04 3 views
-3

古いコードのノートとコメントをすべて見てきましたが、なぜ動作しないのかわかりません。これはなぜ私のプログラムを台無しにするのですか?

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 
import java.util.Stack; 
import javafx.scene.layout.VBox; 

public class Main extends Application{ 

    Stage window;//makes window a stage 
    Scene scene1, scene2;//makes scene1 and scene2 into Scene's 
     public static void main(String []args) { 

     //launches the main 
     launch(args); 
    } 
    @Override//Overrides Application 
    public void start(Stage primaryStage) throws Exception { 
     window = primaryStage;//Makes window primary stage 
     Label label1 = new Label ("This is the first scene!");//Makes a label 
     Button button1 = new Button();//Declares button as a Button 
     button1.setText("Click me for Scene 2");//Sets the text of the button 
     button1.setOnAction(e -> window.setScene(scene2)); 
     //This say, on the action, change the scene 

     VBox layout1 = new VBox(20);//Makes layout1 into a VBox 
     layout1.getChildren().addAll(label1, button1);//Adds the 'Children' 
     scene1 = new Scene(layout1, 500, 400);//Sets up layout1 

     Button button2 = new Button();//makes a second button 
     button2.setText("Click me for scene 1");//sets the text for button2 
     button2.setOnAction(e -> window.setScene(scene1));//When button 2 is click, it changes scene 

     StackPane layout2 = new StackPane();//Makes a new StackPane layout 
     layout2.getChildren().add(button2);//Adds button2 to the layout 
     scene2 = new Scene (layout2, 500, 400);//Gives arguemnts for Scene2 

     window.setScene(scene1);//Sets scene of the window stage 
     window.setTitle("This is a title");//Sets title 
     window.show();//Shows the window 

    } 
} 

対。私は2つの間で変更唯一のことは、単語 'シーン' を追加して

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 
import java.util.Stack; 
import javafx.scene.layout.VBox; 

public class Main extends Application{ 

    Stage window;//makes window a stage 
    Scene scene1, scene2;//makes scene1 and scene2 into Scene's 
     public static void main(String []args) { 

     //launches the main 
     launch(args); 
    } 
    @Override//Overrides Application 
    public void start(Stage primaryStage) throws Exception { 
     window = primaryStage;//Makes window primary stage 
     Label label1 = new Label ("This is the first scene!");//Makes a label 
     Button button1 = new Button();//Declares button as a Button 
     button1.setText("Click me for Scene 2");//Sets the text of the button 
     button1.setOnAction(e -> window.setScene(scene2)); 
     //This say, on the action, change the scene 

     VBox layout1 = new VBox(20);//Makes layout1 into a VBox 
     layout1.getChildren().addAll(label1, button1);//Adds the 'Children' 
     Scene scene1 = new Scene(layout1, 500, 400);//Sets up layout1 

     Button button2 = new Button();//makes a second button 
     button2.setText("Click me for scene 1");//sets the text for button2 
     button2.setOnAction(e -> window.setScene(scene1));//When button 2 is click, it changes scene 

     StackPane layout2 = new StackPane();//Makes a new StackPane layout 
     layout2.getChildren().add(button2);//Adds button2 to the layout 
     Scene scene2 = new Scene (layout2, 500, 400);//Gives arguemnts for Scene2 

     window.setScene(scene1);//Sets scene of the window stage 
     window.setTitle("This is a title");//Sets title 
     window.show();//Shows the window 

    } 
} 

SCENE1 =新しいシーンが....

シーンSCENE2 =新しいシーン対

作品....動作しません。どうしてこれなの?

+2

「作品」と「うまくいかない」の意味を詳しく説明できますか?あなたはここにたくさんのコードを掲載していますが、あなたが見ているエラーとそれを修正しようとしたことを知らなくても、私たちが有用なフィードバックを提供することは難しいです。 – templatetypedef

答えて

0

ライン Scene scene1, scene2 でのシーンは、あなたが再びそれを指定する必要はありませんようにそれらが動作しないの前にシーンを置く理由は、すでにそれらを宣言したためであるが、それらを初期化されていません。 整数を作成してint aとし、それをint a = 1に初期化するようなものです。それはaがすでにintとして定義されているため意味がありません。 そして、実際には、「それは機能しません」と「それは理にかなっていない」ことのほうが少ないです。

0

あなたはshadowing your member variablesであり、2番目のコードではnullのままです。これを行うと言い換える

は、他の

の最初のコードでScene、および4の唯一の2つのインスタンスがあります、それはMain.this.scene2、ないローカルインスタンスを使用しています。あなたがローカルインスタンスを使用する必要がなかった場合は

button1.setOnAction(e -> window.setScene(scene2)); 

、あなたはfinalそれを宣言し、アクションリスナーが設定される前に初期化する必要があります。

+2

ラムダ式で参照される 'scene1'と' scene2'がクラスレベルの変数であることを意味するので、ローカルの 'Scene'初期化が' onAction'ボタンの初期化の後に起こるので、これはうまくいきません。初期化の順序を逆にすることで、実際に問題が解決されます。 – n247s

+0

ちょうど気づいた –

関連する問題