嘿,我搜查了一段時間的網,但我找不到解決以下問題:如何在控制器類中的javafx應用程序中交換屏幕?
在javafx中你有3個基本文件;控制器類,fxml文件和應用程序類。現在我想在控制器中做出反應,按下按鈕(其效果非常好),然後在點擊時改變屏幕(通常用stage.setScreen()做的),但是我沒有提及舞臺(你可以在應用程序類中找到)。
應用程序示例:
public class JavaFXApplication4 extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXML-樣品:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication4.SampleController">
<children>
<Button id="button" fx:id="nextScreen" layoutX="126.0" layoutY="90.0" onAction="#handleButtonAction" text="Next Screen" />
<Label fx:id="label" layoutX="126.0" layoutY="120.0" minHeight="16.0" minWidth="69.0" />
</children>
</AnchorPane>
控制器 - 樣品:
public class SampleController implements Initializable {
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
//Here I want to swap the screen!
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
我會感謝任何形式的幫助。
非常感謝,那就是我一直在尋找:) –
這是非常有益的!謝謝 :) –