Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试构建Ping功能,当按下按钮时,将向用户返回一个对话框,显示主机是否启动';s咔嗒一声_Java_Swing_Networking - Fatal编程技术网

Java 尝试构建Ping功能,当按下按钮时,将向用户返回一个对话框,显示主机是否启动';s咔嗒一声

Java 尝试构建Ping功能,当按下按钮时,将向用户返回一个对话框,显示主机是否启动';s咔嗒一声,java,swing,networking,Java,Swing,Networking,下面是我正在编写的代码。代码将执行一个基于Swing的GUI,带有一个简单的JFrame,其中包含一个按钮,按下该按钮后将在后台运行Ping实用程序。如果主机是否可访问,将显示一个对话框,其中显示成功/不成功消息 import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.Inet

下面是我正在编写的代码。代码将执行一个基于Swing的GUI,带有一个简单的JFrame,其中包含一个按钮,按下该按钮后将在后台运行Ping实用程序。如果主机是否可访问,将显示一个对话框,其中显示成功/不成功消息

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);


}
}

下图显示了按下按钮后流出的tcpdump流量与终端发出的ping流量。


任何见解都将不胜感激。

因此,运行您的代码将生成

Exception in thread "main" java.lang.NullPointerException
    at sotest.App.<init>(App.java:25)
    at sotest.App.main(App.java:54)
但现在当我运行它时,我得到

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() {

    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();
            }
        }
    });
}
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);
}
啊,但现在当我运行它时,它没有显示任何内容。这是因为我没有将按钮添加到面板中,我们再次更新构造函数,如下所示:

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();
            }
        }
    });
}
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,建议您可能需要花更多的时间通读


你的下一个问题将更难解决,但你可以从

开始,我不确定你的问题是什么。你需要帮忙展示对话吗?在展示对话之前,您是否需要帮助等待?您是否需要帮助将信息放入对话中?因此,除了您编写的代码可能会生成
NullPointerException
和/或不显示按钮之外。。。你的问题是什么?啊!对不起,应该更明确地解释基本的网络。当我ping任何内部/私有IP(即192.168.x.x)时,它运行正常。但是,当我运行任何公共IP(即,尝试访问互联网时,ping不会通过。我可以详细介绍ping的工作原理,但我不想让你感到厌烦。谢谢!啊!对不起,应该更明确地解释基本网络。当我ping任何内部/私有IP(即192.168.x.x)时,它运行良好。但当我运行任何公共IP时(也就是说,尝试访问互联网时,ping不会通过。我可以深入了解ping如何工作的细节,但我不想让你感到厌烦。谢谢!@95C3ntS好吧,我认为这超出了NPE和
非法组件状态例外
的直接问题,并开始进入另一个问题领域