站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java & J2SE 技术区→如何编写一个浏览器(用java技术)?

如何编写一个浏览器(用java技术)?

发表新主题   回复此主题

第1楼 2006-12-11 21:09 apollo_dsa 写道:

如何编写一个浏览器(用java技术)?



第2楼 2013-08-31 12:44 Robot :

如何编写一个浏览器(用java技术)? 相关


第3楼 2006-12-11 23:29 Lieo 写道:

使用java自己编写浏览器

package html;


import java.io.IOException; //导入包
import java.net.URL;
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class HtmlBrower extends JFrame{ //定义类
JPanel contentPane;包含整个框架的容器
BorderLayout borderLayoutAll=new BorderLayout();
JLabel jLabelPrompt=new JLabel(); //状态提示框
JPanel jPanelMain=new JPanel(); //包含地址栏和URL内容显示框的容器
BorderLayout borderLayoutMain=new BorderLayout();
JTextField textFieldURL=new JTextField();//定义地址栏
JEditorPane jEditorPane=new JEditorPane(); //URL内容显示窗口

public HtmlBrower(){ //构造函数
try{
jbInit();
}
catch(Exception e){
e.printStackTrace();
}
}

private void jbInit()throws Exception{ //界面初始化
contentPane=(JPanel)getContentPane();
contentPane.setLayout(borderLayoutAll);
jPanelMain.setLayout(borderLayoutMain);
jLabelPrompt.setText("简单的HTML浏览器,请在text框内输入完整的URL");
textFieldURL.setText(""); //开始运行时,清空地址栏
textFieldURL.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){ //输入URL,回车后执行
textFieldURL_actionPerformed(e);
}
});
jEditorPane.setEditable(false); //网页显示部分不可修改
jEditorPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent e){
jEditorPane_hyperlinkUpdate(e); //点击超级连接后执行
}
});
JScrollPane scrollPane=new JScrollPane();
scrollPane.getViewport().add(jEditorPane);
jPanelMain.add(textFieldURL,"North");
jPanelMain.add(scrollPane,"Center");
contentPane.add(jLabelPrompt,"North");
contentPane.add(jPanelMain,"Center");
enableEvents(AWTEvent.WINDOW_EVENT_MASK); //启动事件监视
this.setSize(new Dimension(600,500)); //窗口大小
this.setTitle("网页浏览器"); //设置标题
this.setVisible(true); //显示窗口
}
void textFieldURL_actionPerformed(ActionEvent e){
try{
jEditorPane.setPage(textFieldURL.getText()); //显示URL内容
}
catch(IOException ex){
JOptionPane msg=new JOptionPane();
JOptionPane.showMessageDialog(this,"不正确的URL地址"+textFieldURL.getText(),"不正确的输入",0);
}
}
void jEditorPane_hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType()==javax.swing.event.HyperlinkEvent.EventType.ACTIVATED){
try{
URL url=e.getURL(); //获得最新URL
jEditorPane.setPage(url); //显示超级连接内容
textFieldURL.setText(url.toString()); //修改地址栏
}
catch(IOException io){
JOptionPane msg=new JOptionPane();
JOptionPane.showMessageDialog(this,"不能打开连接","不正确的输入",0);
}
}
}
protected void processWindowEvent(WindowEvent e){
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING){ //关闭窗口
System.exit(0); //退出
}
}
public static void main(String[] args){ //主函数
new HtmlBrower();
}
}


发表新主题   回复此主题