【问题标题】:javafx run one after the otherjavafx一个接一个地运行
【发布时间】:2018-12-18 12:53:11
【问题描述】:

我的代码有问题。 通过单击按钮,我想确定一个 IP 地址,并从该 IP 确定两个连续的 IP 地址,但我还想检查 IP 地址是否可访问。检查需要几秒钟。我希望显示 IP 地址,并且应该在后台进行检查。 我怎样才能做到这一点?

   public void GetIP() {
        String mn = tfmn.getText();
        String d = "cachea." + mn + ".de.kaufland";

        try {
            InetAddress i = InetAddress.getByName(d);
            int intIP = ByteBuffer.wrap(i.getAddress()).getInt();

            intIP += 1496;

            i = InetAddress.getByName(String.valueOf(intIP));
            String ip = i.getHostAddress();

            tfip1.setText(ip);

            //Check IP
            boolean reachable = i.isReachable(1000);
            if (reachable) {
                tfipinfo1.setText("IP-Addresse reachable");
                tfipinfo1.setStyle("-fx-text-fill: green;");
            } else {
                tfipinfo1.setText("IP-Adresse ist not reachable");
                tfipinfo1.setStyle("-fx-text-inner-color: red;");
            }

            // next IP
            intIP += 1;

            InetAddress i2 = InetAddress.getByName(String.valueOf(intIP));
            ip = i2.getHostAddress();
            tfip2.setText(ip);

            //Check IP-Address
            boolean reachable2 = i2.isReachable(1000);
            if (reachable2) {
                tfipinfo2.setText("IP-Adresse ist erreichbar");
                tfipinfo2.setStyle("-fx-text-fill: green;");
            } else {
                tfipinfo2.setText("IP-Adresse ist nicht erreichbar");
                tfipinfo2.setStyle("-fx-text-inner-color: red;");
            }

            //next IP
            intIP += 1;

            InetAddress i3 = InetAddress.getByName(String.valueOf(intIP));
            ip = i3.getHostAddress();
            tfip3.setText(ip);

            //check IP
            boolean reachable3 = i3.isReachable(1000);
            if (reachable3) {
                tfipinfo3.setText("IP-Adresse ist erreichbar");
                tfipinfo3.setStyle("-fx-text-fill: green;");
            } else {
                tfipinfo3.setText("IP-Adresse ist nicht erreichbar");
                tfipinfo3.setStyle("-fx-text-inner-color: red;");
            }

          } catch (UnknownHostException ex) {
            ex.printStackTrace();

            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error");
            alert.setHeaderText(null);
            alert.setContentText("Der Markt existiert nicht!");

            alert.showAndWait();

        } catch (IOException e) {
            e.printStackTrace();
        }
}

【问题讨论】:

  • 与您的问题无关:请学习 java 命名约定并遵守它们

标签: java button javafx javafx-8


【解决方案1】:

你做的:

ip = i2.getHostAddress();
tfip2.setText(ip);

//Check IP-Address
boolean reachable2 = i2.isReachable(1000);

所以顺序是

  • 获取地址
  • 显示地址
  • 查看地址

你只需要制作:

...
tfip1.setText(ip);

// next IP
intIP += 1;

InetAddress i2 = InetAddress.getByName(String.valueOf(intIP));
ip = i2.getHostAddress();
tfip2.setText(ip);
//Check IP
boolean reachable = i.isReachable(1000);
if (reachable) {
  tfipinfo1.setText("IP-Addresse reachable");
  tfipinfo1.setStyle("-fx-text-fill: green;");
} else {
  tfipinfo1.setText("IP-Adresse ist not reachable");
  tfipinfo1.setStyle("-fx-text-inner-color: red;");
}

//Check IP-Address
boolean reachable2 = i2.isReachable(1000);
if (reachable2) {
  tfipinfo2.setText("IP-Adresse ist erreichbar");
  tfipinfo2.setStyle("-fx-text-fill: green;");
} else {
  tfipinfo2.setText("IP-Adresse ist nicht erreichbar");
  tfipinfo2.setStyle("-fx-text-inner-color: red;");
}
...

你也可以在另一个线程中让你的 IP 地址检查器

你会得到类似的东西:

class IPChecker extends Task {
    @Override
    public Object call() throws IOException, InterruptedException{
        //your ip checker algo
        return null;
    }
}

并称之为:

