import java.awt.*;
import java.awt.event.*;
public class CZIP extends Frame implements ActionListener {
TextField text1, text2, text3;
Label lab1, lab2, lab3;
Button but1, but2;
URL add;
InetAddress ip;
CZIP() {
super("IP地址查询窗口");
setSize(400, 150);
setLayout(null);
text1 = new TextField(30);
text1.setBounds(160, 36, 220, 20);
text2 = new TextField(30);
text2.setBounds(160, 66, 220, 20);
text3 = new TextField(16);
text3.setBounds(160, 96, 100, 20);
lab1 = new Label("所查URL(例如:http://www.baidu.com):");
lab1.setBounds(10, 30, 120, 30);
lab2 = new Label("主机域名(例如:www.baidu.com):");
lab2.setBounds(10, 60, 120, 30);
lab3 = new Label("主机IP地址");
lab3.setBounds(10, 90, 120, 30);
but1 = new Button("查询");
but1.setBounds(276, 96, 50, 20);
but2 = new Button("退出");
but2.setBounds(330, 96, 50, 20);
add(lab1);
add(text1);
add(lab2);
add(text2);
add(lab3);
add(text3);
add(but1);
add(but2);
text1.addActionListener(this);
but1.addActionListener(this);
but2.addActionListener(this);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
try {
add = new URL(text1.getText().trim());
ip = InetAddress.getByName(add.getHost());
} catch (MalformedURLException murle) {
System.out.println(murle);
} catch (UnknownHostException uh) {
}
if (e.getSource() == text1) {
text2.setText("");
text3.setText("");
}
if (e.getSource() == but1) {
if (text1.getText() != "") {
text2.setText(ip.getHostName());
text3.setText(ip.getHostAddress());
}
}
if (e.getSource() == but2)
System.exit(0);
}
public static void main(String args[]) {
new CZIP();
}
}
评论