请教如何在Swing界面中显示网页? 请教如何在Swing界面中显示网页?要能支持CSS,javascript,flash。网页在浏览器中打开是什么样的,在Swing界面中就显示什么样的。大虾们帮忙。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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; }} String url = "http://www.baidu.com"; 没用Java做过客户端程序,一直做WEB开发,有个问题想问一下,按照一楼的写法嵌入的浏览器其版本会不会随客户端浏览器的变化而变化,就想.net中的那个浏览器控件一样 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]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] 帮忙说说怎么配置?jdic.jar,jdic.dll我都有了,但报错:Can't execute the native embedded browser. Error message: Cannot run program "IeEmbed.exe": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶ 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); } } }); }} 项目中不允许使用插件,更不用说dll了 如果是这样,你很难做到这点 “网页在浏览器中打开是什么样的,在Swing界面中就显示什么样的 ”。这个浏览器你指的是什么? 不同浏览器看同一个网页都可能显示不同(比如布局可能会变动)。市场上没有纯的基于Swing的浏览器。简单的应用用Swing当然可以实现,如楼上给出的例子代码。如果你的项目要求比较强的网页处理能力和速度,我建议你说服项目经理去使用JDIC。使用native控件并不是什么可怕的事情。用JDIC不必担心跨平台的问题,你只需小量代码就可以在Windows和Linux下都可以用。 hibernate+spring+jsp怎么实现分页? 求<<java数据结构和算法>>中文第二版 课后编程作业的源代码 如何将ResultSet返回成ARRAYLIST哦 初学者,编译没有问题,却不能运行? executeBatch()的返回值问题 关于调用系统声音的问题?请高手指教! 在document对象里怎么分离出图片和文字? jbx可以制作.exe文件 但JBuilder 2005 中制作.exe文件是怎麼回事 如何用JAVA把数据库的内容打印成标签??? 怎样解决JButton上中文是方框的问题???????? 求一双向键表和一循环键表! 小小的聊天室程序`连接异常
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;
}
}
不会随客户端浏览器的变化而变化,就想.net中的那个浏览器控件一样
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); }
}
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]
jdic.jar,jdic.dll我都有了,但报错:Can't execute the native embedded browser. Error message: Cannot run program "IeEmbed.exe": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
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);
}
}
});
}
}
市场上没有纯的基于Swing的浏览器。简单的应用用Swing当然可以实现,如楼上给出的例子代码。如果你的项目要求比较强的网页处理能力和速度,我建议你说服项目经理去使用JDIC。使用native控件并不是什么可怕的事情。用JDIC不必担心跨平台的问题,你只需小量代码就可以在Windows和Linux下都可以用。