/
Serge8114
/
Lab1
Обзор
Документация
Войти
/
Serge8114
/
Lab1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
ConverterController.java
84 строки
2 KB
Serge8114
upload files
27 фев 2026, 03:27
Верифицирован
27 фев 2026, 03:27
e722d30
Код
Авторство
О чём код?
package com.example.demo2; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.*; import java.net.URL; import java.util.ResourceBundle; public class ConverterController implements Initializable { // Связываем с элементами из FXML @FXML private TextField inputField; @FXML private ComboBox<String> fromComboBox; @FXML private ComboBox<String> toComboBox; @FXML private Label resultLabel; @FXML private Label errorLabel; @FXML private Button convertButton; private ConverterModel model; @Override public void initialize(URL url, ResourceBundle resourceBundle) { model = new ConverterModel(); String[] units = {"Обыкновенная дробь", "Десятичная дробь", "Проценты"}; fromComboBox.getItems().addAll(units); toComboBox.getItems().addAll(units); fromComboBox.setValue("Обыкновенная дробь"); toComboBox.setValue("Десятичная дробь"); inputField.textProperty().addListener((observable, oldValue, newValue) -> { errorLabel.setText(""); }); convertButton.setOnAction(event -> handleConvert()); } @FXML private void handleConvert() { try { errorLabel.setText(""); if (fromComboBox.getValue() == null || toComboBox.getValue() == null) { errorLabel.setText("Выберите единицы конвертации!"); return; } String inputText = inputField.getText().trim(); if (inputText.isEmpty()) { errorLabel.setText("Введите значение!"); return; } double result = model.convert( inputText, fromComboBox.getValue(), toComboBox.getValue() ); if (toComboBox.getValue().equals("Проценты")) { resultLabel.setText(String.format("%.2f%%", result)); } else { resultLabel.setText(String.format("%.4f", result)); } } catch (NumberFormatException e) { errorLabel.setText("Ошибка: введите число!"); } catch (IllegalArgumentException e) { errorLabel.setText("Ошибка: " + e.getMessage()); } } }