计算机网络作业(五)

做一个pingGUI程序,本来用MFC写的,写到一半真不想用那个垃圾框架了,干脆用Java写算了,第一次用Java写界面,有点丑。

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package pingGUI;


import org.icmp4j.IcmpPingUtil;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

import org.icmp4j.IcmpPingRequest;
import org.icmp4j.IcmpPingResponse;

// Sample class, copyright 2009 and beyond, icmp4j
public class pingGUI {

// the java entry point
public static void main (final String[ ] args)
throws Exception {
Frame f=new Frame("ping GUI");
f.setLayout(new FlowLayout(FlowLayout.LEFT,45,30));
f.setSize(450, 500);
f.setLocation(300, 200);
Label a=new Label("IP/域名");
f.add(a);
JTextField inputField;
inputField=new JTextField(20);
f.add(inputField);
MyWindowListener mw=new MyWindowListener();
f.addWindowListener(mw);
Label b=new Label("次数");
f.add(b);
JButton start=new JButton("开始");

JTextField time;
time=new JTextField(20);
f.add(time);
JTextArea logContent=new JTextArea(12,30);
JScrollPane showPanel=new JScrollPane(logContent);
logContent.setEditable(false);
JPanel inputPanel=new JPanel();
f.add(showPanel,BorderLayout.CENTER);
f.add(inputPanel,BorderLayout.SOUTH);
f.add(start,BorderLayout.CENTER);
f.setVisible(true);
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String content=inputField.getText();
String ci=time.getText();
int ttime=Integer.parseInt(ci);
final IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest ();
request.setHost (content);

// repeat a few times
for (int count = 1; count <= ttime; count ++) {

// delegate
final IcmpPingResponse response = IcmpPingUtil.executePingRequest (request);

// log
final String formattedResponse = IcmpPingUtil.formatResponse (response);

if(content!=null&&!content.trim().equals("")) {
logContent.append (formattedResponse+"\n");
}
else
{logContent.append("域名或者IP不能为空");break;}
}
}
});
// rest
Thread.sleep (1000);
}

}

class MyWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e) {
Window window=e.getWindow();
window.setVisible(false);
window.dispose();
}


}

测试gif

作业.gif

总结

   用Java写出一个比较精美,并且布局相当精致的程序还是比较困难的,你需要知道怎么使用不同的布局,才能将一个程序写的相当不错。