我正在使用javafx和scenebuilder创建一个程序,该程序首先显示一个算术计算器,该计算器具有标记为“历史记录”和“三角函数”的按钮。单击三角函数将显示另一个窗口-三角计算器-依次具有“历史记录”和“算术”按钮。
现在,在两个计算器中的任意一个上单击“历史记录”,将打开“历史记录”窗口,该窗口具有一个滚动窗格,显示在各自控制器中存储在变量Result中的操作
我已经成功创建了窗口,现在不确定如何传递给主窗口中的showHistory方法并将其调用
这是主类的代码
app.MapWhen(context => {
return context.Request.Host.Value.StartsWith("photos.",StringComparison.OrdinalIgnoreCase)
} (appBuilder) =>
{
appBuilder.UseStaticfiles(new StaticfileOptions
{
FileProvider = blobFileProvider,OnPrepareResponse = ctx =>
{
const int durationInSeconds = 3600 * 72;
ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=" + durationInSeconds;
}
});
});
这里是用于算术控制器的,其设置类似于三角控制器
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
}
要在“历史记录”中显示的样本输出(如果是算术计算器):
2.0 * 3.0 = 6.0
1.5 + 2.5 = 4.0
...等等