如何在主屏幕上从一个文本字段弹出窗口向另一个文本字段添加双精度。我有一个电影院系统,其中有一个显示总价的主页,还有一个弹出菜单。如何从酒水弹出窗口获取值并添加到主窗口的文本字段中。我给两个类分配了变量completetotal,当我单击“完成”按钮时,该变量应加在一起并关闭窗口。但是事实并非如此。
Drinkspopup的控制器
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import static sample.FilmsController.completetotal;
public class DrinksController {
public TextField watertf;
public TextField coketf;
public Button totalbut;
public TextField totaltf;
public Button finishdrinks2;
private FilmsController fc = new FilmsController();
/*changed button to private */
/*public TextField gettotaltf() {
return totaltf;
}
private FilmsController parent;*/
private Drinks drink = new Drinks();
/* public void setParent(FilmsController fc) {
// Remember the reference to the parent GUI, then we can call its methods
parent = fc;
}*/
/*added action event*/
public void addrinks(ActionEvent actionEvent) {
try {
int water = new Integer(watertf.getText());
int coke = new Integer(coketf.getText());
double result = water * (1.75) + coke * (1.25);
totaltf.setText("" + result);
completetotal += result;
} catch (NumberFormatException e) {
totaltf.setText("Not a Number");
}
}
/*This is supposed to add the drinks then add it to the variable complete total*/
public void finishdrinksclose2(ActionEvent actionEvent) {
int water = new Integer(watertf.getText());
int coke = new Integer(coketf.getText());
double result = water * (1.75) + coke * (1.25);
completetotal += result;
totaltf.setText("" + result);
Stage stage = (Stage) finishdrinks2.getScene().getWindow();
stage.close();
}
}
主屏幕Filmscontroller
public void comptotal(ActionEvent actionEvent) {
try {
int adult = new Integer(adulttf.getText());
int child = new Integer(childtf.getText());
double filmprice= new Double(filmpricetf.getText());
double result = adult * (filmprice) + child*(filmprice);
completetotal+= result;
totalpricetf.setText("" + result);
}
catch (NumberFormatException e) {
totalpricetf.setText("Not a Number");
}}
}
我只添加了一部分代码,这些代码在filmscontroller上执行了该程序。
答案 0 :(得分:0)
让我们想象一下,当您尝试调用popUp窗口时,这就是您的主屏幕
click.setOnAction(event -> {
FXMLLoader loader = new FXMLLoader();
try {
Parent parent = loader.load(getClass().getClassLoader().getResource("second.fxml").openStream());
Stage stage = new Stage();
stage.setScene(new Scene(parent));
stage.initModality(Modality.APPLICATION_MODAL);
Children children = loader.getController();
children.setController(this);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
});
让我们想象一下您的弹出窗口
@FXML
private JFXTextField field;
private Controller controller;
@FXML private Button cls;
@Override
public void initialize(URL location, ResourceBundle resources)
{
cls.setOnAction(event -> {
this.controller.setTextIntoTextField(field.getText());
Stage stage = (Stage)((Button)(event).getSource()).getScene().getWindow();
stage.close();
});
}
public void setController(Controller controller) {
this.controller = controller;
}
这是一种回调方法,可以帮助您加载应显示在主屏幕中的所有数据,更确切地说是从子级到父控制器类