Load html in Swing GUI

來源:互聯網
上載者:User

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();
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.