⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jhostname.java

📁 Java 范例实战 光盘使用说明 ========================== 本光盘的文件结构如下所示: =====================================
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.*;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class jHostName extends Applet {
    InetAddress localHost = null;
    FontMetrics fontMatric = null;
    Font dispFont = null;
    Color txtColor = null,
          bgColor = null;
    String fontName = null,
           fromHost = null,
           preString = null,
           postString = null;
    int fontSize = 0,
        fontStyle = 0;

    public jHostName()
    {
        fromHost = new String();
        preString = new String();
        postString = new String();
    }

    public void init()
    {
        String param;

        fontSize = (param = getParameter("Font_Size")) == null ?
            24 : Integer.parseInt(param);
        txtColor = (param = getParameter("Text_Color")) == null ?
            Color.lightGray :
            new Color(Integer.parseInt(param, 16));
        bgColor = (param = getParameter("Bg_Color")) == null ?
            Color.black : new Color(Integer.parseInt(param, 16));

        fontName = getParameter("Font_Name");
        
        if(fontName == null)
            fontName = new String("TimesRoman");
        else if(fontName.equalsIgnoreCase("Helvetica"))
            fontName = new String("Helvetica");
        else if(fontName.equalsIgnoreCase("Couier"))
            fontName = new String("Courier");
        else
            fontName = new String("TimesRoman");

        if((param = getParameter("Font_Style")) == null)
            fontStyle = 1;
        else if(param.equalsIgnoreCase("Plain"))
            fontStyle = 0;
        else if(param.equalsIgnoreCase("Italic"))
            fontStyle = 2;
        else if(param.equalsIgnoreCase("BoldItalic"))
            fontStyle = 3;
        else
            fontStyle = 1;

        try
        {
            localHost = InetAddress.getLocalHost();
            fromHost = localHost.getHostName();
        }
        catch(UnknownHostException _ex)
        {
            fromHost = "Unidentified Visitor";
        }

        preString = getParameter("preString");
        
        if(preString == null)
            preString = " ";
        
        postString = getParameter("postString");
        
        if(postString == null)
            postString = " ";
        
        fromHost = preString + " " + fromHost + " " + postString;
        dispFont = new Font(fontName, fontStyle, fontSize);
        
        for(fontMatric = getFontMetrics(dispFont); 
            fontMatric.stringWidth(fromHost) > getSize().width;
            fontMatric = getFontMetrics(dispFont))
        {
            fontSize = fontSize - 2;
            dispFont = new Font(fontName, fontStyle, fontSize);
        }

        while(fontMatric.getHeight() > getSize().height - 10)
        {
            fontSize = fontSize - 4;
            dispFont = new Font(fontName, fontStyle, fontSize);
            fontMatric = getFontMetrics(dispFont);
        }

    }

    public void paint(Graphics g)
    {
        int i = (getSize().width-fontMatric.stringWidth(fromHost))
                /2;
        int j = (getSize().height+fontMatric.getHeight())
                /2;

        g.setFont(dispFont);
        g.setColor(bgColor);
        g.fillRect(0, 0, getSize().width, getSize().height);
        g.setColor(txtColor);
        g.drawString(fromHost, i, j);
    }
}

⌨️ 快捷键说明

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