英文:
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的情况对比。
非常感谢您的任何见解。
英文:
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.
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 "main" java.lang.NullPointerException
at sotest.App.<init>(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("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();
}
}
});
}
But now when I run it, I get...
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)
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("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();
}
}
});
}
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("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);
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论