计算机作业五

来源:互联网 发布:国企程序员面试题 编辑:程序博客网 时间:2024/05/29 19:14

pingGUI程序源代码

  1. package pingGUI;
  2. import org.icmp4j.IcmpPingUtil;
  3. import java.awt.BorderLayout;
  4. import java.awt.FlowLayout;
  5. import java.awt.Frame;
  6. import java.awt.Label;
  7. import java.awt.Window;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.WindowAdapter;
  11. import java.awt.event.WindowEvent;
  12. import javax.swing.*;
  13. import org.icmp4j.IcmpPingRequest;
  14. import org.icmp4j.IcmpPingResponse;
  15. // Sample class, copyright 2009 and beyond, icmp4j
  16. public class pingGUI {
  17. // the java entry point
  18. public static void main (final String[ ] args)
  19. throws Exception {
  20.  Frame f=new Frame("ping GUI");
  21.  f.setLayout(new FlowLayout(FlowLayout.LEFT,45,30));
  22.  f.setSize(450, 500);
  23.  f.setLocation(300, 200);
  24.  Label a=new Label("IP/域名");
  25.  f.add(a);
  26.  JTextField inputField;
  27.  inputField=new JTextField(20);
  28.  f.add(inputField);
  29.  MyWindowListener mw=new MyWindowListener();
  30.  f.addWindowListener(mw);
  31.  Label b=new Label("次数");
  32.  f.add(b);
  33.  JButton start=new JButton("开始");
  34.  JTextField time;
  35.  time=new JTextField(20);
  36.  f.add(time);
  37.  JTextArea logContent=new JTextArea(12,30);
  38.  JScrollPane showPanel=new JScrollPane(logContent);
  39.  logContent.setEditable(false);
  40.  JPanel inputPanel=new JPanel();
  41.  f.add(showPanel,BorderLayout.CENTER);
  42.  f.add(inputPanel,BorderLayout.SOUTH);
  43.  f.add(start,BorderLayout.CENTER);
  44.  f.setVisible(true);
  45.  start.addActionListener(new ActionListener() {
  46.  public void actionPerformed(ActionEvent e) {
  47.  String content=inputField.getText();
  48.  String ci=time.getText();
  49.  int ttime=Integer.parseInt(ci);
  50.  final IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest ();
  51.  request.setHost (content);
  52.  // repeat a few times
  53.  for (int count = 1; count <= ttime; count ++) {
  54.  // delegate
  55.  final IcmpPingResponse response = IcmpPingUtil.executePingRequest (request);
  56.  // log
  57.  final String formattedResponse = IcmpPingUtil.formatResponse (response);
  58.  if(content!=null&&!content.trim().equals("")) {
  59.  logContent.append (formattedResponse+"\n");
  60.  }
  61.  else
  62.  {logContent.append("域名或者IP不能为空");break;}
  63.  }
  64.  }
  65.  });
  66.  // rest
  67.  Thread.sleep (1000);
  68.  }
  69. }
  70. class MyWindowListener extends WindowAdapter{
  71.  public void windowClosing(WindowEvent e) {
  72.  Window window=e.getWindow();
  73.  window.setVisible(false);
  74.  window.dispose();
  75.  }
  76. }

测试gif

作业.gif注意

总结

原创粉丝点击