最近在学习java,,一步一步往上爬,,感觉自己的头脑不怎么适合编程,但学了这个,就得踏实走下去,不然以后拿什么吃饭。。所以还是乖乖滴从编小程序练手开始,,希望有一天自己不再是菜鸟。。。。。
以前学过C和C++,接触过JAVA后感觉自己的兴趣来了,,因为java可以编些小小的软件,难度不是很大。。。下面是一个ip获取器,功能胡乱胡乱滴(工具Netbeans)..。
界面设计如图,下面是主要功能的代码:
- import java.awt.event.KeyEvent;
- import java.io.IOException;
- import java.net.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- public class Main extends javax.swing.JFrame {
- public Main() {
- initComponents();
- setTitle("IP获取器---java制作"); }
- InetAddress myIP=null;
- InetAddress address=null;
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- myIP=InetAddress.getLocalHost(); }
- catch (UnknownHostException err){
- JOptionPane.showMessageDialog(null,"无法获取本地地址");
- }
- try{
- address=InetAddress.getByName(jTextField1.getText());}
- catch (UnknownHostException err){
- JOptionPane.showMessageDialog(null,"网址输入错误","警告",JOptionPane.WARNING_MESSAGE);}
- jLabel5.setText(myIP.getHostAddress());
- jLabel6.setText(address.getHostAddress());
- if((myIP.getAddress()).length==4)
- { jLabel7.setText("本机IP为ipv4");
- byte[] ipByte=myIP.getAddress();
- int firstByte=ipByte[0];
- if(firstByte<0) {firstByte=firstByte+256;}
- if((firstByte&0x80)==0){ jLabel8.setText("本机IP地址为A类");}
- else if((firstByte&0xC0)==0x80){jLabel8.setText("本机IP地址为B类");}
- else if((firstByte&0xE0)==0xC0){jLabel8.setText("本机IP地址为C类");}
- else if((firstByte&0xF0)==0xE0){jLabel8.setText("本机IP地址为D类");}
- else if((firstByte&0xF8)==0xF0){jLabel8.setText("本机IP地址为E类");}
- }
- else if((myIP.getAddress()).length==16) jLabel7.setText("本机IP为ipv6");
- if((address.getAddress()).length==4)
- { jLabel9.setText("网络IP为ipv4");
- byte[] ipByte=address.getAddress();
- int firstByte=ipByte[0];
- if(firstByte<0) {firstByte=firstByte+256;}
- if((firstByte&0x80)==0){ jLabel10.setText("网络IP地址为A类");}
- else if((firstByte&0xC0)==0x80){jLabel10.setText("网络IP地址为B类");}
- else if((firstByte&0xE0)==0xC0){jLabel10.setText("网络IP地址为C类");}
- else if((firstByte&0xF0)==0xE0){jLabel10.setText("网络IP地址为D类");}
- else if((firstByte&0xF8)==0xF0){jLabel10.setText("网络IP地址为E类");}
- }
- else if((address.getAddress()).length==16) jLabel9.setText("网络IP为ipv6");
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { //打开此网页
- try{
- myIP=InetAddress.getLocalHost();}
- catch(UnknownHostException err){
- JOptionPane.showMessageDialog(null,"无法获取本地地址");}
- try{
- address=InetAddress.getByName(jTextField1.getText());}
- catch(UnknownHostException err){
- JOptionPane.showMessageDialog(null,"网址输入错误","警告",JOptionPane.WARNING_MESSAGE); }
- try{
- String str=jTextField1.getText();
- String command="C:\\Program Files\\Internet Explorer\\iexplore.exe "+str;
- Runtime.getRuntime().exec( command);
- }
- catch(IOException err){
- JOptionPane.showMessageDialog(null,"无效的输入","警告",JOptionPane.WARNING_MESSAGE); }
- jLabel5.setText(myIP.getHostAddress());
- jLabel6.setText(address.getHostAddress());
- }
转载于:https://blog.51cto.com/1164101639/725017