2017-05-18 129 views
-2

显示其他窗口我想解决我的功课,我不知道如何开始;目标是使2 GUI表格JavaFX。第一个是包含Button1的首页表单,当用户点击Button1时:显示第二个表单并关闭第一个表单。当点击按钮JavaFX

如何做到这一点?希望给我举例。

感谢您的阅读和帮助。

+0

是的,我知道,计算器是不是一门功课求解 –

+0

确定感谢ü我将等待别人来帮我 –

回答

-1

你可以做这样的事情,但请记住,我们通过实践和培训学习,努力做到有在这个例子来看看的想法后,自己的一个:

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(); 

    } 

} 

结果

enter image description here

点击该按钮后 - >第二个窗口。

enter image description here

+0

感谢,它现在的工作,我会developp它:) –

+0

玉任理解u和没有问题 –