Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package checkip;
- import java.awt.Button;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import javax.swing.JOptionPane;
- /**
- *
- * @author Thieu Mao
- */
- public class CheckIP extends Frame implements ActionListener, KeyListener {
- private TextField tfInput;
- private Button btEnter;
- private String strAddress;
- private InetAddress[] addresses;
- public static void main(String[] args) {
- CheckIP frame = new CheckIP("Check IP - thieumao");
- frame.pack();
- frame.setVisible(true);
- }
- public CheckIP(String title) {
- super(title);
- this.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent we) {
- System.exit(0);
- }
- });
- tfInput = new TextField(20);
- btEnter = new Button("Enter");
- this.setLayout(new FlowLayout());
- this.add(tfInput);
- this.add(btEnter);
- tfInput.addKeyListener(this);
- btEnter.addActionListener(this);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btEnter) {
- strAddress = tfInput.getText();
- try {
- addresses = InetAddress.getAllByName(strAddress);
- } catch (UnknownHostException ex) {
- System.err.println("Cound not find " + strAddress);
- }
- if (addresses == null) {
- JOptionPane.showMessageDialog(null, "Cound not find " + strAddress);
- } else {
- JOptionPane.showMessageDialog(null, addresses);
- }
- tfInput.requestFocus();
- }
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == e.VK_ENTER) {
- strAddress = tfInput.getText();
- try {
- addresses = InetAddress.getAllByName(strAddress);
- } catch (UnknownHostException ex) {
- System.err.println("Cound not find " + strAddress);
- }
- if (addresses == null) {
- JOptionPane.showMessageDialog(null, "Cound not find " + strAddress);
- } else {
- JOptionPane.showMessageDialog(null, addresses);
- }
- }
- if (e.getKeyCode() == e.VK_ESCAPE) {
- System.exit(0);
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment