public class MyLocalIPAddress
1: // MyLocalIPAddress.java
2: // Finds the IP address of your local machine.
4: import java.awt.*;
5: import java.awt.event.*;
6: import java.net.*;
8: public class MyLocalIPAddress
9: extends Frame
10: implements ActionListener
11: {
12: private Button btnDisplayIp;
13: private TextField txtIpAddress;
14: private Button exit;
16: public static void main(String [] args)
17: {
18: MyLocalIPAddress m = new MyLocalIPAddress();
19: m.makeGUI();
20: m.setSize(300, 120);
21: m.setVisible(true);
22: }
24: public void makeGUI()
25: {
26: setLayout(new FlowLayout());
28: btnDisplayIp = new Button("Display local IP address");
29: txtIpAddress = new TextField(40);
30: btnDisplayIp.addActionListener(this);
31: txtIpAddress.addActionListener(this);
32: add(btnDisplayIp);
33: add(txtIpAddress);
35: exit = new Button("Exit");
36: add(exit);
37: exit.addActionListener(this);
38: }
40: public void actionPerformed(ActionEvent event)
41: {
42: if (event.getSource() == exit) System.exit(0);
44: try
45: {
46: InetAddress address = InetAddress.getLocalHost();
47: txtIpAddress.setText(address + "");
48: }
49: catch (UnknownHostException e)
50: {
51: txtIpAddress.setText("Could not find the local address");
52: }
53: }
54: }