两种办法:
1) 在原有方法里加设置到textfield的命令
public static void ipaddress()
{
try{
InetAddress inet = InetAddress.getLocalHost();
System.out.println(inet.getHostAddress());
text1.setText(inet.getHostAddress());//new added
}
catch(UnknownHostException e){
System.err.println("UnknownHostException" + e.getMessage());
}
}
2) 方法改返回值.
public static String ipaddress()
{
String ret = null;
try{
InetAddress inet = InetAddress.getLocalHost();
ret=inet.getHostAddress();
System.out.println(ret);
}
catch(UnknownHostException e){
System.err.println("UnknownHostException" + e.getMessage());
}
return ret;
}
然后在调用此方法的后面,加
text1.setText(inet.getHostAddress());
取消
评论