请教如何在Swing界面中显示网页?

plplum 2009-11-06 12:27:08
请教如何在Swing界面中显示网页?

要能支持CSS,javascript,flash。

网页在浏览器中打开是什么样的,在Swing界面中就显示什么样的。

大虾们帮忙。。。。
...全文
1053 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangyong20081204 2009-11-06
  • 打赏
  • 举报
回复
楼上的正解。。。
invoked 2009-11-06
  • 打赏
  • 举报
回复
String url = "http://www.baidu.com";
invoked 2009-11-06
  • 打赏
  • 举报
回复

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

YBBPS1109 2009-11-06
  • 打赏
  • 举报
回复
用jdic吧,应该可以
LiuTaiYe 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 plplum 的回复:]
项目中不允许使用插件,更不用说dll了
引用 19 楼 ybbps1109 的回复:
应该不能吧,当时我没有找到,只找到这种方法

[/Quote]
如果是这样,你很难做到这点 “网页在浏览器中打开是什么样的,在Swing界面中就显示什么样的 ”。这个浏览器你指的是什么? 不同浏览器看同一个网页都可能显示不同(比如布局可能会变动)。
市场上没有纯的基于Swing的浏览器。简单的应用用Swing当然可以实现,如楼上给出的例子代码。如果你的项目要求比较强的网页处理能力和速度,我建议你说服项目经理去使用JDIC。使用native控件并不是什么可怕的事情。用JDIC不必担心跨平台的问题,你只需小量代码就可以在Windows和Linux下都可以用。
YBBPS1109 2009-11-06
  • 打赏
  • 举报
回复
没有办法了
plplum 2009-11-06
  • 打赏
  • 举报
回复
项目中不允许使用插件,更不用说dll了
[Quote=引用 19 楼 ybbps1109 的回复:]
应该不能吧,当时我没有找到,只找到这种方法
[/Quote]
YBBPS1109 2009-11-06
  • 打赏
  • 举报
回复
应该不能吧,当时我没有找到,只找到这种方法
plplum 2009-11-06
  • 打赏
  • 举报
回复
请问下在不使用第三方插件的情况下可以实现这样的效果吗?
[Quote=引用 17 楼 ybbps1109 的回复:]
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);
                }
            }
        });
    }
}

[/Quote]
YBBPS1109 2009-11-06
  • 打赏
  • 举报
回复
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);
}
}
});
}
}
plplum 2009-11-06
  • 打赏
  • 举报
回复
已可以运行了,下来要解决点连接时不要在新窗口打开

[Quote=引用 14 楼 ybbps1109 的回复:]
IeEmbed.exe这个你没有吧
[/Quote]
plplum 2009-11-06
  • 打赏
  • 举报
回复

它放在那?
[Quote=引用 14 楼 ybbps1109 的回复:]
IeEmbed.exe这个你没有吧
[/Quote]
YBBPS1109 2009-11-06
  • 打赏
  • 举报
回复
IeEmbed.exe这个你没有吧
plplum 2009-11-06
  • 打赏
  • 举报
回复
帮忙说说怎么配置?
jdic.jar,jdic.dll我都有了,但报错:

Can't execute the native embedded browser. Error message: Cannot run program "IeEmbed.exe": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶

[Quote=引用 11 楼 ybbps1109 的回复:]
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);

    }
}
[/Quote]
YBBPS1109 2009-11-06
  • 打赏
  • 举报
回复

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

}
}
YBBPS1109 2009-11-06
  • 打赏
  • 举报
回复
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);

}
}
plplum 2009-11-06
  • 打赏
  • 举报
回复
不行啊,显示出来的还是样式有问题。

[Quote=引用 2 楼 invoked 的回复:]
Java codeimport javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.IOException;publicclass Testextends JFrame {private JEditorPane edi¡­
[/Quote]
LiuTaiYe 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ybbps1109 的回复:]
用jdic吧,应该可以
[/Quote]

顶,这个应用很广,而且有跨平台版本。
invoked 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xingqiliudehuanghun 的回复:]
运行了下1楼那个例子,效果是出来了,不过样子实在丑的可以
[/Quote]

呵呵,时间仓促。
invoked 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xingqiliudehuanghun 的回复:]
没用Java做过客户端程序,一直做WEB开发,有个问题想问一下,按照一楼的写法嵌入的浏览器其版本会
不会随客户端浏览器的变化而变化,就想.net中的那个浏览器控件一样
[/Quote]

不会
加载更多回复(2)

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