0
你好,我從JavaFX開始,我遇到了一些問題。 我想從兩個diferents的意見溝通兩個控制。我該怎麼做? 我使用標籤和我有這兩個控制器和我想要做類似這樣的:JavaFX。控制器之間的通信
Application.java:
public class JavaFXApplication6 extends Application
{
@Override
public void start(Stage primaryStage)
{
try
{
Parent root = FXMLLoader.load(getClass().getResource(
"/view/FXML_Main.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e){
e.printStackTrace();
}
primaryStage.setTitle("Back-end GUI");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
FXML_Main.fxml:
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"
fx:controller="controller.FXML_MainController">
<children>
<TabPane layoutX="61.0" layoutY="30.0" prefHeight="400.0" prefWidth="600.0"
tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="Untitled Tab 1">
<content>
<fx:include source="FXML_Tab1.fxml" />
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<fx:include source="FXML_Tab2.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
FXML_MainController.java:
public class FXML_MainController implements Initializable {
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
FXML_Tab1.fx毫升:
<?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="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.FXML_Tab1Controller">
<children>
<Label fx:id="Label1" layoutX="282.0" layoutY="108.0" text="Label" />
<TextField fx:id="FextField1" layoutX="215.0" layoutY="146.0" />
<Button fx:id="Button1" layoutX="269.0" layoutY="197.0" mnemonicParsing="false"
onAction="#actionButton1" text="Button" />
</children>
</AnchorPane>
FXML_Tab1Controller.java:
public class FXML_Tab1Controller implements Initializable {
FXML_Tab2Controller tab2controller;
@FXML public Label Label1;
@FXML public TextField TextField1;
@FXML public Button Button1;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML private void actionButton1(ActionEvent event)
{
Label1.setText(tab2controller.TextField2.getText());
}
}
FXML_Tab2.fxml:
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.FXML_Tab2Controller">
<children>
<Label fx:id="Label2" layoutX="282.0" layoutY="99.0" text="Label" />
<TextField fx:id="TextField2" layoutX="215.0" layoutY="149.0" />
<Button fx:id="Button2" layoutX="270.0" layoutY="200.0" mnemonicParsing="false"
onAction="#actionButton2" text="Button" />
</children>
</AnchorPane>
FXML_Tab2Controller.java:
public class FXML_Tab2Controller implements Initializable {
FXML_Tab1Controller tab1controller;
@FXML public Label Label2;
@FXML public TextField TextField2;
@FXML public Button Button2;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML private void actionButton2(ActionEvent event){
Label2.setText(tab1controller.TextField1.getText());
}
}
類似這樣的東西視頻: https://www.youtube.com/watch?v=XLVx46ycxco
正如一個評論,我真的不建議共享控制器是這樣的:你應該在封裝數據一個使用JavaFX可觀察屬性的數據模型,並在兩個控制器之間共享數據模型。這爲您的控制器提供了更好的解耦。 – 2014-10-27 15:31:12
我嘗試做第一件事,但它不起作用。 – Charlio 2014-10-28 10:02:39
詳細說明「不起作用」 – 2014-10-28 10:38:25