Load html in Swing GUI

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class JTextPaneDemo
    extends JFrame
{

    static SimpleAttributeSet ITALIC_GRAY = new SimpleAttributeSet();

    static SimpleAttributeSet BOLD_BLACK = new SimpleAttributeSet();

    static SimpleAttributeSet BLACK = new SimpleAttributeSet();

    JTextPane textPane = new JTextPane();

    // Best to reuse attribute sets as much as possible.
    static
    {
        StyleConstants.setForeground(ITALIC_GRAY, Color.gray);
        StyleConstants.setItalic(ITALIC_GRAY, true);
        StyleConstants.setFontFamily(ITALIC_GRAY, "Helvetica");
        StyleConstants.setFontSize(ITALIC_GRAY, 14);

        StyleConstants.setForeground(BOLD_BLACK, Color.black);
        StyleConstants.setBold(BOLD_BLACK, true);
        StyleConstants.setFontFamily(BOLD_BLACK, "Helvetica");
        StyleConstants.setFontSize(BOLD_BLACK, 14);

        StyleConstants.setForeground(BLACK, Color.black);
        StyleConstants.setFontFamily(BLACK, "Helvetica");
        StyleConstants.setFontSize(BLACK, 14);
    }

    public JTextPaneDemo()
    {
        super("JTextPane Demo");

        JScrollPane scrollPane = new JScrollPane(textPane);
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        setEndSelection();
        textPane.insertIcon(new ImageIcon("java2sLogo.GIF"));
        insertText("/nWebsite for: www.java2s.com /n/n", BOLD_BLACK);

        setEndSelection();
        insertText("                                    ", BLACK);
        setEndSelection();
        insertText("/n      Java            " + "                                    "
                                   + "Source/n/n",
                   ITALIC_GRAY);

        insertText(" and Support. /n", BLACK);

        setEndSelection();
        JButton manningButton = new JButton("Load the web site for www.java2s.com");
        manningButton.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                textPane.setEditable(false);
                //set char set, it means encode of page file.
                textPane.setContentType("text/html;charset=utf-8");
                textPane.setEditable(false);

                try
                {
                    // load from classpath
                    // URL resource =
                    // JTextPaneDemo.class.getResource("testutf.html");
                    // textPane.setPage(resource);
                    // load local file
                    textPane.setPage("file:///D:/test.html");
                    // load web
                    textPane.setPage("http://www.google.com");
                }
                catch(IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
        });
        textPane.insertComponent(manningButton);

        setSize(500, 450);
        setVisible(true);
    }

    protected void insertText(String text, AttributeSet set)
    {
        try
        {
            textPane.getDocument().insertString(textPane.getDocument()
                                                        .getLength(),
                                                text,
                                                set);
        }
        catch(BadLocationException e)
        {
            e.printStackTrace();
        }
    }

    // Needed for inserting icons in the right places
    protected void setEndSelection()
    {
        textPane.setSelectionStart(textPane.getDocument().getLength());
        textPane.setSelectionEnd(textPane.getDocument().getLength());
    }

    public static void main(String argv[])
    {
        new JTextPaneDemo();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值