- 论坛徽章:
- 0
|
JAVA笔记 这个我的写的一个自己获得本机IP和获得其它网站的IP的一个小程序希望大家来看看 还有什么要补的。
请在输入框里输入网址: 比如:
[color="#800080"]www.baidu.com
;
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Ipserver11 extends JFrame implements ActionListener{
private InetAddress ServerIP ;
private InetAddress LocalIP ;
JTextField t_tr = new JTextField() ;
JTextField t_hd = new JTextField() ;
InetAddress uip ;
InetAddress lip ;
Ipserver11() {
//设置窗体的属性
this.setSize(300 ,180 ) ;
this.setResizable(false) ;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
//设置窗体剧中
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
this.setLocation((width-300)/2 , (height-180)/2 ) ;
//新建一大堆组件
JLabel l_from = new JLabel("请输入网址:") ;
JButton b_hd = new JButton("获得IP");
JButton b_local = new JButton("获得本机IP") ;
//注册监听器
b_hd.addActionListener(this) ;
b_local.addActionListener(this) ;
//设置输入面板
JPanel p_input = new JPanel() ;
p_input.setLayout(new GridLayout()) ;
p_input.add(l_from) ;
p_input.add(t_tr) ;
//设置获得面板
JPanel p_button = new JPanel() ;
p_button.setLayout(new FlowLayout() ) ;
p_button.add(b_hd) ;
p_button.add(b_local) ;
//布置面板
this.setLayout(new BorderLayout()) ;
this.add(p_input , BorderLayout.NORTH) ;
this.add(t_hd , BorderLayout.CENTER) ;
this.add(p_button , BorderLayout.SOUTH) ;
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true) ;
Ipserver11 is = new Ipserver11() ;
is.setVisible(true) ;
//System.out.println("ServerIP ip is :" +is.CatchServerIP()) ;
}
public InetAddress CatchServerIP(){
try{
ServerIP = InetAddress.getByName(t_tr.getText()) ;
//获得服务器IP
}catch(Exception ex){
JOptionPane.showMessageDialog(this," 请输入正确地址: !!!!") ;
}
return ServerIP ;
}
//获得本机IP
public InetAddress CatchLocalIP(){
try{
LocalIP = InetAddress.getLocalHost() ;
}catch(Exception ex){
ex.printStackTrace() ;
}
return LocalIP ;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("获得IP")){
try{
String st = t_tr.getText() ;
if(st.equals(" localhost")){
JOptionPane.showConfirmDialog(this,"dd") ;
}
uip = this.CatchServerIP() ;
t_hd.setText(uip.toString()) ;
}catch(Exception ex){
ex.printStackTrace() ;
}
}
if(e.getActionCommand().equals("获得本机IP")){
try{
//System.out.print("dd") ;
lip = this.CatchLocalIP() ;
t_hd.setText(lip.toString()) ;
}catch(Exception ex){
ex.printStackTrace() ;
}
}
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/22190/showart_337745.html |
|