在JAVA语言中,有什么语句可以直接执行,打开一个index.html文件
比如说在
button1.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
  //打开一个a.html文件,我怎么写  }
});

解决方案 »

  1.   

    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
       
    public class JEditorPaneExample extends JFrame implements
        HyperlinkListener
    {
        public static void main(String[] argv)
        {
            JEditorPaneExample mainApp = new JEditorPaneExample();
        }
        
        public JEditorPaneExample()
        {
            super("JEditorPaneExample Example");
            setBounds(0, 0, 600, 400);
            getContentPane().setLayout(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
                   
            // Attempt to load an 'index' html page...
            try
            {
                URL url = null;
            
               try
                {
                    url = getClass().getResource("index.html");
                }
                catch(Exception e)
                {
                    System.out.println("Could not open file!");
                    url = null;
                }
                
                if(url != null)
                {
                    htmlViewer = new JEditorPane(url);
                    htmlViewer.setEditable(false);
                    htmlViewer.addHyperlinkListener(this);
                
                    scrollpane = new JScrollPane();
                    scrollpane.setBounds(10, 10, 570, 350);
                    scrollpane.getViewport().add(htmlViewer);
                }
             }
             catch(MalformedURLException e)
             {
                 System.out.println(e);
             }
             catch(IOException e)
             {
                 System.out.println(e);
             }
             
            getContentPane().add(scrollpane);
            
            setVisible(true);
        }
        
        public void hyperlinkUpdate(HyperlinkEvent e)
        {
            if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
            {
                if(e instanceof HTMLFrameHyperlinkEvent)
                {
                    ((HTMLDocument) htmlViewer.getDocument()).processHTML
                        FrameHyperlinkEvent((HTMLFrameHyperlinkEvent) e);
                }
                else
                {
                    try
                    {
                        htmlViewer.setPage(e.getURL());
                    }
                    catch(IOException e2)
                    {
                        System.out.println(e2);
                    }
                }
            }
        }
           
        JEditorPane htmlViewer;
        JScrollPane scrollpane;
    }