⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ping.java

📁 运用Java实现一个Ping程序
💻 JAVA
字号:
/* * Ping.java * * Created on 2008年5月24日, 下午3:15 */package 实验五;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;  /** * * @author  jsj */public class Ping extends javax.swing.JFrame {        /** Creates new form Ping */    public Ping() {        initComponents();    }        private static final String windowsCommand = "ping";        /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents    private void initComponents() {        jTextField1 = new javax.swing.JTextField();        jLabel1 = new javax.swing.JLabel();        jButtonOk = new javax.swing.JButton();        jScrollPane1 = new javax.swing.JScrollPane();        jList1 = new javax.swing.JList();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jTextField1.setText("202.202.243.2");        jTextField1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jTextField1ActionPerformed(evt);            }        });        jLabel1.setFont(new java.awt.Font("宋体", 0, 18));        jLabel1.setText("Ip地址:");        jButtonOk.setFont(new java.awt.Font("宋体", 0, 14));        jButtonOk.setText("确定");        jButtonOk.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButtonOkActionPerformed(evt);            }        });        jList1.setModel(new javax.swing.AbstractListModel() {            String[] strings = { "" };            public int getSize() { return strings.length; }            public Object getElementAt(int i) { return strings[i]; }        });        jScrollPane1.setViewportView(jList1);        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());        getContentPane().setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)                    .addGroup(layout.createSequentialGroup()                        .addGap(11, 11, 11)                        .addComponent(jLabel1)                        .addGap(18, 18, 18)                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                        .addComponent(jButtonOk))                    .addGroup(layout.createSequentialGroup()                        .addGap(31, 31, 31)                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)))                .addContainerGap(76, Short.MAX_VALUE))        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addGap(21, 21, 21)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel1)                    .addComponent(jButtonOk)                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))                .addGap(18, 18, 18)                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)                .addContainerGap(29, Short.MAX_VALUE))        );        pack();    }// </editor-fold>//GEN-END:initComponents    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed        // TODO add your handling code here:    }//GEN-LAST:event_jTextField1ActionPerformed    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed         // TODO add your handling code here:  try{      final String os=System.getProperty("os");      String command=windowsCommand+' '+jTextField1.getText();      final Process process = Runtime.getRuntime().exec(command);      BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));         String[]result =new String[100];         int i=0;        for (String line = null; (line = reader.readLine()) != null;) {//          将line按给定模式创建一个匹配器                     result[i++]=line;            jList1.setListData(result);        }} catch (IOException ex) {            System.out.println(ex);        }    }//GEN-LAST:event_jButtonOkActionPerformed        /**     * @param args the command line arguments     */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                new Ping().setVisible(true);            }        });    }        // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton jButtonOk;    private javax.swing.JLabel jLabel1;    private javax.swing.JList jList1;    private javax.swing.JScrollPane jScrollPane1;    private javax.swing.JTextField jTextField1;    // End of variables declaration//GEN-END:variables    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -