ch8_e8_28.java

来自「清华的Java程序」· Java 代码 · 共 56 行

JAVA
56
字号
import java.awt.*; 
import java.awt.event.*;
import java.net.*;

public class ch8_e8_28
{
    public static void main(String args[])
    {
        new MyFrame();
    }
}

class MyFrame extends Frame implements ActionListener
{
    TextField hostInputTfd = new TextField(50);
    Label ipOutputLbl = new Label();
    
    MyFrame()
    {
        super("主机与IP");
        ipOutputLbl.setText("                                   ");
        add(hostInputTfd,BorderLayout.NORTH);
        add(ipOutputLbl,BorderLayout.CENTER);
        pack();
        show();
        hostInputTfd.addActionListener(this);
        addWindowListener(new winClose());
    }
    
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == hostInputTfd)
        {
            try
            {
                InetAddress ip = InetAddress.getByName(
                    hostInputTfd.getText().trim());
                ipOutputLbl.setText(ip.toString());
            }
            catch(UnknownHostException uhe)
            {
                ipOutputLbl.setText("未找到指定的主机。");
            }
        }
    }
}

class winClose extends WindowAdapter
{
    public void windowClosing(WindowEvent we)
    {
        we.getWindow().dispose();
        System.exit(0);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?