Доброго времени суток. Имеется три класса. Menu1 - отрисовка основного меню, Box1 - выпадающее окна, при нажатии на Data в меню и Chart1 - рисует график по передаваемым значениям из Box1.
С помощью вот этого выражения:
ok.setOnAction(e -> chart.setPopulation(Integer.parseInt(populationInput.getText())));
Я передаю полученное значение в Chart1 где ловлю его вот этим сеттером:
public void setPopulation(int peoples) {
G0 = peoples;
System.out.println("Set G0 = " + G0);
}
В консоле исправно выдаётся, что значение получено и пишется:
Set G0 = 88.0
Но когда я хочу построить характеристику, я обнаруживую, что значение моих переменных Go и Gэ хранят ноль. Как это получается и что можно с этим сделать?
public class Menu1 extends Application {
Stage window;
BorderPane layout;
Chart1 chart = new Chart1();
String tractionChar = "tractionChar";
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("Test");
Menu data = new Menu("Data");
MenuItem data1 = new MenuItem("Data1");
Menu functions = new Menu("Functions");
MenuItem func3 = new MenuItem("Func3");
Menu launch = new Menu("Launch");
data.getItems().addAll(data1);
functions.getItems().addAll(func3);
MenuBar menuBar = new MenuBar();
TextField nameInput = new TextField();
menuBar.getMenus().addAll(data,functions,launch);
data1.setOnAction(e -> System.out.println(nameInput.getText()));
func3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
chart.start(tractionChar);
}
});
data1.setOnAction(e -> {
Box1.display();
});
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setMinHeight(250);
layout = new BorderPane();
layout.setTop(menuBar);
Scene scene = new Scene(layout, 400, 300);
window.setScene(scene);
window.show();
}
}
public class Box1 {
public static void display() {
Chart1 chart = new Chart1();
Stage window = new Stage();
window.setTitle("Исходные данные");
GridPane grid = new GridPane();
grid.setPadding(new Insets(5,5,5,5));
grid.setVgap(10);
grid.setHgap(10);
Label populationLabel = new Label("Населенность");
GridPane.setConstraints(populationLabel, 0,3);
grid.getChildren().add(populationLabel);
TextField populationInput = new TextField("88");
GridPane.setConstraints(populationInput,2,3);
grid.getChildren().add(populationInput);
Button cancel = new Button("Закрыть");
GridPane.setConstraints(cancel, 2,8);
grid.getChildren().add(cancel);
cancel.setOnAction(e -> window.close());
Button ok = new Button("Ok");
GridPane.setConstraints(ok, 1,8);
grid.getChildren().add(ok);
ok.setOnAction(e -> chart.setPopulation(Integer.parseInt(populationInput.getText())));
window.close();
Scene scene = new Scene(grid, 305, 250);
window.setScene(scene);
window.show();
}
}
class Chart1 {
public static final String tractionChar = "tractionChar";
public void setPopulation(int peoples) {
G0 = peoples;
System.out.println("Set G0 = " + G0);
}
public double getPopulation() {
System.out.println("Get G0 = " + G0);
return this.G0;
}
private double G0 = getPopulation();
private double Gэ = G0*40;
double kп = 1.6;
double kpm = 0.955;
int Vp = 60;
double γ = 1.06;
double асрп = 1.2;
double power = (Gэ*γ*асрп*Vp)/(3.6*kpm*kп);
public double[] tractionCharacteristic() {
System.out.println("G0 " + G0);
System.out.println("Gэ " + Gэ);
System.out.println("γ " + γ);
System.out.println("асрп " + асрп);
System.out.println("Vp " + Vp);
System.out.println("kpm " + kpm);
System.out.println("kп " + kп);
double[] traction = new double[12];
for (int i = 0; i < 12; i++) {
if (i <= Vp) {
traction[i] = power * 3.6 * kpm / Vp;
} else {
traction[i] = power * 3.6 * kpm / i;
}
}
return traction;
}
public void start(String func) {
Stage stage = new Stage();
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis);
XYChart.Series series = new XYChart.Series();
if (func.equals(tractionChar)) {
for (int i = 0; i < 12; i++) {
series.getData().add(new XYChart.Data(i, tractionCharacteristic()[i]));
}
}
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().add(series);
stage.setScene(scene);
stage.setScene(scene);
stage.showAndWait();
}
}
Сборка персонального компьютера от Artline: умный выбор для современных пользователей