Tôi đã tìm kiếm trên mạng trong một thời gian nhưng tôi không thể tìm thấy giải pháp cho vấn đề sau:Làm thế nào để trao đổi màn hình trong một ứng dụng javafx trong lớp điều khiển?
Trong javafx bạn có 3 tệp cơ bản; lớp điều khiển, tệp fxml và lớp ứng dụng. Bây giờ tôi muốn phản ứng trong bộ điều khiển với một nút bấm (hoạt động hoàn toàn tốt) và thay đổi màn hình trên nhấp chuột đó (mà bạn thường làm với stage.setScreen()), nhưng tôi không có tham chiếu đến giai đoạn (mà bạn có thể tìm thấy trong lớp ứng dụng).
Application-Sample:
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-Sample:
<?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>
Controller-Sample:
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
}
}
tôi sẽ biết ơn đối với bất kỳ loại giúp đỡ.
Cảm ơn rất nhiều, đó chính xác là những gì tôi đang tìm kiếm :) –
Điều đó rất hữu ích! Cảm ơn :) –