Trying to build a Ping functionality, that wil return a dialog to the user displaying the host is up or not when a button it's clicked

huangapple go评论66阅读模式
英文:

Trying to build a Ping functionality, that wil return a dialog to the user displaying the host is up or not when a button it's clicked

问题

以下是我正在处理的代码该代码将执行基于Swing的GUI其中有一个简单的JFrame其中包含一个按钮当按下按钮时将在后台运行Ping实用程序如果主机是可达或不可达的则会显示一个带有成功/失败消息的对话框

```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;

public class App {

    private JTextArea clickThisButtonTextArea;
    public JButton button1;
    private JPanel panelMain;

    public App() {
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    sendPing("8.8.8.8");
                    //JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public String sendPing(String ipAddr) throws IOException {
        InetAddress ip = InetAddress.getByName(ipAddr);
        boolean ipReach = ip.isReachable(5000);
        System.out.println("Sending Ping Request to " + ipAddr);
        if (ip.isReachable(5000)) {
            JOptionPane.showMessageDialog(null, "主机可达!");
            System.out.println("主机可达");
        } else {
            JOptionPane.showMessageDialog(null, "抱歉,无法连接到主机!");
            System.out.println("抱歉!我们无法连接到此主机");
        }
        return null;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("App");
        frame.setContentPane(new App().panelMain);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setSize(250, 250);
    }
}

下面的图片显示了在按下按钮后流出的流量的tcpdump,与从终端运行的ping的情况对比。

Trying to build a Ping functionality, that wil return a dialog to the user displaying the host is up or not when a button it's clicked

非常感谢您的任何见解。

英文:

Below is the code I'm working on. The code will execute a Swing-based GUI, with a simple JFrame that holds a button which when pressed will run the Ping utility in the background. If the host is either reachable or not, a dialog will be displayed with a successful/unsuccess message.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
public class App {
private JTextArea clickThisButtonTextArea;
public JButton button1;
private JPanel panelMain;
public App() {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
sendPing("8.8.8.8");
//JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public String sendPing(String ipAddr) throws IOException {
InetAddress ip = InetAddress.getByName(ipAddr);
boolean ipReach = ip.isReachable(5000);
System.out.println("Sending Ping Request to " + ipAddr);
if (ip.isReachable(5000)) {
JOptionPane.showMessageDialog(null, "Host is reachable!");
System.out.println("Host is reachable");
} else {
JOptionPane.showMessageDialog(null, "Sorry, no host!");
System.out.println("Sorry ! We can't reach to this host");
}
return null;
}
public static void main(String[] args) {
JFrame frame = new JFrame("App");
frame.setContentPane(new App().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(250,250);
}

}

The image below shows a tcpdump of traffic flowing out after the button is pressed vs. a ping ran from the terminal.
Trying to build a Ping functionality, that wil return a dialog to the user displaying the host is up or not when a button it's clicked

Any insights will be greatly appreciated.

答案1

得分: 2

所以,运行您的代码会生成以下异常:

Exception in thread "main" java.lang.NullPointerException
	at sotest.App.<init>(App.java:25)
	at sotest.App.main(App.java:54)

这是因为 button1 从未被初始化。我可以通过以下方式更新构造函数来修复它:

public App() {
    button1 = new JButton("Ping");
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                sendPing("8.8.8.8");
                //JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

但现在当我运行它时,我得到以下异常:

Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
	at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:598)
	at java.desktop/javax.swing.JFrame.setContentPane(JFrame.java:679)
	at sotest.App.main(App.java:56)

这是因为 panelMain 从未被初始化。我可以通过以下方式更新构造函数来修复它:

public App() {
    panelMain = new JPanel();

    button1 = new JButton("Ping");
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                sendPing("8.8.8.8");
                //JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

啊,但现在当我运行它时,什么都没有显示出来。这是因为我没有将按钮添加到面板上,再次更新构造函数如下:

public App() {
    panelMain = new JPanel();

    button1 = new JButton("Ping");
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                sendPing("8.8.8.8");
                //JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    panelMain.add(button1);
}

所有这些都是相当基本的 Java,我建议您可能需要花更多时间阅读 使用 JFC/Swing 创建 GUI。您接下来的问题会更棘手,但可以从 Swing 中的并发性 开始解决。

英文:

So, running your code generates

Exception in thread &quot;main&quot; java.lang.NullPointerException
at sotest.App.&lt;init&gt;(App.java:25)
at sotest.App.main(App.java:54)

This is because button1 is never initialised. I can fix it by updating the constructor like so:

public App() {
button1 = new JButton(&quot;Ping&quot;);        
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
sendPing(&quot;8.8.8.8&quot;);
//JOptionPane.showMessageDialog(null, sendPing(&quot;192.168.0.1&quot;));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}

But now when I run it, I get...

Exception in thread &quot;main&quot; java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:598)
at java.desktop/javax.swing.JFrame.setContentPane(JFrame.java:679)
at sotest.App.main(App.java:56)

This is because panelMain has never been initialised. I can fix it by updating the constructor like so:

public App() {
panelMain = new JPanel();
button1 = new JButton(&quot;Ping&quot;);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
sendPing(&quot;8.8.8.8&quot;);
//JOptionPane.showMessageDialog(null, sendPing(&quot;192.168.0.1&quot;));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}

Ah, but now when I run it, it's not displaying anything. This is because I've not added the button to the panel, once again, we update the constructor like so:

public App() {
panelMain = new JPanel();
button1 = new JButton(&quot;Ping&quot;);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
sendPing(&quot;8.8.8.8&quot;);
//JOptionPane.showMessageDialog(null, sendPing(&quot;192.168.0.1&quot;));
} catch (IOException e) {
e.printStackTrace();
}
}
});
panelMain.add(button1);
}

All of this is pretty basic Java and would suggest you might need to spend some more time reading through Creating a GUI With JFC/Swing.

Your next problem is going to more tricky to solve, but you can get a start with Concurrency in Swing

huangapple
  • 本文由 发表于 2020年9月1日 10:29:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63680507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定