題目要求:
1.編程作業,要求用一種自己擅長的語言實現ip地址的獲取,及其詳細信息的顯示,要求用窗口顯示。
2.生成GIF動態演示圖片,要求先演示程序目錄結構與內容,然后以程序源碼為背景,演示軟件運行過程。
3.最后提交的軟件以綠色軟件的形式提交,具體為兩種形式:
--a)壓縮文件:運行文件(如nicinfo.exe)+ 各種運行所需要文件(如xxx.dll)夠成的壓縮包,解壓可以直接運行,不需要安裝任何軟件或插件;
--b)單一運行文件:利用相關工具將原有的可執行程序和所需要各種資源文件打包成一個可直接運行于Windows或Linux平臺的單一文件。
一.編寫函數
我采用的語言是java,由于涉及到窗口的創建,所以java書第九章的知識很重要,把其中的關鍵要點掌握下來。又由于要獲取主機信息和ip信息,這方面的知識不會,只有求助于百度了。完成情況基本如下:
代碼:
main.java
package IP1;
/**
* @author JavaAlpha
* @date 2011-12-14
* @version V 1.0 java代碼 調用dos的ipconfig /all 命令,獲取網卡詳細信息
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import java.io
.BufferedReader;
import java.io
.IOException;
import java.io
.InputStreamReader;
public class main {
public static void main(String[] args){
String hostName = getLocalMachineInfo("主機名? . . . . . . . . . . . . . :");
JFrame f=new JFrame("網卡信息");
Label la=new Label("用戶名稱;");
la.setBounds(10, 10, 100, 30);
la.setFont(new Font("宋體", Font.BOLD,20));
f.add(la);
JComboBox comboBox=new JComboBox();
comboBox.addItem("請選擇用戶");
comboBox.addItem(hostName);
comboBox.setBounds(115, 10, 200, 30);
comboBox.setFont(new Font("宋體", Font.BOLD,20));
f.add(comboBox);
JButton button=new JButton("詳細");
button.setFont(new Font("宋體", Font.BOLD,20));
button.setBounds(320,10,90,30);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String item=(String)comboBox.getSelectedItem();
if(hostName.equals(item)){
new xiangxi();}
}
});
f.add(button);
f.setLayout(null);
f.setSize(500,300);
f.setLocation(300,200);
f.setVisible(true);
}
static String getLocalMachineInfo(String str) {
String line = "";
int n;
try {
Process ps = Runtime.getRuntime().exec("cmd /c ipconfig /all");
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
while (null != (line = br.readLine())) {
if (line.indexOf(str) != -1) { // 在line字符串中 查找str出現的位置,str即上面的字符串
n = line.indexOf(":");
line = line.substring(n + 2); // 獲得 :后面的數據;
break;
}
}
ps.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return line; // 返回數據
}
}
xiangxi.java
package IP1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class xiangxi {
public xiangxi()
{
// 獲取機器名
String hostName = getLocalMachineInfo("主機名? . . . . . . . . . . . . . :");
// 獲取網卡型號
String desc = getLocalMachineInfo("描述. . . . . . . . . . . . . . . :");
// 獲取MAC地址
String mac = getLocalMachineInfo(" 物理地址. . . . . . . . . . . . . :");
// 獲取IP地址
String ip = getLocalMachineInfo(" IPv4 地址 . . . . . . . . . . . . :");
// 獲取子網掩碼
String subnetMask = getLocalMachineInfo(" 子網掩碼? . . . . . . . . . . . . :");
// 獲取默認網關
String defaultGateway = getLocalMachineInfo(" 默認網關. . . . . . . . . . . . . :");
// 獲取DNS服務器
String DNSServers = getLocalMachineInfo("DNS 服務器? . . . . . . . . . . . :");
JFrame f = new JFrame("ip信息");
Label la = new Label("機器名:" + hostName);
la.setBounds(10, 10, 500, 30);
la.setFont(new Font("宋體", Font.BOLD,20));
f.add(la);
Label lb = new Label("網卡信息:" + desc);
lb.setBounds(10, 50, 500, 30);
lb.setFont(new Font("宋體", Font.BOLD,20));
f.add(lb);
Label lc = new Label("MAC地址:" + mac);
lc.setBounds(10, 90, 500, 30);
lc.setFont(new Font("宋體", Font.BOLD,20));
f.add(lc);
Label ld = new Label("IP地址:" + ip);
ld.setBounds(10, 130, 500, 30);
ld.setFont(new Font("宋體", Font.BOLD,20));
f.add(ld);
Label le = new Label("子網掩碼:" + subnetMask);
le.setBounds(10, 170, 500, 30);
le.setFont(new Font("宋體", Font.BOLD,20));
f.add(le);
Label lf = new Label("默認網關:" + defaultGateway);
lf.setBounds(10, 210, 500, 30);
lf.setFont(new Font("宋體", Font.BOLD,20));
f.add(lf);
Label lg = new Label("Dns服務器:" + DNSServers);
lg.setBounds(10, 250, 500, 30);
lg.setFont(new Font("宋體", Font.BOLD,20));
f.add(lg);
f.setLayout(null);
f.setSize(500, 400);
f.setLocation(300, 200);
f.setVisible(true);
}
static String getLocalMachineInfo(String str) {
String line = "";
int n;
try {
Process ps = Runtime.getRuntime().exec("cmd /c ipconfig /all");
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
while (null != (line = br.readLine())) {
if (line.indexOf(str) != -1) { // 在line字符串中
// 查找str出現的位置,str即上面的字符串
n = line.indexOf(":");
line = line.substring(n + 2); // 獲得 :后面的數據;
break;
}
}
ps.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return line; // 返回數據
}
}
二,心得與體會
? ? ? 本次作業技術難度有點高,對于知識的掌握比較細,使我對于Java圖形用戶界面的知識掌握加深,也讓我了解了關于訪問系統信息的相關知識。相信每一次的作業都能讓我取得進步。