我是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), 符号:方法millis(int) 位置:课程时长 1个错误
这是它的完整代码,只想测试时间线,为事件创建计时器。
在这种情况下,我尝试添加一个简单的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);
}
}
答案 0 :(得分:4)
非常感谢@kleopatra和@Pagbo,我才意识到我导入了错误的类。 我的坏事..:)
import java.time.Duration;
到
import javafx.util.Duration;