单击按钮JavaFX时显示其他窗口
我想解决我的家庭作业,但我不知道如何开始;目标是用JavaFX制作2个GUI表单。第一个是包含按钮1的主窗体,当用户单击按钮1时:显示第二个窗体并关闭第一个窗体 怎么做?希望给我举个例子单击按钮JavaFX时显示其他窗口,java,netbeans,jdk1.6,Java,Netbeans,Jdk1.6,我想解决我的家庭作业,但我不知道如何开始;目标是用JavaFX制作2个GUI表单。第一个是包含按钮1的主窗体,当用户单击按钮1时:显示第二个窗体并关闭第一个窗体 怎么做?希望给我举个例子 感谢您的阅读和帮助。您可以这样做,但请记住,我们是通过实践和培训来学习的,在看了本例中的想法后,尝试自己做一个: import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Butto
感谢您的阅读和帮助。您可以这样做,但请记住,我们是通过实践和培训来学习的,在看了本例中的想法后,尝试自己做一个:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TwoForms extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane(); // TLC (Top Layer Container) a root container for all other components, which in your case is the Button
Button button = new Button("Go To Second Form"); // the button
root.getChildren().add(button); // add the button to the root
Scene scene = new Scene(root, 500,500); // create the scene and set the root, width and height
primaryStage.setScene(scene); // set the scene
primaryStage.setTitle("First Form");
primaryStage.show();
// add action listener, I will use the lambda style (which is data and code at the same time, read more about it in Oracle documentation)
button.setOnAction(e->{
//primaryStage.close(); // you can close the first stage from the beginning
// create the structure again for the second GUI
// Note that you CAN use the previous root and scene and just create a new Stage
//(of course you need to remove the button first from the root like this, root.getChildren().remove(0); at index 0)
StackPane root2 = new StackPane();
Label label = new Label("Your are now in the second form");
root2.getChildren().add(label);
Scene secondScene = new Scene(root2, 500,500);
Stage secondStage = new Stage();
secondStage.setScene(secondScene); // set the scene
secondStage.setTitle("Second Form");
secondStage.show();
primaryStage.close(); // close the first stage (Window)
});
}
public static void main(String[] args) {
launch();
}
}
结果
单击按钮->第二个窗口后
是的,我知道stackoverflow不是一个家庭作业解决方案,谢谢你,我会等其他人来帮我