顯示其他窗口我想解決我的功課,我不知道如何開始;目標是使2 GUI表格在JavaFX。第一個是包含Button1
的首頁表單,當用戶點擊Button1
時:顯示第二個表單並關閉第一個表單。當點擊按鈕JavaFX
如何做到這一點?希望給我舉例。
感謝您的閱讀和幫助。
顯示其他窗口我想解決我的功課,我不知道如何開始;目標是使2 GUI表格在JavaFX。第一個是包含Button1
的首頁表單,當用戶點擊Button1
時:顯示第二個表單並關閉第一個表單。當點擊按鈕JavaFX
如何做到這一點?希望給我舉例。
感謝您的閱讀和幫助。
你可以做這樣的事情,但請記住,我們通過實踐和培訓學習,努力做到有在這個例子來看看的想法後,自己的一個:
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();
}
}
結果
點擊該按鈕後 - >第二個窗口。
感謝,它現在的工作,我會developp它:) –
玉任理解u和沒有問題 –
是的,我知道,計算器是不是一門功課求解 –
確定感謝ü我將等待別人來幫我 –