最近在一个网站上看图,某一个帖子的图片都在一个页面,往下滚动的时候感觉眼花并且看不全,所以想开发一个简单的浏览器,实现功能就是填一个地址,然后把所有的图片解析出来,按上下键来浏览图片:
我用HtmlUnit很容易就解析到了所有图片:
HtmlPage page=webClient.getPage("http://www.xxx.com");
Iterable<HtmlElement> iterable=page.getAllHtmlChildElements();
Iterator<HtmlElement> hIterator=iterable.iterator();
while (hIterator.hasNext()) {
HtmlElement htmlElement = (HtmlElement) hIterator.next();
if (htmlElement instanceof HtmlImage) {
String imageurl=((HtmlImage)htmlElement).getAttributeValue("src");
ShareURL.imageURLs.add(new URL(imageurl));
}
}
但是我得把图片显示出来在Java的JFRAME中,怎么办呢?我在Sun的Tutorial中搜到一个办法,就是利用Swing的控件来实现:
http://java.sun.com/docs/books/tutorial/uiswing/components/html.html
...
String initialText = "<html>\n" +
"Color and font test:\n" +
"<ul>\n" +
"<li><font color=red>red</font>\n" +
"<li><font color=blue>blue</font>\n" +
"<li><font color=green>green</font>\n" +
"<li><font size=-2>small</font>\n" +
"<li><font size=+2>large</font>\n" +
"<li><i>italic</i>\n" +
"<li><b>bold</b>\n" +
"</ul>\n";
htmlTextArea = new JTextArea(10, 20);
htmlTextArea.setText(initialText);
...
theLabel = new JLabel(initialText);
...
大概就是JLabel能够显示Html代码,JButton也可以用用Html代码生成,这个link也有:
button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");
但是因为我解析到的图片路径是远程的,不知为什么显示不出来,所以就想用嵌入浏览器的方式来实现,从google又搜索了一下,找到了标题中的EZJComIExplorer18,利用他的jar包中提供的方法很容易就实现了显示图片的功能,当然因为它可以嵌入到JFrame中,所以可准确控制它的navigation路径,上下按钮也可以用了,并且这个貌似还有其它功能,可以调用Microsoft的ActiveX控件.具体可以到他们的网站上了解.
[url]
http://ezjcom.com/
[/url]
我的嵌入浏览器到JFrame的代码大概是这段Sample code:
// Shows how to embed Internet Explorer in a Swing panel.
// Note that the examples of using IE Java API, as shown in
// other sample files, can also be used on this instance
// of IE.
import iexplorer.InternetExplorer;
import iexplorer.IWebBrowserApp;
import iexplorer.WebBrowser;
import ezjcom.JComObject;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class WebFrame extends JFrame {
WebBrowser browser;
JButton button = new JButton();
JLabel label = new JLabel( "URL: " );;
JTextField url = new JTextField( 12 );
JPanel buttonPanel = new JPanel();
/** TO BE DONE
** Change the string below to the "home" site for this program.
**/
String home = "http://www.your-home-site.com/";
// This method is called when the Test button is clicked.
void onButtonClick()
{
try {
// Navigate to the URL typed.
browser.getIWebBrowser2().Navigate( url.getText());
} catch (Exception ex) {
ex.printStackTrace();
}
}
void showBrowser()
{
try {
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
getContentPane().setLayout( new BorderLayout());
// Create a container instance and add it to the frame.
ezjcom.JComActiveXContainer activexContainer =
new ezjcom.JComActiveXContainer();
getContentPane().add( activexContainer, BorderLayout.CENTER );
button.setText( "Navigate" );
buttonPanel.add( label );
buttonPanel.add( url );
buttonPanel.add( button );
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
button.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) { onButtonClick(); }
});
// Create the ActiveX object and attach it to the container.
browser = new WebBrowser();
activexContainer.setActiveX( browser );
// Provide a home.
browser.getIWebBrowser2().Navigate( home );
// Show the frame.
setSize( 600, 400 );
setVisible( true );
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main( String[] args )
{
new WebFrame().showBrowser();
}
}