...
tfip1.setText(ip);
new Thread(new IPChecker()).start();
...

所以你的遗嘱会像这样接缝:

public void GetIP() {
    String mn = tfmn.getText();
    String d = "cachea." + mn + ".de.kaufland";

    try {
        InetAddress i = InetAddress.getByName(d);
        int intIP = ByteBuffer.wrap(i.getAddress()).getInt();

        intIP += 1496;

        i = InetAddress.getByName(String.valueOf(intIP));
        String ip = i.getHostAddress();

        tfip1.setText(ip);

        //Check IP
        new Thread(new IPChecker1()).start();

        // next IP
        intIP += 1;

        InetAddress i2 = InetAddress.getByName(String.valueOf(intIP));
        ip = i2.getHostAddress();
        tfip2.setText(ip);

        //Check IP-Address
        new Thread(new IPChecker2()).start();

        //next IP
        intIP += 1;

        InetAddress i3 = InetAddress.getByName(String.valueOf(intIP));
        ip = i3.getHostAddress();
        tfip3.setText(ip);

        //check IP
        new Thread(new IPChecker3()).start();

      } catch (UnknownHostException ex) {
        ex.printStackTrace();

        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText(null);

你也可以定义另一个构造函数写new IPChecker(ip1)等等

【讨论】:

  • 更改顺序不起作用,我不知道如何用线程来做到这一点。可以举个例子吗?
  • @MarkusS 在这里
【解决方案2】:

我猜你想说的是

UI 需要在程序在后台检查 IP 地址时运行

这将使程序在检查 IP 地址期间无法保持。这可以而且应该通过单独的线程来实现,因为不建议使用主 JavaFX 线程来执行除处理 UI 之外的任务。

javafx.concurrent 包中提供了在 JavaFX 应用程序中实现并发所需的类。此解决方案中将使用的类是Task。下面给出了您的代码段的解决方案。

public class IPChecker extends Task<Void>{
    private final InetAddress i;
    private TextField tf;

    public IPChecker(InetAddress i,TextField tf) {
        this.i = i;
        this.tf=tf;
    }

    @Override
    protected Void call() throws Exception{
        boolean reachable = i.isReachable(1000);
        Platform.runLater(() -> {
            if (reachable) {
                tf.setText("IP-Addresse reachable");
                tf.setStyle("-fx-text-fill: green;");
            } else {
                tf.setText("IP-Adresse ist not reachable");
                tf.setStyle("-fx-text-inner-color: red;");
            }
        });
        return null;
    }
}

public void GetIP() {
    String mn = tfmn.getText();
    String d = "cachea." + mn + ".de.kaufland";

    try {
        InetAddress i = InetAddress.getByName(d);
        int intIP = ByteBuffer.wrap(i.getAddress()).getInt();

        intIP += 1496;

        i = InetAddress.getByName(String.valueOf(intIP));
        String ip = i.getHostAddress();

        tfip1.setText(ip);

        //Check IP
        Thread t1=new Thread(new IPChecker(i,tfipinfo1));
        t1.start();

        // next IP
        intIP += 1;

        InetAddress i2 = InetAddress.getByName(String.valueOf(intIP));
        ip = i2.getHostAddress();
        tfip2.setText(ip);

        //Check IP-Address
        Thread t2=new Thread(new IPChecker(i2,tfipinfo2));
        t2.start();

        //next IP
        intIP += 1;

        InetAddress i3 = InetAddress.getByName(String.valueOf(intIP));
        ip = i3.getHostAddress();
        tfip3.setText(ip);

        //check IP
        Thread t3=new Thread(new IPChecker(i3,tfipinfo3));
        t3.start();

    } catch (UnknownHostException ex) {
        ex.printStackTrace();

        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText(null);
        alert.setContentText("Der Markt existiert nicht!");

        alert.showAndWait();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

我创建了一个名为IPChecker 的类来执行需要在后台运行的操作。这个类需要你传递InetAddress 对象和要执行工作的TextField 对象。

为什么在call() 函数中调用Platform.runLater()

这样做是因为不建议在 JavaFX 应用程序线程之外的单独线程中执行 JavaFX UI 操作。因此,为了确保与 UI 相关的操作在 JavaFX 应用程序线程中执行,我们在 Platform.runLater() 中调用它们

参考文献

Task - A Task Which Modifies The Scene Graph

JavaFX: Interoperability

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多