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

📄 htmljframe.java

📁 《Java2程序设计实用教程(第2版)》课件
💻 JAVA
字号:
//【例10.1】  查看指定URL的HTML源文档内容。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class HtmlJFrame  extends JFrame implements ItemListener
{
    private URL url;                                       //URL地址
    private JComboBox combobox_url;                        //URL地址栏
    private JTextArea text_content;                        //文本区,显示文件内容
    private JTextField text_attribute;                     //文本行,显示文件属性

    public HtmlJFrame()
    {
        super("查看指定URL的HTML源文档内容");
        this.setSize(640,480);
        this.setLocation(300,240);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        String[] urls={"file://localhost/C:/Program Files/Java/docs/api/index.html", 
                       "http://www.edu.cn"};
        combobox_url = new JComboBox(urls);                //组合框,显示URL地址
        combobox_url.setEditable(true);
        this.add(combobox_url,"North");
        combobox_url.addItemListener(this);                //注册组合框的选择事件监听器

        text_content = new JTextArea();
        this.add(text_content);

        text_attribute = new JTextField();
        this.add(text_attribute,"South");
        this.setVisible(true);

        try
        {
            this.url = new URL(urls[0]);                   //创建一个URL对象
            this.readFromFile();                           //通过输入流获得URL表示文件的内容
            text_attribute.setText(this.getAttribute());   //获得文件属性
        }
        catch(MalformedURLException murle)
        {
            System.out.println(murle);
        }
    }

    public void readFromFile()                             //通过字符输入流获得URL表示的HTML文件内容
    {
        try
        {
            InputStreamReader in = new InputStreamReader(this.url.openStream());
            BufferedReader bin = new BufferedReader(in);
            String aline = "";
            do
            {
                aline = bin.readLine();
                if (aline!=null)
                    text_content.append(aline+"\r\n");
            }while (aline!=null);
            bin.close();
            in.close();
        }
        catch(IOException ioe)
        {
            System.out.println(ioe);
        }
    }
    
    public String getAttribute()                           //通过文件对象获得文件属性
    {
        String str_attr="文件:";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        
        if (url.getProtocol().equals("file"))             //当URL对象使用FILE协议时
        {
            File afile = new File(url.getFile());         //获得文件对象
            str_attr += afile.getAbsolutePath()+"\t";     //获得文件路径名
            str_attr += afile.length()+"B\t";             //获得文件长度
            str_attr += sdf.format(new Date(afile.lastModified()));  //获得文件最后修改时间
        }   

        if (url.getProtocol().equals("http"))             //当URL对象使用HTTP协议时
        {
            try
            {
                URLConnection urlconn = url.openConnection();
                str_attr += url.toString()+"\t";                     //获得URL路径名
                str_attr += urlconn.getContentLength()+"B\t";        //获得文件长度
                str_attr += urlconn.getContentType()+"\t";           //获得文件类型
                str_attr += sdf.format(new Date(urlconn.getLastModified()));   //获得文件最后修改时间
            }
            catch(IOException ioe)
            {
                System.out.println(ioe);
            }
        }   
        return str_attr;
    }

    public void itemStateChanged(ItemEvent e)            //在组合框的下拉列表中选择数据项时触发执行
    {
        String urlname = (String)combobox_url.getSelectedItem();     //获得组合框选中数据项的字符串
        try
        {
            this.url = new URL(urlname);
            this.readFromFile();
            text_attribute.setText(this.getAttribute());
        }
        catch(MalformedURLException murle)
        {
            System.out.println(murle);
        }
    }
    
    public static void main(String args[])
    {
        new HtmlJFrame();
    }
}

⌨️ 快捷键说明

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