请问如何在java应用程序里面,制作一个类似web浏览器的东西 自己分析html实在是很高科技的东西,我不在行,想想就知道很复杂 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可行啊就是一个词法分析器而已不过要分析的可不光是html还有css,dhtml,script等可不是很简单的 我看意义不大因为 需要了解太多的 spacification而且 只有启动了jvm才能用 那么其安全性和速度可想而知换一个方面想想:当前的IE Mozilla 都不能很好的支持xml因为他的解析器太灵活了,与此相同很多新的协议你要看,要支持不容易啊 我建议你如果真的想学学java 换种方式这个目标太高了,也不是你一人儿能干的。 唉,VC里面都有很好的组件可以直接显示web页面。 很多SWING組件都提供HTML的分析功能﹐比如JEditorPane,你只要再實現一個HyperlinkListener接口﹐處理點擊超連接的事件就好了 以下是我的一個程序中的部分代碼﹐這是我為了做一個JAVA幫助程序寫的﹐你可以參考一下package zr.net.browser;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.net.*;import java.util.*;import java.io.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class Browser extends JFrame implements HyperlinkListener{ JEditorPane epanHtml; //To display the html. JLabel labMessage; //To show message. JTextField texfURL; //To edit and display the url. JFileChooser fchoFile; //To choose local html file. java.util.List history = new ArrayList(); //The list of history record. int curHistory = -1; public static final int MAX_HISTORY = 50; String home; //Default home page. ForwardAction fAction; BackAction bAction; public Browser() { super(); epanHtml = new JEditorPane(); epanHtml.setEditable(false); epanHtml.addHyperlinkListener(this); this.getContentPane().add(new JScrollPane(epanHtml),BorderLayout.CENTER); labMessage = new JLabel(" "); this.getContentPane().add(labMessage,BorderLayout.SOUTH); texfURL = new JTextField(); texfURL.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ displayPage(texfURL.getText()); } }); JToolBar toolBar = new JToolBar(); fAction = new ForwardAction("Forward",null); bAction = new BackAction("Back",null); fAction.setEnabled(false); bAction.setEnabled(false); toolBar.add(bAction); toolBar.add(fAction); toolBar.add(new JLabel(" URL:")); toolBar.add(texfURL); this.getContentPane().add(toolBar,BorderLayout.NORTH); JMenuBar menuBar = new JMenuBar(); JMenu menuFile = new JMenu("File"); JMenuItem openMenu = menuFile.add(new OpenAction("Open",null)); openMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); JMenuItem closeMenu = menuFile.add(new CloseAction("Close",null)); closeMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); menuBar.add(menuFile); JMenu menufb = new JMenu("Action"); menufb.add(fAction); menufb.add(bAction); this.setJMenuBar(menuBar); menuBar.add(menufb); } /** * Set the default home page * @param home home page url. */ public void setHome(String home){ this.home = home; } /** * Load the html page from dedicated url * @param url The url dedicate which page should be load * @return If load success,return true,else,return false */ public boolean visit(URL url){ try{ String href = url.toString(); startAnimation("Loading " + href + "..."); epanHtml.setPage(url); texfURL.setText(href); stopAnimation(); return true; } catch(Exception e){ stopAnimation(); labMessage.setText("Can't load page: " + e.getMessage()); return false; } } /** * Display page * @param url The url which should be displayed. */ public void displayPage(URL url){ if (!this.isVisible()){ this.setVisible(true); } if (this.getState() == JFrame.ICONIFIED){ this.setExtendedState(JFrame.NORMAL); //this.processWindowEvent(new WindowEvent(this,WindowEvent.WINDOW_ICONIFIED)); System.out.println("a"); //this.setVisible(true); } if(visit(url)){ history.add(url); int numentries = history.size(); if (numentries > MAX_HISTORY + 10){ history = history.subList(numentries - MAX_HISTORY,numentries); numentries = MAX_HISTORY; } curHistory = numentries - 1; if (curHistory > 0) bAction.setEnabled(true); } } public void displayPage(String href){ try { displayPage(new URL(href)); } catch (MalformedURLException ex) { System.err.println("Error: " + ex.toString() ); } } private String animationMessage; int animationFrame; private String[] animationFrames = new String[]{ "-","\\","|","/","-","\\","|","/",",",".","o","0","O","#","*","+" }; javax.swing.Timer animator = new javax.swing.Timer(125,new ActionListener(){ public void actionPerformed(ActionEvent e){ animate(); } }); /** * Display the animate */ private void animate(){ String frame = animationFrames[animationFrame++]; labMessage.setText(animationMessage + " " + frame); animationFrame = animationFrame % animationFrames.length; } public void hyperlinkUpdate(HyperlinkEvent e){ HyperlinkEvent.EventType type = e.getEventType(); if (type == HyperlinkEvent.EventType.ACTIVATED){ displayPage(e.getURL()); } else if ( type == HyperlinkEvent.EventType.ENTERED ){ labMessage.setText(e.getURL().toString()); } else if (type == HyperlinkEvent.EventType.EXITED){ labMessage.setText(" "); } } /** * Start the animate. * @param msg The message which you want to show. */ private void startAnimation(String msg){ animationMessage = msg; animationFrame = 0; animator.start(); } /** * Stop the animate. */ private void stopAnimation(){ animator.stop(); labMessage.setText(" "); } public void back(){ if (curHistory > 0){ visit((URL)history.get(--curHistory)); } fAction.setEnabled(curHistory < history.size() - 1); bAction.setEnabled(curHistory > 0); } public void forward(){ if(curHistory < history.size() - 1){ visit((URL)history.get(++curHistory)); } fAction.setEnabled(curHistory < history.size() - 1); bAction.setEnabled(curHistory > 0); } class BackAction extends AbstractAction{ public BackAction(String name,Icon icon){ this.putValue(Action.NAME,name); this.putValue(Action.SMALL_ICON,icon); } public void actionPerformed(ActionEvent evt){ Browser.this.back(); } } class ForwardAction extends AbstractAction{ public ForwardAction(String name,Icon icon){ this.putValue(Action.NAME,name); this.putValue(Action.SMALL_ICON,icon); } public void actionPerformed(ActionEvent evt){ Browser.this.forward(); } } class CloseAction extends AbstractAction{ public CloseAction(String name,Icon icon){ this.putValue(Action.NAME,name); this.putValue(Action.SMALL_ICON,icon); } public void actionPerformed(ActionEvent evt){ // Browser.this.setVisible(false); Browser.this.processWindowEvent(new WindowEvent(Browser.this,WindowEvent.WINDOW_CLOSING)); } } class OpenAction extends AbstractAction{ public OpenAction(String name,Icon icon){ this.putValue(Action.NAME,name); this.putValue(Action.SMALL_ICON,icon); } public void actionPerformed(ActionEvent evt){ if (Browser.this.fchoFile == null){ Browser.this.fchoFile = new JFileChooser(); javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter(){ public boolean accept(File f){ String fn = f.getName(); if(f.isDirectory() || fn.endsWith(".html") || fn.endsWith(".htm")) return true; else return false; } public String getDescription(){ return "Find html files"; } }; fchoFile.setFileFilter(filter); fchoFile.addChoosableFileFilter(filter); } int result = fchoFile.showOpenDialog(Browser.this); if (result == JFileChooser.APPROVE_OPTION){ File selectFile = fchoFile.getSelectedFile(); String url = "file:/" + selectFile.getAbsolutePath(); displayPage(url); } } } public static void main(String[] args){ Browser browser = new Browser(); browser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); browser.setSize(800,600); browser.setVisible(true); browser.displayPage("file:/E:/help/java/index.html"); }} 建议找一个开源的纯java browser来研究,source forge很多的,现在从头写一是不实际,而是穷一人之力,很难有作为的。 java窗体中的textbox事件 Spring的注入方式和依赖注入方式有什么区别? 如何用JAVA做一个简单的音乐播放器? 看看这段纯JAVA访问windows注册表,会的不会的都看下,没多少分 java生成.exe文件在红旗linux下运行显示汉字为小方块的问题 exe4j连结多个class文件的制作方法 请教连续四个JComboBox()的问题 谢谢 关于如何判断程序运行完成的问题,在线等候 为什么程序不能退出?解决就马上给分。 如何在JAVA中使用时间器?(新手提问) java api中有实现文件夹的拷贝功能吗? 急需解决:TextField的文本区域太小,怎么办呀?
就是一个词法分析器而已不过要分析的可不光是html
还有css,dhtml,script等可不是很简单的
因为 需要了解太多的 spacification
而且 只有启动了jvm才能用 那么其安全性和速度可想而知
换一个方面想想:当前的IE Mozilla 都不能很好的支持xml
因为他的解析器太灵活了,与此相同很多新的协议你要看,要支持
不容易啊 我建议你如果真的想学学java 换种方式
这个目标太高了,也不是你一人儿能干的。
package zr.net.browser;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.util.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Browser extends JFrame implements HyperlinkListener{
JEditorPane epanHtml; //To display the html.
JLabel labMessage; //To show message.
JTextField texfURL; //To edit and display the url.
JFileChooser fchoFile; //To choose local html file.
java.util.List history = new ArrayList(); //The list of history record.
int curHistory = -1;
public static final int MAX_HISTORY = 50;
String home; //Default home page.
ForwardAction fAction;
BackAction bAction; public Browser() {
super();
epanHtml = new JEditorPane();
epanHtml.setEditable(false);
epanHtml.addHyperlinkListener(this);
this.getContentPane().add(new JScrollPane(epanHtml),BorderLayout.CENTER);
labMessage = new JLabel(" ");
this.getContentPane().add(labMessage,BorderLayout.SOUTH);
texfURL = new JTextField();
texfURL.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
displayPage(texfURL.getText());
}
});
JToolBar toolBar = new JToolBar();
fAction = new ForwardAction("Forward",null);
bAction = new BackAction("Back",null);
fAction.setEnabled(false);
bAction.setEnabled(false);
toolBar.add(bAction);
toolBar.add(fAction);
toolBar.add(new JLabel(" URL:"));
toolBar.add(texfURL);
this.getContentPane().add(toolBar,BorderLayout.NORTH);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem openMenu = menuFile.add(new OpenAction("Open",null));
openMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
JMenuItem closeMenu = menuFile.add(new CloseAction("Close",null));
closeMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
menuBar.add(menuFile);
JMenu menufb = new JMenu("Action");
menufb.add(fAction);
menufb.add(bAction);
this.setJMenuBar(menuBar);
menuBar.add(menufb);
} /**
* Set the default home page
* @param home home page url.
*/
public void setHome(String home){
this.home = home;
}
/**
* Load the html page from dedicated url
* @param url The url dedicate which page should be load
* @return If load success,return true,else,return false
*/
public boolean visit(URL url){
try{
String href = url.toString();
startAnimation("Loading " + href + "...");
epanHtml.setPage(url);
texfURL.setText(href);
stopAnimation();
return true;
}
catch(Exception e){
stopAnimation();
labMessage.setText("Can't load page: " + e.getMessage());
return false;
}
}
/**
* Display page
* @param url The url which should be displayed.
*/
public void displayPage(URL url){
if (!this.isVisible()){
this.setVisible(true);
}
if (this.getState() == JFrame.ICONIFIED){
this.setExtendedState(JFrame.NORMAL);
//this.processWindowEvent(new WindowEvent(this,WindowEvent.WINDOW_ICONIFIED));
System.out.println("a");
//this.setVisible(true);
}
if(visit(url)){
history.add(url);
int numentries = history.size();
if (numentries > MAX_HISTORY + 10){
history = history.subList(numentries - MAX_HISTORY,numentries);
numentries = MAX_HISTORY;
}
curHistory = numentries - 1;
if (curHistory > 0) bAction.setEnabled(true);
}
}
public void displayPage(String href){
try {
displayPage(new URL(href));
}
catch (MalformedURLException ex) {
System.err.println("Error: " + ex.toString() );
}
}
private String animationMessage;
int animationFrame;
private String[] animationFrames = new String[]{
"-","\\","|","/","-","\\","|","/",",",".","o","0","O","#","*","+"
}; javax.swing.Timer animator = new javax.swing.Timer(125,new ActionListener(){
public void actionPerformed(ActionEvent e){
animate();
}
});
/**
* Display the animate
*/
private void animate(){
String frame = animationFrames[animationFrame++];
labMessage.setText(animationMessage + " " + frame);
animationFrame = animationFrame % animationFrames.length;
}
public void hyperlinkUpdate(HyperlinkEvent e){
HyperlinkEvent.EventType type = e.getEventType();
if (type == HyperlinkEvent.EventType.ACTIVATED){
displayPage(e.getURL());
}
else if ( type == HyperlinkEvent.EventType.ENTERED ){
labMessage.setText(e.getURL().toString());
}
else if (type == HyperlinkEvent.EventType.EXITED){
labMessage.setText(" ");
}
}
/**
* Start the animate.
* @param msg The message which you want to show.
*/
private void startAnimation(String msg){
animationMessage = msg;
animationFrame = 0;
animator.start();
}
/**
* Stop the animate.
*/
private void stopAnimation(){
animator.stop();
labMessage.setText(" ");
}
public void back(){
if (curHistory > 0){
visit((URL)history.get(--curHistory));
}
fAction.setEnabled(curHistory < history.size() - 1);
bAction.setEnabled(curHistory > 0);
}
public void forward(){
if(curHistory < history.size() - 1){
visit((URL)history.get(++curHistory));
}
fAction.setEnabled(curHistory < history.size() - 1);
bAction.setEnabled(curHistory > 0);
}
class BackAction extends AbstractAction{
public BackAction(String name,Icon icon){
this.putValue(Action.NAME,name);
this.putValue(Action.SMALL_ICON,icon);
}
public void actionPerformed(ActionEvent evt){
Browser.this.back();
}
} class ForwardAction extends AbstractAction{
public ForwardAction(String name,Icon icon){
this.putValue(Action.NAME,name);
this.putValue(Action.SMALL_ICON,icon);
}
public void actionPerformed(ActionEvent evt){
Browser.this.forward();
}
}
class CloseAction extends AbstractAction{
public CloseAction(String name,Icon icon){
this.putValue(Action.NAME,name);
this.putValue(Action.SMALL_ICON,icon);
}
public void actionPerformed(ActionEvent evt){
// Browser.this.setVisible(false);
Browser.this.processWindowEvent(new WindowEvent(Browser.this,WindowEvent.WINDOW_CLOSING));
}
}
class OpenAction extends AbstractAction{
public OpenAction(String name,Icon icon){
this.putValue(Action.NAME,name);
this.putValue(Action.SMALL_ICON,icon);
}
public void actionPerformed(ActionEvent evt){
if (Browser.this.fchoFile == null){
Browser.this.fchoFile = new JFileChooser();
javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter(){
public boolean accept(File f){
String fn = f.getName();
if(f.isDirectory() || fn.endsWith(".html") || fn.endsWith(".htm"))
return true;
else
return false;
}
public String getDescription(){
return "Find html files";
}
};
fchoFile.setFileFilter(filter);
fchoFile.addChoosableFileFilter(filter);
}
int result = fchoFile.showOpenDialog(Browser.this);
if (result == JFileChooser.APPROVE_OPTION){
File selectFile = fchoFile.getSelectedFile();
String url = "file:/" + selectFile.getAbsolutePath();
displayPage(url);
}
}
} public static void main(String[] args){
Browser browser = new Browser();
browser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
browser.setSize(800,600);
browser.setVisible(true);
browser.displayPage("file:/E:/help/java/index.html");
}
}