我試圖用兩個屏幕構成一個使用SceneBuilder和FXML的概念程序的簡單證明。第一個屏幕只是一個文本字段和一個按鈕,可將您帶到第二個屏幕,而第二個屏幕只有一個標籤,理想情況下,該按鈕將在按下按鈕時顯示文本字段內的任何內容。每個屏幕都有自己的FXML文件和它自己的控制器。我已經閱讀了關於FXMLLoader的上下左右,因爲我的研究指出這是完成這項工作的理想方式,但我仍然無法辨別如何正確使用它。最終,我希望通過角色扮演遊戲的一種「角色創造」遊戲前屏幕來實現這一點,其中玩家統計卷/庫存選擇從初始屏幕移至計算/處理模型或到第二屏幕控制器進行顯示。在JavaFX/FXML/SceneBuilder中屏幕之間傳遞參數
0
A
回答
-1
好的,這是一個類似的答案,忍受着我,我發現了一個Sahil Huseynzade的視頻,我認爲這個視頻最終爲我做了點擊。我在想,我可以使用FXMLLoader作爲幾乎各種導入,我可以在創建我的控制器時以某種方式使用,以便隨意訪問其他控制器。我在下面複製了一些我使用FXMLLoader的代碼,其中有一些關於我認爲FXMLLoader的工作原理的評論。對不起,如果從技術角度來看,解釋是鈍的或措辭不當,或者如果我在所有這些方面都是一般的新手/笨蛋。但我還有一個問題,我期望在答案中提出這種問題的方式很糟糕,是否有辦法讓信息在整個窗口中動態更新?像我可以有一個窗口有一個骰子滾動按鈕,誰的結果自動出現在另一個窗口?
package twoscreentest;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Screen1Controller implements Initializable {
@FXML
public Label label;
@FXML
public Button button;
@FXML
private TextField textCharName;
public String testtext;
@Override
public void initialize(URL url, ResourceBundle rb) {
label.setText(Byte.toString((byte) (Math.floor(Math.random()*6)+1)));
}
@FXML
private void btnSend(ActionEvent event) throws IOException {
PlayerInfo player = new PlayerInfo(textCharName.getText(), Double.parseDouble(label.getText())); // Creating the temporary container.
//((Node)event.getSource()).getScene().getWindow().hide(); This is to close the previous window, for testing it was easier to disable it.
FXMLLoader loader = new FXMLLoader(); // Creating a the FXMLLoader
loader.setLocation(getClass().getResource("Screen2.fxml")); // Telling it to get the proper FXML file.
loader.load(); // Loading said FXML.
Parent p = loader.getRoot(); // Setting up the window, I think I get how this works,
//probably...
Stage stage = new Stage();
stage.setScene(new Scene(p));
Screen2Controller screen2controller = loader.getController(); // This right here I
//don't entirely get, I know that I'm using the loader to get the controller, but
//what's with the "NameofControllerClass variablename"?
screen2controller.setCurrentInfo(player); // Setting the empty container in
// the second class' variables to the ones in the the temporary container that
// I created earlier.
screen2controller.testlabel.setText(testtext); // An additional test, setting a label
// on the second screen to a variable I set in this controller with a separate button.
stage.show(); // Create the second screen.
}
@FXML
private void btnSend2(ActionEvent event) {
testtext = "Hello world";
}
}
相關問題
- 1. React Native:在屏幕之間傳遞數據
- 2. 如何使用mvvmcross在屏幕之間傳遞數據
- 3. 在Symbian/QML應用程序中屏幕之間傳遞狀態
- 4. 在函數之間傳遞參數
- 5. 在knitr之間傳遞圖形參數在塊中傳遞
- 6. 在網頁之間傳遞參數
- 7. Struts - 在動作之間傳遞參數
- 8. 在託管bean之間傳遞參數
- 9. 在JSP之間傳遞參數
- 10. Java在方法之間傳遞參數
- 11. 在Flink作業之間傳遞參數
- 12. 在qt進程之間傳遞參數?
- 13. 在頁面之間傳遞jQuery參數
- 14. 在Jade模板之間傳遞參數
- 15. 在兩頁之間傳遞參數html
- 16. 在.NET和PLSQL之間傳遞參數
- 17. 在JSF頁面之間傳遞參數
- 18. Android:在類之間傳遞參數
- 19. 在WordPress頁面之間傳遞參數
- 20. 在jsp和xsl之間傳遞參數
- 21. 在managedbeans primefaces之間傳遞參數
- 22. 在屏幕之間傳遞一個列表。 Windows Phone 8.1 C#
- 23. 在React導航的不同屏幕之間傳遞TextInput道具
- 24. 每個函數之間傳遞參數
- 25. Android和JNI之間的參數傳遞
- 26. html頁面之間傳遞參數
- 27. Webapp2重定向之間傳遞參數
- 28. 參數XAML之間傳遞的Windows Phone
- 29. 咕嚕任務之間傳遞參數
- 30. 在屏幕之間滑動
最初打開2個屏幕?或者第二個屏幕在按鈕上點擊打開? – Calips
第二個屏幕設置爲在點擊按鈕時打開。 – Fgsfds
請參閱http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310 –