2012-10-09 26 views
10

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 đỡ.

Trả lời

18
@FXML 
private void handleButtonAction(ActionEvent event) { 
    System.out.println("You clicked me!"); 
    label.setText("Hello World!"); 
    //Here I want to swap the screen! 

    Stage stageTheEventSourceNodeBelongs = (Stage) ((Node)event.getSource()).getScene().getWindow(); 
    // OR 
    Stage stageTheLabelBelongs = (Stage) label.getScene().getWindow(); 
    // these two of them return the same stage 
    // Swap screen 
    stage.setScene(new Scene(new Pane())); 
} 
+0

Cảm ơn rất nhiều, đó chính xác là những gì tôi đang tìm kiếm :) –

+0

Điều đó rất hữu ích! Cảm ơn :) –

0

Tôi đã tìm thấy câu hỏi cũ này khi tham gia Java và cố gắng giải quyết cùng một điều. Vì tôi muốn cảnh ghi nhớ nội dung giữa các công tắc, tôi không thể sử dụng câu trả lời được chấp nhận, bởi vì khi chuyển đổi giữa các cảnh, nó sẽ hiển thị lại chúng (mất trạng thái trước đó).

Dù sao câu trả lời được chấp nhận và answer to the similar question đã cho tôi một gợi ý về cách chuyển cảnh mà không làm mất trạng thái của chúng. Ý tưởng chính là đưa trường hợp của cảnh vào bộ điều khiển khác, để bộ điều khiển không cần phải tạo ra một cảnh mới lặp đi lặp lại nhưng có thể sử dụng cá thể đã tồn tại (với trạng thái của nó).

Vì vậy, đây là lớp chính mà instantiates cảnh:

public class Main extends Application { 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     // getting loader and a pane for the first scene. 
     // loader will then give a possibility to get related controller 
     FXMLLoader firstPaneLoader = new FXMLLoader(getClass().getResource("firstLayout.fxml")); 
     Parent firstPane = firstPaneLoader.load(); 
     Scene firstScene = new Scene(firstPane, 300, 275); 

     // getting loader and a pane for the second scene 
     FXMLLoader secondPageLoader = new FXMLLoader(getClass().getResource("secondLayout.fxml")); 
     Parent secondPane = secondPageLoader.load(); 
     Scene secondScene = new Scene(secondPane, 300, 275); 

     // injecting second scene into the controller of the first scene 
     FirstController firstPaneController = (FirstController) firstPaneLoader.getController(); 
     firstPaneController.setSecondScene(secondScene); 

     // injecting first scene into the controller of the second scene 
     SecondController secondPaneController = (SecondController) secondPageLoader.getController(); 
     secondPaneController.setFirstScene(firstScene); 

     primaryStage.setTitle("Switching scenes"); 
     primaryStage.setScene(firstScene); 
     primaryStage.show(); 
    } 
} 

Và đây là các bộ điều khiển cả hai:

public class FirstController { 

    private Scene secondScene; 

    public void setSecondScene(Scene scene) { 
     secondScene = scene; 
    } 

    public void openSecondScene(ActionEvent actionEvent) { 
     Stage primaryStage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow(); 
     primaryStage.setScene(secondScene); 
    } 
} 

vâng, thứ hai trông giống nhau (một số logic có thể có thể được chia sẻ , nhưng trạng thái hiện tại đủ để chứng minh khái niệm)

public class SecondController { 

    private Scene firstScene; 

    public void setFirstScene(Scene scene) { 
     firstScene = scene; 
    } 

    public void openFirstScene(ActionEvent actionEvent) {  
     Stage primaryStage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow(); 
     primaryStage.setScene(firstScene); 
    } 
} 
0

Bạn có thể thử như thế này quá.

public void onBtnClick(ActionEvent event) { 
    try { 
     FXMLLoader loader = new FXMLLoader(getClass().getResource("login.fxml")); 
     Stage stage = (Stage) btn.getScene().getWindow(); 
     Scene scene = new Scene(loader.load()); 
     stage.setScene(scene); 
    }catch (IOException io){ 
     io.printStackTrace(); 
    } 

} 
Các vấn đề liên quan