Я пытаюсь сделать приложение по типу счётчика калорий: в первом окне я вписываю текущий рост и вес, а во втором окне в текстовом поле Label я должен вывести "Ваш текущий рост и вес: " + Height + " см и " + Weight + " кг.". К сожалению, текст не устанавливается, в чём причина?
FXML файл первого окна:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="480.0" prefWidth="720.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.heightAndWeightController">
<children>
<Label layoutX="255.0" layoutY="26.0" prefHeight="62.0" prefWidth="211.0" text="Введите Ваш рост и вес">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField fx:id="fieldHeight" layoutX="274.0" layoutY="124.0" prefHeight="45.0" prefWidth="161.0" promptText="Рост" />
<TextField fx:id="fieldWeight" layoutX="274.0" layoutY="195.0" prefHeight="45.0" prefWidth="161.0" promptText="Вес" />
<Button fx:id="nextButton" layoutX="313.0" layoutY="310.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="83.0" text="Далее" />
</children>
</AnchorPane>
Контроллер первого окна:
package sample;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.scene.control.TextField;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.fxml.FXML;
import javafx.stage.Stage;
public class heightAndWeightController {
public int Weight;
public int Height;
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private TextField fieldHeight;
@FXML
private TextField fieldWeight;
@FXML
private Button nextButton;
@FXML
void initialize() {
nextButton.setOnAction(actionEvent -> {
Weight = Integer.parseInt(fieldWeight.getText());
Height = Integer.parseInt(fieldHeight.getText());
nextButton.getScene().getWindow().hide();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(heightAndWeightController.class.getResource("/sample/sample.fxml"));
try {
loader.load();
}catch (IOException e){
e.printStackTrace();
}
Parent root = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
stage.setResizable(false);
});
}
}
Контроллер окна, в котором должен выводится текст:
package sample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Controller extends heightAndWeightController{
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private Label currentHaWLable;
@FXML
void initialize() {
currentHaWLable.setText("Ваш текущий рост и вес: " + Height + " см и " + Weight + " кг.");
}
}
FXML файл окна, в котором должен выводится текст:
**<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="717.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="260.0" layoutY="38.0" prefHeight="50.0" prefWidth="109.0" text="Калории" textAlignment="CENTER">
<font>
<Font name="Segoe UI Italic" size="25.0" />
</font>
</Label>
<ProgressBar layoutX="260.0" layoutY="96.0" prefWidth="200.0" progress="0.0" />
<DatePicker layoutX="274.0" layoutY="124.0" />
<Label layoutX="133.0" layoutY="183.0" prefHeight="50.0" prefWidth="109.0" text="Завтрак" textAlignment="CENTER">
<font>
<Font name="Segoe UI Italic" size="25.0" />
</font>
</Label>
<Label layoutX="133.0" layoutY="281.0" prefHeight="50.0" prefWidth="109.0" text="Обед" textAlignment="CENTER">
<font>
<Font name="Segoe UI Italic" size="25.0" />
</font>
</Label>
<Label layoutX="133.0" layoutY="366.0" prefHeight="50.0" prefWidth="109.0" text="Ужин" textAlignment="CENTER">
<font>
<Font name="Segoe UI Italic" size="25.0" />
</font>
</Label>
<ScrollPane layoutX="260.0" layoutY="183.0" prefHeight="71.0" prefWidth="200.0" />
<ScrollPane layoutX="260.0" layoutY="270.0" prefHeight="71.0" prefWidth="200.0" />
<ScrollPane layoutX="260.0" layoutY="355.0" prefHeight="71.0" prefWidth="200.0" />
<Label layoutX="390.0" layoutY="35.0" prefHeight="57.0" prefWidth="117.0" text="1200/1800">
<font>
<Font name="System Italic" size="19.0" />
</font>
</Label>
<Label fx:id="currentHaWLable" layoutX="14.0" layoutY="13.0" prefHeight="50.0" prefWidth="200.0" text="" textAlignment="CENTER">
<font>
<Font name="Segoe UI Italic" size="12.0" />
</font>
</Label>
</children>
</AnchorPane>**
fxml
и в коде (что-то по типуloader.setController( new Controller() );
).import java.awt.*;
- вот этого в коде с javafx не должно быть. Из-за этого у вас контролы могут импортироваться из другого GUI фреймворка.