从jar文件运行时,计时器不启动

时间:2017-02-18 18:49:40

标签: java intellij-idea javafx

我试图让一个小应用程序同时ping多个服务器 当我在Intellij中运行它时它运行正常,但是当我将它构建到jar中时,ui启动但是Timer' Timeline'不开始,我试图改变时间轴与计时器,但同样的问题发生

**

这是Javafx控制器代码

**

import com.sun.deploy.util.StringUtils;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.shape.Circle;
import javafx.scene.control.TextField;
import javafx.util.Duration;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;

public class Controller  {

@FXML private TextField text1;
@FXML private TextField text2;
@FXML private TextField text3;
@FXML private TextField text4;
@FXML private Circle red1;
@FXML private Circle red2;
@FXML private Circle red3;
@FXML private Circle red4;
@FXML private Circle Green1;
@FXML private Circle Green2;
@FXML private Circle Green3;
@FXML private Circle Green4;
@FXML private Label label;

public boolean[] RED = new boolean[4];
public boolean[] GREEN = new boolean[4];
public Timer time ;

public void startPing(ActionEvent actionEvent) {

    Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), ev ->            {


            final Task<Void> task = new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    try {

                        String[] res = new String[4];
                        res[0] = PingIpAddr(text1.getText());
                        res[1] = PingIpAddr(text2.getText());
                        res[2] = PingIpAddr(text3.getText());
                        res[3] = PingIpAddr(text4.getText());

                        res[0] = StringUtils.splitString(res[0], " ")[0];
                        res[1] = StringUtils.splitString(res[1], " ")[0];
                        res[2] = StringUtils.splitString(res[2], " ")[0];
                        res[3] = StringUtils.splitString(res[3], " ")[0];

                        for (int x = 0 ; x<=3 ; x++) {
                            if (res[x].intern() == "Reply") {
                                RED[x] = false;
                                GREEN[x] = true;
                            } else {
                                RED[x] = true;
                                GREEN[x] = false;
                            }
                        }

                        Platform.runLater(new Runnable() {

                            @Override
                            public void run() {
                                label.setText("Running");
                                red1.setVisible(RED[0]);
                                Green1.setVisible(GREEN[0]);
                                red2.setVisible(RED[1]);
                                Green2.setVisible(GREEN[1]);
                                red3.setVisible(RED[2]);
                                Green3.setVisible(GREEN[2]);
                                red4.setVisible(RED[3]);
                                Green4.setVisible(GREEN[3]);
                            }
                        });



                    }
                    catch (Exception e){
                        Alert alert = new Alert(Alert.AlertType.ERROR, e.toString());
                        alert.showAndWait();
                    }


                    return null;
                }
            };
            new Thread(task).start();
        }));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();

}







public void stopPing(ActionEvent actionEvent) {
    //Not Implemented yet
}

public static String PingIpAddr(String ip) throws IOException
{   int counter = 0 ;
    ProcessBuilder pb = new ProcessBuilder("ping", ip);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(pb.start().getInputStream()));

    String line;
    while ((line = stdInput.readLine()) != null)
    {   counter++;

        if (counter == 3 ){

            return line;

        }

    }
    return " "    ;
}


}

Program when executed from Intellij

0 个答案:

没有答案