fxml上Slider的标签

时间:2019-03-11 12:15:24

标签: java javafx-2

我是javafx的新手,无法完全从fxml的Slider获取标签,我尝试通过main方法和controller方法进行操作,但我不知道使用哪个标签调用onDrag *方法不起作用如果没有成功,将不胜感激,在此先感谢您的帮助。fxml代码如下


import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;

import javax.swing.*;

public class Controller {
    @FXML
    private TextField amount;
    @FXML
    private Label percent;
    @FXML
    private Slider slider;
    @FXML
    private TextField tip;
    @FXML
    private TextField total;
    @FXML
    private Button button;
    @FXML
    private Button clear;

    public void initialize() {
        button.setDisable(true);
        clear.setDisable(true);
    }

    @FXML
    public void keyHandle() {
        String text = amount.getText();
        boolean disableButton = (text.isEmpty() || 
        text.trim().isEmpty()) && (tip.getText().isEmpty() ||
                tip.getText().trim().isEmpty());
        button.setDisable(disableButton);
    }

    @FXML
    public void changeStateOfLabel() {
        slider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> 
            observable, Number oldValue, Number newValue) {
                percent.textProperty().setValue((int) slider.getValue() 
                + "%");
            }
        });

    }

    @FXML
    public void calculate(ActionEvent event) {
        try {
            int intAmount = Integer.valueOf(amount.getText());
            int tipAmount = Integer.valueOf(tip.getText());
            double intSlider = slider.valueProperty().getValue();
            double calculator = (intAmount + tipAmount * intSlider / 
            100);
            total.setText(String.valueOf(calculator));
            button.setDisable(true);
            clear.setDisable(false);
            if (slider.getValue() != intSlider) {
                intSlider = slider.getValue();
                calculator = (intAmount + tipAmount * intSlider / 100);
                button.setDisable(false);
                total.setText(String.valueOf(calculator));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Invalid Input");
        }
    }

    public void clear(ActionEvent event) {
        slider.setValue(0);
        total.clear();
        amount.clear();
        tip.clear();
        button.setDisable(true);
        clear.setDisable(true);
    }
}

0 个答案:

没有答案