請幫我做到這一點。它凍結而不是改變場景。Javafx如何通過函數創建按鈕?
Button b1 = new Button("Go to s2");
b1.setOnAction(e -> window.setScene(s2));
Button b2 = new Button("Go to s1");
b2.setOnAction(e -> window.setScene(s1));
,但我想,使其更加優雅...
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
Scene s1,s2;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
Button b1 = makeButton("Go to s2", s2);
Button b2 = makeButton("Go to s1", s1);
s1 = new Scene(b1);
s2 = new Scene(b2);
primaryStage.setScene(s1);
primaryStage.show();
}
public Button makeButton(String name, Scene destScene) {
Button button = new Button(name);
button.setOnAction(e -> window.setScene(destScene));
return button;
}
}
非常感謝你提前: 按鈕創建這樣,當它工作正常!