2012-10-09 11 views
10

अरे मैंने थोड़ी देर के लिए नेट की खोज की लेकिन मुझे निम्न समस्या का समाधान नहीं मिला:नियंत्रक-वर्ग में javafx-application में स्क्रीन कैसे स्वैप करें?

javafx में आपको 3 मूलभूत फ़ाइलें मिलीं; नियंत्रक वर्ग, एफएक्सएमएल फ़ाइल और अनुप्रयोग वर्ग। अब मैं नियंत्रक में एक बटन-क्लिक (जो पूरी तरह से ठीक काम करता है) पर प्रतिक्रिया करना चाहता हूं और उस क्लिक पर स्क्रीन को बदलें (जिसे आप आमतौर पर मंच.सेटस्क्रीन() के साथ करते हैं), लेकिन मेरे पास मंच का कोई संदर्भ नहीं है (जो आप आवेदन कक्षा में मिल सकता है)।

आवेदन-नमूना:

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 
}  
} 

मैं मदद के किसी भी प्रकार के लिए आभारी होंगे।

उत्तर

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

धन्यवाद एक बहुत है कि मैं :) –

+0

के लिए वास्तव में क्या देख रहा था कि काफी मददगार था था! धन्यवाद :) –

0

मुझे जावा में आने और एक ही चीज़ को हल करने का प्रयास करते हुए यह पुराना प्रश्न मिला। चूंकि मैं दृश्यों के बीच सामग्री को याद रखने के दृश्यों को देखना चाहता था, इसलिए मैं स्वीकार्य उत्तर का उपयोग नहीं कर सका, क्योंकि दृश्यों के बीच स्विच करते समय यह उन्हें फिर से चालू करता है (उनके पिछले राज्य को खो देता है)।

वैसे भी स्वीकृत उत्तर और answer to the similar question ने मुझे अपने राज्य खोने के बिना दृश्यों को स्विच करने के बारे में एक संकेत दिया। मुख्य विचार दृश्य के उदाहरण को किसी अन्य नियंत्रक में इंजेक्ट करना है, ताकि नियंत्रक को बार-बार एक नए दृश्य को तुरंत चालू करने की आवश्यकता न हो लेकिन पहले से ही मौजूदा उदाहरण (इसके राज्य के साथ) का उपयोग कर सकें।

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

और यहाँ दोनों नियंत्रकों:

तो यहाँ मुख्य वर्ग है कि दृश्यों को दर्शाता है

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

हां, दूसरा एक ही लग रहा है (कुछ तर्क शायद साझा किया जा सकता है , लेकिन वर्तमान स्थिति अवधारणा के सबूत के रूप में पर्याप्त है)

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

आप इस तरह से कोशिश कर सकते हैं भी।

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

} 
संबंधित मुद्दे