2013-12-10 43 views
0

您好,我正在尝试创建一个GUI,并使用一个简单的按钮来刷新3个文本框中的信息。TextField中的MAC地址

IP地址 主机名 MAC地址

领域的

上面我有前2分选他们的工作,但我不能工作,如何得到最后一个工作。

在我的GUI上,文本字段不包含代码,我使用按钮的代码来设置这些字段中的文本。

代码如下。

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package testforensicsapp; 

import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.net.UnknownHostException; 

/** 
* 
* @author jblacklock 
*/ 
public class ForensicsGui extends javax.swing.JFrame { 

    /** 
    * Creates new form ForensicsGui 
    */ 
    public ForensicsGui() { 
     initComponents(); 
    } 

    /** 
    * 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. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jTabbedPane2 = new javax.swing.JTabbedPane(); 
     jButton1 = new javax.swing.JButton(); 
     jTabbedPane3 = new javax.swing.JTabbedPane(); 
     jPanel1 = new javax.swing.JPanel(); 
     jButton3 = new javax.swing.JButton(); 
     jButton2 = new javax.swing.JButton(); 
     jTabbedPane8 = new javax.swing.JTabbedPane(); 
     jPanel2 = new javax.swing.JPanel(); 
     jTextField2 = new javax.swing.JTextField(); 
     jButton4 = new javax.swing.JButton(); 
     jTextField1 = new javax.swing.JTextField(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextPane1 = new javax.swing.JTextPane(); 
     jScrollPane2 = new javax.swing.JScrollPane(); 
     jTextPane2 = new javax.swing.JTextPane(); 
     jScrollPane3 = new javax.swing.JScrollPane(); 
     jTextPane3 = new javax.swing.JTextPane(); 
     jTextField3 = new javax.swing.JTextField(); 

     jButton1.setText("jButton1"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 
     jTabbedPane2.addTab("tab1", jButton1); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 841, Short.MAX_VALUE) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 292, Short.MAX_VALUE) 
     ); 

     jTabbedPane3.addTab("tab1", jPanel1); 

     jButton3.setText("jButton3"); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setUndecorated(true); 

     jButton2.setText("X"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(evt); 
      } 
     }); 

     jTextField2.setEditable(false); 
     jTextField2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jTextField2ActionPerformed(evt); 
      } 
     }); 

     jButton4.setText("Start/Refresh"); 
     jButton4.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton4ActionPerformed(evt); 
      } 
     }); 

     jTextPane1.setEditable(false); 
     jTextPane1.setText("IP Address"); 
     jScrollPane1.setViewportView(jTextPane1); 

     jTextPane2.setText("Host Name"); 
     jScrollPane2.setViewportView(jTextPane2); 

     jTextPane3.setText("MAC Address"); 
     jScrollPane3.setViewportView(jTextPane3); 

     javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
     jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) 
        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup() 
         .addContainerGap() 
         .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup() 
         .addGap(32, 32, 32) 
         .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(jPanel2Layout.createSequentialGroup() 
         .addGap(32, 32, 32) 
         .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
          .addComponent(jScrollPane3) 
          .addComponent(jScrollPane2)) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
          .addComponent(jTextField1) 
          .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)))) 
       .addContainerGap(627, Short.MAX_VALUE)) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(19, 19, 19) 
       .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addGap(28, 28, 28) 
       .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE) 
        .addComponent(jTextField2)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(jTextField1) 
        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(jTextField3) 
        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)) 
       .addContainerGap(191, Short.MAX_VALUE)) 
     ); 

     jTabbedPane8.addTab("Computer Information", jPanel2); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addGap(0, 904, Short.MAX_VALUE) 
       .addComponent(jButton2)) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(jTabbedPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 888, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(28, 28, 28)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jButton2) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jTabbedPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 392, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(22, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
     System.exit(0); 
// TODO add your handling code here: 
    }           

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
    }           

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {            
     // TODO add your handling code here: 

    }           

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 

     InetAddress ip; 
     try { 

      ip = InetAddress.getLocalHost(); 
      jTextField2.setText(ip.getHostAddress()); 

      java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost(); 
      jTextField1.setText(localMachine.getHostName()); 

      byte[] mac = network.getHardwareAddress(); 
      jTextField3.setText(.getHardwareAddress()); 

     } catch (UnknownHostException e) { 

      e.printStackTrace(); 

     } 
    }           

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(ForensicsGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(ForensicsGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(ForensicsGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(ForensicsGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 


     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new ForensicsGui().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JButton jButton3; 
    private javax.swing.JButton jButton4; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JScrollPane jScrollPane2; 
    private javax.swing.JScrollPane jScrollPane3; 
    private javax.swing.JTabbedPane jTabbedPane2; 
    private javax.swing.JTabbedPane jTabbedPane3; 
    private javax.swing.JTabbedPane jTabbedPane8; 
    private javax.swing.JTextField jTextField1; 
    private javax.swing.JTextField jTextField2; 
    private javax.swing.JTextField jTextField3; 
    private javax.swing.JTextPane jTextPane1; 
    private javax.swing.JTextPane jTextPane2; 
    private javax.swing.JTextPane jTextPane3; 
    // End of variables declaration     

    private static class ComponentMover { 

     public ComponentMover() { 
     } 
    } 

    private static class BorderLayout { 

     public BorderLayout() { 
     } 
    } 
} 

我无法揣摩出我与这个脚麻任何帮助在这一阶段是极大的赞赏,因为它是采取了很多的时间才能完成。

这段代码是为了获取我机器的本地,IP,Mac和主机名。

感谢您的任何帮助。

+0

什么问题? – Chiron

+0

你的代码不完整,你的问题不清楚 – Reimeus

+0

太棒了!它看起来像JBuidler生成的GUI。我出去了,对不起。 – Chiron

回答

0

在Java 6及以上,你可以使用这种方法(和commons codec) - 替换这两行(从你的问题) -

byte[] mac = network.getHardwareAddress(); 
jTextField3.setText(.getHardwareAddress()); 

与这些线 -

Enumeration<NetworkInterface> nics = NetworkInterface 
     .getNetworkInterfaces(); // to actually make this work 
byte[] mac = null; 
while (nics.hasMoreElements()) { 
    NetworkInterface ni = nics.nextElement(); 
    if (ni != null && ni.getHardwareAddress() != null) { // Be sure. 
    mac = ni.getHardwareAddress(); 
    break; 
    } 
} 
// https://commons.apache.org/proper/commons-codec/javadocs/api-release/org/apache/commons/codec/binary/Hex.html 
if (mac != null) { 
    jTextField3.setText(Hex.encodeHexString(mac)); 
} 

它在我的机器上工作。

+0

感谢您的信息,但我仍然不确定如何实现此目的?然而,我确实有另一个项目,它抓住了IP,MAC和主机名,只是在没有GUI的输出中显示它。是否有可能从单独的项目输出中拖动信息,并在我的文本框中设置文本,这会更容易还是使事情更复杂?如果它更容易,我可以编辑我的帖子以显示我的其他项目的代码。 – user3080543

+0

@ user3080543编辑我的回答,但是,我正在替换你的两行'byte [] mac = network.getHardwareAddress(); jTextField3.setText(.getHardwareAddress());'我测试了它。 –