我查看了旧代码中的所有注释和评论,但我不明白为什么它不起作用。
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
}
}
Vs以上。
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 =新场景....作品
VS
场景scene2 =新场景....不起作用。这是为什么?
答案 0 :(得分:0)
您是shadowing your member variables并在第二个代码中将它们保留为空。
换句话说,第一个代码中只有Scene
的2个实例,而另一个代码中只有4个
执行此操作时,它使用Main.this.scene2
,而不是本地实例。
button1.setOnAction(e -> window.setScene(scene2));
如果确实需要使用本地实例,则需要声明它final
并在设置动作侦听器之前进行初始化。
答案 1 :(得分:0)
将场景放在它们之前的原因不起作用是因为你已经声明了它们,但没有初始化它们,因为行中的场景
Scene scene1, scene2
所以你不需要再次指定。
这就像创建一个整数int a
然后初始化它int a = 1
。这没有意义,因为a
已被定义为int
。
实际上,它不是“它不起作用”而是“它没有意义”。