找不到Duration.millis符号?

问题描述 投票:-1回答:1

我是JavaFX的新手,特别是内置的API,即时间轴

但我想使用时间轴为事件添加计时器

                Timeline timeline;
                timeline = new Timeline(new KeyFrame(Duration.millis(2500),
                        (evt) -> {
                            System.out.println("hello");
                        }));
                timeline.setCycleCount(Animation.INDEFINITE);
                timeline.play();

错误:找不到符号Duration.millis(2500),symbol:method millis(int)location:class Duration 1 error

这是它的完整代码,只是想测试时间线的事情来为事件创建一个计时器。

在这种情况下,我尝试添加一个简单的println“Hello”来查看它是否有效,但事实并非如此。

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import javafx.scene.image.Image;
import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.ResourceBundle;
import java.util.TimerTask;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.Window;
import javax.swing.*;

public class Scene2Controller implements Initializable  {

@FXML
private JFXButton btn_submit;
@FXML
private JFXTextField txt_email;
@FXML
private JFXPasswordField txt_pass;
@FXML
private ImageView img1;
@FXML
private ImageView img2;
@FXML
private Pane pane_loader;

@FXML
private void handleButtonAction(ActionEvent event) throws IOException, Exception {
    if (event.getSource() == btn_submit) {
        if (txt_email.getText().equals("") && txt_pass.getText().equals("")) 
        {
            JOptionPane.showMessageDialog(null, "Please fill in the field", "Error", JOptionPane.ERROR_MESSAGE);
        } 
        else 
        {
            pane_loader.setVisible(true);
            pane_loader.toFront();
            if (txt_email.getText().equals("root") && txt_pass.getText().equals("test1234")) 
            {

                Timeline timeline;
                timeline = new Timeline(new KeyFrame(Duration.millis(2500l),
                        (evt) -> {
                            System.out.println("hello");
                        }));
                timeline.setCycleCount(Animation.INDEFINITE);
                timeline.play();
            }                          

            else 
                JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", JOptionPane.ERROR_MESSAGE);
        }

    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    Image image = new Image ("/image/business_care_clinic_1282308.jpg");
    Image image2 = new Image ("/image/815.gif");
    img1.setImage(image);
    img2.setImage(image2);

    }
}
java javafx duration milliseconds symbol-not-found
1个回答
4
投票

非常感谢@kleopatra和@Pagbo,我才意识到我导入了错误的类。我的错.. :)

import java.time.Duration;

import javafx.util.Duration;
© www.soinside.com 2019 - 2024. All rights reserved.