javafx一个接一个地运行

时间:2018-12-18 12:53:11

标签: java button javafx javafx-8

我的代码有问题。 单击一个按钮,我想确定一个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();
        }
}

2 个答案:

答案 0 :(得分:0)

您制作:

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),依此类推

答案 1 :(得分:0)

我认为您想说的是

  

程序在后台检查IP地址时,用户界面需要起作用

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

#include <iostream> #include <memory> using namespace std; class A { public: A(int a):_a( make_shared<int>(a)) {cout <<"A constructor" << endl;} ~A(){/*_a.reset();*/cout <<"After destructor, number of As "<< _a.use_count() <<endl;} std::shared_ptr<int> a(){return _a;} //private: std::shared_ptr<int> _a; }; class B { public: B(A a):_b( make_shared<A>(a)) {cout <<"B constructor" << endl;} ~B(){ /*_b.reset();*/ cout <<"After destructor, number of Bs "<< _b.use_count() << endl;} std::shared_ptr<A> b(){return _b;} private: std::shared_ptr<A> _b; }; int main() { int number = 10; A a(number); cout <<"counter of A is " << a._a.use_count() << endl; cout <<"counter of A is " << a.a().use_count() << endl; B b(a); cout <<"counter of A is " << a.a().use_count() << endl; cout <<"counter of B is " << b.b().use_count() << endl; return 0; } A--->Number: Counter = 1 B(constructor) pass A by value : counter = 2 B--->Number: Counter = 3 包中提供了在JavaFX应用程序中实现并发所需的类。此解决方案中将使用的类为Sub Candace() Dim i As Long Dim r As Long Dim UsdRws As Long Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer Dim currentRowValue As String, lastrowCD As Long, lastrowOM As Long Dim lastrow As Long, wsCD As Worksheet, wsOM As Worksheet, j As Long Dim rngOp As Range, n As Long, rngOp2 As Range, rw As Range Set wsCD = ActiveWorkbook.Worksheets("Capital-Data") Set wsOM = ActiveWorkbook.Worksheets("O&M-Data") lastrowCD = wsCD.Cells(Rows.Count, 1).End(xlUp).Row lastrowOM = wsOM.Cells(Rows.Count, 1).End(xlUp).Row With wsCD.Sort .SortFields.Clear .SortFields.Add Key:=wsCD.Range("E:E"), SortOn:=xlSortOnValues, _ Order:=xlAscending, DataOption:=xlSortNormal .SetRange wsCD.Range("A1").CurrentRegion .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With UsdRws = wsCD.Range("A1").CurrentRegion.Rows.Count For i = UsdRws To 2 Step -1 If wsCD.Range("E" & i).Value Like "ITS####" Then Build rngOp, wsCD.Rows(i) 'collecting a range to move.... 'find and collect matches on O&M sheet For j = 2 To (lastrowOM - 1) If wsOM.Cells(j, 10) = wsCD.Range("A" & i) Then Build rngOp2, wsOM.Rows(j) End If Next End If Next i If Not rngOp Is Nothing Then rngOp.Copy wsCD.Range("A" & Rows.Count).End(xlUp).Offset(2) rngOp.Delete End If 'move matched rows on OM sheet If Not rngOp2 Is Nothing Then rngOp2.Copy wsOM.Range("A" & Rows.Count).End(xlUp).Offset(2) rngOp2.Delete End If End Sub 'utility Sub for building a range Sub Build(ByRef rngTot As Range, ByRef rngAdd As Range) If rngTot Is Nothing Then Set rngTot = rngAdd Else Set rngTot = Application.Union(rngTot, rngAdd) End If End Sub 。下面给出的是您的代码段的解决方案。

javafx.concurrent

我创建了一个名为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

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

内部对其进行了调用

参考

Task - A Task Which Modifies The Scene Graph

JavaFX: Interoperability