计算机网络作业(五)
做一个pingGUI程序,本来用MFC写的,写到一半真不想用那个垃圾框架了,干脆用Java写算了,第一次用Java写界面,有点丑。
源代码
xpackage 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, icmp4jpublic class pingGUI {// the java entry pointpublic 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

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