thieumao

Check IP

Apr 11th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. package checkip;
  2.  
  3. import java.awt.Button;
  4. import java.awt.FlowLayout;
  5. import java.awt.Frame;
  6. import java.awt.TextField;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.event.KeyListener;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import java.net.InetAddress;
  14. import java.net.UnknownHostException;
  15. import javax.swing.JOptionPane;
  16.  
  17. /**
  18.  *
  19.  * @author Thieu Mao
  20.  */
  21. public class CheckIP extends Frame implements ActionListener, KeyListener {
  22.  
  23.     private TextField tfInput;
  24.     private Button btEnter;
  25.     private String strAddress;
  26.     private InetAddress[] addresses;
  27.  
  28.     public static void main(String[] args) {
  29.         CheckIP frame = new CheckIP("Check IP - thieumao");
  30.         frame.pack();
  31.         frame.setVisible(true);
  32.     }
  33.  
  34.     public CheckIP(String title) {
  35.         super(title);
  36.         this.addWindowListener(new WindowAdapter() {
  37.             @Override
  38.             public void windowClosing(WindowEvent we) {
  39.                 System.exit(0);
  40.             }
  41.         });
  42.         tfInput = new TextField(20);
  43.         btEnter = new Button("Enter");
  44.         this.setLayout(new FlowLayout());
  45.         this.add(tfInput);
  46.         this.add(btEnter);
  47.         tfInput.addKeyListener(this);
  48.         btEnter.addActionListener(this);
  49.     }
  50.  
  51.     @Override
  52.     public void actionPerformed(ActionEvent e) {
  53.         if (e.getSource() == btEnter) {
  54.             strAddress = tfInput.getText();
  55.             try {
  56.                 addresses = InetAddress.getAllByName(strAddress);
  57.             } catch (UnknownHostException ex) {
  58.                 System.err.println("Cound not find " + strAddress);
  59.             }
  60.             if (addresses == null) {
  61.                 JOptionPane.showMessageDialog(null, "Cound not find " + strAddress);
  62.             } else {
  63.                 JOptionPane.showMessageDialog(null, addresses);
  64.             }
  65.             tfInput.requestFocus();
  66.         }
  67.     }
  68.  
  69.     @Override
  70.     public void keyTyped(KeyEvent e) {
  71.     }
  72.  
  73.     @Override
  74.     public void keyPressed(KeyEvent e) {
  75.         if (e.getKeyCode() == e.VK_ENTER) {
  76.             strAddress = tfInput.getText();
  77.             try {
  78.                 addresses = InetAddress.getAllByName(strAddress);
  79.             } catch (UnknownHostException ex) {
  80.                 System.err.println("Cound not find " + strAddress);
  81.             }
  82.             if (addresses == null) {
  83.                 JOptionPane.showMessageDialog(null, "Cound not find " + strAddress);
  84.             } else {
  85.                 JOptionPane.showMessageDialog(null, addresses);
  86.             }
  87.         }
  88.         if (e.getKeyCode() == e.VK_ESCAPE) {
  89.             System.exit(0);
  90.         }
  91.     }
  92.  
  93.     @Override
  94.     public void keyReleased(KeyEvent e) {
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment