Я создаю программу с использованием javafx и scenebuilder, которая сначала отображает арифметический калькулятор с кнопками, помеченными как History и Trigonometry. При нажатии на кнопку «Тригонометрия» открывается другое окно - тригонометрический калькулятор, который в свою очередь имеет кнопки «История» и «Арифметика».
Теперь, щелкнув «Журнал» в любом из двух калькуляторов, следует открыть окно «Журнал», в котором имеется полоса прокрутки, отображающая операции, хранящиеся в переменной «Результат» на соответствующих контроллерах
Я успешно создал окно иТеперь я не уверен, как я перейду и вызову их для методов showHistory в основном
Вот код для основного класса
public class Main extends Application {
private Stage primaryStage;
private Scene ArithmeticCalc;
private Scene TrigoCalc;
private Scene History;
private Scene History2;
static Stage Trig;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
primaryStage.setTitle("Calculator");
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/ArithmeticCalculator.fxml"));
AnchorPane inputViewFXML = (AnchorPane) loader.load();
Scene scene = new Scene(inputViewFXML);
ArithmeticCalc = scene;
primaryStage.setScene(scene);
primaryStage.show();
ArithmeticController inputViewController = loader.getController();
inputViewController.setMain(this);
FXMLLoader Trigloader = new FXMLLoader();
Trigloader.setLocation(Main.class.getResource("view/TrigonometricCalculator.fxml"));
AnchorPane TriginputViewFXML = (AnchorPane) Trigloader.load();
TrigoCalc = new Scene(TriginputViewFXML);
TrigonometricController TriginputViewController = Trigloader.getController();
TriginputViewController.setMain(this);
} catch(Exception e) {
e.printStackTrace();
}
}
public void showArithmetic() {
primaryStage.setScene(ArithmeticCalc);
primaryStage.show();
}
public void showArithmeticHistory(){
try {
FXMLLoader Historyloader = new FXMLLoader();
Historyloader.setLocation(Main.class.getResource("view/History.fxml"));
AnchorPane secondaryStageFXML = (AnchorPane) Historyloader.load();
Scene Historyscene = new Scene(secondaryStageFXML);
History = Historyscene;
Stage secondaryStage = new Stage();
//ArithmeticController result = loader.<ArithmeticController>getController();
//result.initData(str);
secondaryStage.initModality(Modality.WINDOW_MODAL);
secondaryStage.initOwner(primaryStage);
secondaryStage.setScene(History);
secondaryStage.show();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void showTrig() {
primaryStage.setScene(TrigoCalc);
primaryStage.show();
}
public void showTrigHistory(){
try {
FXMLLoader Historyloader = new FXMLLoader();
Historyloader.setLocation(Main.class.getResource("view/History.fxml"));
AnchorPane secondaryStageFXML = (AnchorPane) Historyloader.load();
Scene Historyscene = new Scene(secondaryStageFXML);
Stage secondaryStage = new Stage();
secondaryStage.initModality(Modality.WINDOW_MODAL);
secondaryStage.initOwner(primaryStage);
secondaryStage.setScene(Historyscene);
secondaryStage.show();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Вот для Арифметического Контроллера, которыйустановка аналогична тригонометрическому контроллеру
public class ArithmeticController {
private Main main;
private ArrayList<String> fieldList = new ArrayList<String>(100);
@FXML
private String HistoryTextField;
@FXML
private TextField Number1;
@FXML
private TextField Number2;
@FXML
private Button Add;
@FXML
private Button Multiply;
@FXML
private Button Subtract;
@FXML
private Button Divide;
@FXML
private TextField Result;
@FXML
private Button Trig;
@FXML
private Button Hist;
private String str;
@FXML
void Addition() {
Double a = Double.parseDouble(Number1.getText());
Double b = Double.parseDouble(Number2.getText());
Double sum = a + b;
Result.setText(a + " " + "+" + " " + b + " " + "=" + " " + sum.toString());
String str = Result.getText();
fieldList = new ArrayList<String>(Arrays.asList(str));
}
@FXML
void Division() {
Double a = Double.parseDouble(Number1.getText());
Double b = Double.parseDouble(Number2.getText());
Double quotient = a/b;
Result.setText(a + " " + "/" + " " + b + " " + "=" + quotient.toString());
String str = Result.getText();
fieldList = new ArrayList<String>(Arrays.asList(str));
}
@FXML
void Multiplication() {
Double a = Double.parseDouble(Number1.getText());
Double b = Double.parseDouble(Number2.getText());
Double product = a * b;
Result.setText(a + " " + "*" + " " + b + " " + "=" + product.toString());
String str = Result.getText();
fieldList = new ArrayList<String>(Arrays.asList(str));
}
@FXML
void Subtraction() {
Double a = Double.parseDouble(Number1.getText());
Double b = Double.parseDouble(Number2.getText());
Double difference = a - b;
Result.setText(a + " " + "-" + " " + b + " " + "=" + difference.toString());
String str = Result.getText();
fieldList = new ArrayList<String>(Arrays.asList(str));
}
@FXML
void Trigonometry() {
try {
main.showTrig();
} catch(Exception e) {
e.printStackTrace();
}
}
public void getList(ArrayList<String> fieldList)
{
this.fieldList = fieldList;
String str[] = new String[fieldList.size()];
for (int i=0; i<fieldList.size(); i++) {
str[i] = fieldList.get(i);
}
}
@FXML
void History() {
main.showArithmeticHistory();
}
public void setMain(Main main) {
this.main = main;
// TODO Auto-generated method stub
}
Вот код для контроллера истории
public class HistoryController {
private Main main;
@FXML
private TextField Result;
@FXML
private Button close;
@FXML
void Close() {
Stage stage = (Stage) close.getScene().getWindow();
stage.close();
}
public void setMain(Main main) {
this.main = main;
// TODO Auto-generated method stub
}
}
Пример вывода для отображения в истории (если используется арифметический калькулятор):
2,0 * 3,0 = 6,0
1,5 + 2,5 = 4,0
... и т. Д.