JEditorPane是什么????
听说javax.swing.JEditorPane可以处理一些关于HTML的问题,
javax.swing.JEditorPane是一个包还是一个控件?哪可以下载,为什么网上找不到呢,

解决方案 »

  1.   

    当然是一个控件了。!
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.event.HyperlinkEvent;
    import javax.swing.event.HyperlinkListener;public class Test extends JFrame implements ActionListener,
            HyperlinkListener
    {
        private JLabel labUrl = new JLabel("URL 地址:");
        private JTextField txtUrl = new JTextField(20);
        private JEditorPane ep = new JEditorPane();
        private JPanel panel = new JPanel();    public Test()
        {
            
            
            txtUrl.addActionListener(this);
            ep.addHyperlinkListener(this);
            
            panel.setLayout(new BorderLayout());
            panel.add(labUrl,BorderLayout.WEST);
            panel.add(txtUrl,BorderLayout.CENTER);
            
            this.setLayout(new BorderLayout());
            this.add(panel,BorderLayout.NORTH);
            this.add(new JScrollPane(ep),BorderLayout.CENTER);
        }    public void actionPerformed(ActionEvent e)
        {
            URL url;
            try
            {
                url = new URL(txtUrl.getText());
                ep.setPage(url);
            } catch (MalformedURLException e1)
            {
                e1.printStackTrace();
            } catch (IOException e2)
            {
                e2.printStackTrace();
            }
        }    public void hyperlinkUpdate(HyperlinkEvent e)
        {
            URL url = e.getURL();
            txtUrl.setText(url.toString());
            try
            {
                ep.setPage(url);
            } catch (IOException e1)
            {
                e1.printStackTrace();
            }
            //...这里就可以写到达这个swing系统中其他panel页面的代码
        }
        
        public static void main(String[] args)
        {
            Test frame = new Test();
            frame.setTitle("简单的浏览器");
            frame.setSize(640, 480);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
      

  2.   


    谢谢,还有点事问问你,比如你给的这个例子,只能打开WWW网页,如果想要打开本地的网页要怎样实现呢,比如:C:\Documents and Settings\a\My Documents\Untitled-4.html
      

  3.   

    使用 JFileChooser 选择一个HTML 文件 htmlFile。调用 JEditorPane 的 setPage(htmlFile.toURI().toURL())
      

  4.   

    简单,在本地路径前面加file:/:\Documents and Settings\a\My Documents\Untitled-4.html
      

  5.   


    file:/:\Documents and Settings\a\My Documents\Untitled-4.html
    这能行????
      

  6.   


    file:/:\Documents and Settings\a\My Documents\Untitled-4.html不行啊,快点我急用
      

  7.   

    file:/C:\Documents and Settings\a\My Documents\Untitled-4.html
    这样!
      

  8.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
    import java.net.*;public class JEditorPaneExample extends JFrame{
        private JEditorPane editorPane = new JEditorPane();
        private JTextField urlField = new JTextField();
        private JFileChooser fileChooser = new JFileChooser();
        private JToolBar toolbar = new JToolBar();
        private Action openAction = new AbstractAction("Open"){
        {
    putValue(Action.SHORT_DESCRIPTION,"Open a HTML File");
    putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke("control O"));
        }     public void actionPerformed(ActionEvent evt){
    try{
        fileChooser.showOpenDialog(toolbar);
        File selectedFile = fileChooser.getSelectedFile();
        if(selectedFile == null) return;
        URL url = selectedFile.toURI().toURL();
        editorPane.setPage(url);
        urlField.setText(url.toString());
    } catch(MalformedURLException e){
        System.err.println(e);
    } catch(IOException e){
        System.err.println(e);
    }
        }
    };    public JEditorPaneExample(){
    super("JEditorPane Example");
    init();
    getContentPane().add(toolbar,BorderLayout.NORTH);
    getContentPane().add(new JScrollPane(editorPane),BorderLayout.CENTER); setDefaultCloseOperation(EXIT_ON_CLOSE);
    setPreferredSize(new Dimension(800,600));
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
        }    private void init(){
    toolbar.add(openAction);
    toolbar.add(urlField);
    urlField.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        try {
    editorPane.setPage(urlField.getText());
        } catch (IOException e){
    e.printStackTrace();
        }
    }
        });
    editorPane.addHyperlinkListener(new HyperlinkListener(){
    public void hyperlinkUpdate(HyperlinkEvent evt)
    {
        URL url = evt.getURL();
        urlField.setText(url.toString());
        try {
    editorPane.setPage(url);
        } catch (IOException e){
    e.printStackTrace();
        }
    }
        });
    fileChooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter(){
    public boolean accept(File file) {
        String filename = file.getName();
        return file.isDirectory() || filename.endsWith(".html") || filename.endsWith(".htm");
    }
    public String getDescription() {
        return "HTML Files";
    }
        });
        }    public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable(){
    public void run(){
        new JEditorPaneExample();
    }
        });
        }
    }
      

  9.   

    有空格的本地路径:file:/home/alphabeta/Templates/HTML%204.01/HTML%204.01%20Strict%20Document.html