请教如何在Swing界面中显示网页?要能支持CSS,javascript,flash。网页在浏览器中打开是什么样的,在Swing界面中就显示什么样的。大虾们帮忙。

解决方案 »

  1.   


    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.IOException;public class Test extends JFrame {
    private JEditorPane editorPane = new JEditorPane(); public Test() {
    Container contentPane = getContentPane();
    String url = "http://www.baidu.com"; try {
    editorPane.setPage(url);
    } catch (IOException ex) {
    ex.printStackTrace();
    } contentPane.add(new JScrollPane(editorPane), BorderLayout.CENTER); editorPane.setEditable(false); editorPane.addHyperlinkListener(new HyperlinkListener() {
    public void hyperlinkUpdate(HyperlinkEvent e) {
    try {
    editorPane.setPage(e.getURL());
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    });
    } public static void main(String args[]) {
    GJApp.launch(new Test(), "JEditorPane", 300, 300, 450, 300);
    }
    }class GJApp extends WindowAdapter {
    static private JPanel statusArea = new JPanel(); static private JLabel status = new JLabel(" "); static private ResourceBundle resources; public static void launch(final JFrame f, String title, final int x,
    final int y, final int w, int h) {
    launch(f, title, x, y, w, h, null);
    } public static void launch(final JFrame f, String title, final int x,
    final int y, final int w, int h, String propertiesFilename) {
    f.setTitle(title);
    f.setBounds(x, y, w, h);
    f.setVisible(true); statusArea.setBorder(BorderFactory.createEtchedBorder());
    statusArea.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    statusArea.add(status);
    status.setHorizontalAlignment(JLabel.LEFT); f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); if (propertiesFilename != null) {
    resources = ResourceBundle.getBundle(propertiesFilename, Locale
    .getDefault());
    } f.addWindowListener(new WindowAdapter() {
    public void windowClosed(WindowEvent e) {
    System.exit(0);
    }
    });
    } static public JPanel getStatusArea() {
    return statusArea;
    } static public void showStatus(String s) {
    status.setText(s);
    } static Object getResource(String key) {
    if (resources != null) {
    return resources.getString(key);
    }
    return null;
    }
    }
      

  2.   

    String url = "http://www.baidu.com";
      

  3.   

    没用Java做过客户端程序,一直做WEB开发,有个问题想问一下,按照一楼的写法嵌入的浏览器其版本会
    不会随客户端浏览器的变化而变化,就想.net中的那个浏览器控件一样
      

  4.   

    package demo;import java.net.MalformedURLException;
    import java.net.URL;
    import javax.swing.JFrame;
    import org.jdesktop.jdic.browser.WebBrowser;/**
     *
     * @author YBBPS
     */
    public class Mytest {    public static void main(String[] args) throws MalformedURLException {
            WebBrowser wb;
            wb = new WebBrowser();
            wb.setURL(new URL("http://www.renren.com"));
            JFrame frame = new JFrame("MyTest");
            frame.getContentPane().add(wb);
            frame.setSize(800, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);    }
    }
      

  5.   

    [code]
    package demo;import java.net.MalformedURLException;
    import java.net.URL;
    import javax.swing.JFrame;
    import org.jdesktop.jdic.browser.WebBrowser;/**
     *
     * @author YBBPS
     */
    public class Mytest {    public static void main(String[] args) throws MalformedURLException {
            WebBrowser wb;
            wb = new WebBrowser();
            wb.setURL(new URL("http://www.renren.com"));
            JFrame frame = new JFrame("MyTest");
            frame.getContentPane().add(wb);
            frame.setSize(800, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);    }
    }
    [/code]
      

  6.   

    帮忙说说怎么配置?
    jdic.jar,jdic.dll我都有了,但报错:Can't execute the native embedded browser. Error message: Cannot run program "IeEmbed.exe": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
      

  7.   

    package demo;import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import org.jdesktop.jdic.browser.WebBrowser;/**
     *
     * @author YBBPS
     */
    public class Mytest {    static WebBrowser wb;
        public static void main(String[] args) throws MalformedURLException {
            wb = new WebBrowser();
            wb.setURL(new URL("http://www.renren.com"));
            JFrame frame = new JFrame("MyTest");
            frame.getContentPane().add(wb, BorderLayout.CENTER);
            frame.setSize(800, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            JButton button = new JButton("哈");
            frame.add(button, BorderLayout.NORTH);
            button.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    try {
                        wb.setURL(new URL("http://www.baidu.com"));
                    } catch (MalformedURLException ex) {
                        Logger.getLogger(Mytest.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }
    }
      

  8.   

    项目中不允许使用插件,更不用说dll了
      

  9.   

    如果是这样,你很难做到这点 “网页在浏览器中打开是什么样的,在Swing界面中就显示什么样的 ”。这个浏览器你指的是什么? 不同浏览器看同一个网页都可能显示不同(比如布局可能会变动)。
    市场上没有纯的基于Swing的浏览器。简单的应用用Swing当然可以实现,如楼上给出的例子代码。如果你的项目要求比较强的网页处理能力和速度,我建议你说服项目经理去使用JDIC。使用native控件并不是什么可怕的事情。用JDIC不必担心跨平台的问题,你只需小量代码就可以在Windows和Linux下都可以用。