Source of IPFinderGUIApp.java


  1: //IPFinderGUIApp.java
  2: //Finds the IP address of a domain name.

  4: import java.awt.*;
  5: import java.net.*;
  6: import java.io.*;
  7: import java.awt.event.*;
  8: import javax.swing.*;

 10: public class IPFinderGUIApp extends JFrame
 11: implements ActionListener
 12: {

 14:     private JLabel txtLabel;
 15:     private JTextField txtOutput, txtInput;
 16:     private JButton btnDisplay;
 17:     private JButton exit;

 19:     public static void main(String [] args)
 20:     {
 21:         IPFinderGUIApp app = new IPFinderGUIApp();
 22:         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 23:         app.makeGUI();
 24:         app.setSize(400, 200);
 25:         app.setVisible(true);
 26:     }

 28:     public void makeGUI()
 29:     {
 30:         getContentPane().setLayout(new GridLayout(5, 1));

 32:         txtLabel = new JLabel("Enter your domain name in the space below:",
 33:                               SwingConstants.CENTER);
 34:         getContentPane().add(txtLabel);

 36:         txtInput = new JTextField();
 37:         txtOutput = new JTextField();
 38:         btnDisplay = new JButton("Click Here to See Corresponding " +
 39:                                  "IP Address Below");
 40:         btnDisplay.addActionListener(this);
 41:         getContentPane().add(txtInput);
 42:         getContentPane().add(btnDisplay);
 43:         getContentPane().add(txtOutput);

 45:         exit = new JButton("Exit");
 46:         getContentPane().add(exit);
 47:         exit.addActionListener(this);
 48:     }
 49: 
 50:     public void actionPerformed(ActionEvent event)
 51:     {
 52:         if (event.getSource() == exit) System.exit(0);
 53:         String host = txtInput.getText();
 54:         try
 55:         {
 56:               InetAddress address = InetAddress.getByName(host);
 57:               txtOutput.setText(address.toString());
 58:         }
 59:         catch (UnknownHostException e)
 60:         {
 61:             txtOutput.setText("Could not find " + host + ".");
 62:         }
 63:    }
 64: }