我有一个有两个阶段的JavaFx项目。主舞台有能力启动第二阶段。在第二阶段,我有一个文本字段我想将一些数据放入文本字段并将其发送到主舞台并将其设置为标签。这是我的代码: 主要
package sample;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
主控制器:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private javafx.scene.control.Button kbutton;
@FXML
private javafx.scene.control.Button obutton;
@FXML
public Label label;
public TextField popField;
@FXML
public String text;
@FXML
private void konfButton() throws Exception{
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/konfview.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();
} catch(Exception e) {
e.printStackTrace();
}
}
public void myFunction( String text){
popField.setText(text);
}
@FXML
private void oblButton() throws Exception{
try {
// get a handle to the stage
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/sample.fxml"));
Parent root = (Parent) fxmlLoader.load();
Controller Controller=fxmlLoader.getController();
Stage stage = (Stage) obutton.getScene().getWindow();
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
MAIN FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
</rowConstraints>
<children>
<Pane prefHeight="400.0" prefWidth="600.0">
<children>
<Label layoutX="69.0" layoutY="81.0" text="previous reading" />
<Label layoutX="69.0" layoutY="115.0" text="current reading" />
<Label layoutX="71.0" layoutY="154.0" text="months" />
<Label layoutX="108.0" layoutY="14.0" text="Price">
<font>
<Font name="Comic Sans MS" size="18.0" />
</font>
</Label>
<Label layoutX="97.0" layoutY="51.0" text="Give details">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Label>
<Button fx:id="kbutton" layoutX="74.0" layoutY="197.0" mnemonicParsing="false" onAction="#konfButton" text="Configure" />
<Button fx:id="obutton" layoutX="197.0" layoutY="197.0" mnemonicParsing="false" onAction="#oblButton" text="OK" />
<TextField fx:id="popField" layoutX="172.0" layoutY="77.0" />
<TextField fx:id="bieField" layoutX="172.0" layoutY="111.0" />
<TextField fx:id="mcField" layoutX="172.0" layoutY="150.0" />
<Label fx:id="label" layoutX="119.0" layoutY="245.0" prefHeight="37.0" prefWidth="333.0" text="text" />
</children>
</Pane>
</children>
</GridPane>
第二控制器:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
public class ControllerConf {
@FXML
public TextField palField;
public Label label;
public String text;
@FXML
private TextField dysField1;
@FXML
private javafx.scene.control.Button cancelB;
@FXML
public javafx.scene.control.Button saveB;
@FXML
private void cancelButton(){
// get a handle to the stage
Stage stage = (Stage) cancelB.getScene().getWindow();
// do what you have to do
stage.close();
}
@FXML
public Stage saveButton(){
try {
FXMLLoader loader=new
FXMLLoader(getClass().getResource("/sample/sample.fxml"));
Parent root = (Parent) loader.load();
Controller Controller=loader.getController();
Controller.myFunction(palField.getText());
Stage primaryStage = (Stage) saveB.getScene().getWindow();
primaryStage.close();
return primaryStage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
SECOND FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerConf">
<children>
<Label layoutX="185.0" layoutY="28.0" prefHeight="17.0" prefWidth="314.0" text="Give details">
<font>
<Font name="Comic Sans MS" size="20.0" />
</font>
</Label>
<Label layoutX="48.0" layoutY="95.0" text="Price pre KWh" />
<Label layoutX="48.0" layoutY="135.0" text="Price pre item" />
<TextField fx:id="palField" layoutX="252.0" layoutY="91.0" onAction="#saveButton" />
<TextField fx:id="dysField" layoutX="252.0" layoutY="131.0" />
<Button fx:id="cancelB" layoutX="159.0" layoutY="208.0" mnemonicParsing="false" onAction="#cancelButton" text="Cancel">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Button>
<Button fx:id="saveB" layoutX="292.0" layoutY="209.0" mnemonicParsing="false" onAction="#saveButton" text="Save">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Button>
</children>
</AnchorPane>
我创建了MyFunction wchich set text,它在创建新舞台时工作正常但不适用于主舞台。你知道如何解决它吗?在此先感谢。输入代码