用JAVA编写一个简单的浏览器!
我要实现的功能是:
1,在菜单栏中有:文件(新建、打开、另存为、退出);编辑(复制、剪切、粘贴);设置(文字颜色、背景颜色);帮助(帮助)。
2.工具栏
3.窗口
现在我我的文件中的新建、打开、另存为;编辑;和设置没有完成 望高手帮忙~!还有我的浏览器现在不能浏览本地HTML文件~1 我的邮箱是:qianwugang1982@163.com 大家可以把源代码发到我的邮箱中,分一样给!
源代码:
第一个Show.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Show
{
public static void main(String[] args)
{
BrowserFrame bf = new BrowserFrame();
bf.setVisible(true);
}
}
第二个BrowserFrame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class BrowserFrame extends JFrame{
private WebWindow ww = new WebWindow(this);
private MenuBar mb = new MenuBar(this);
private ToolBar tb = new ToolBar(this);
private HistoryList hl = new HistoryList();
private String sURL;
public BrowserFrame(){
setTitle("Show");
setSize(640, 480);
setLocation(20, 20);
getContentPane().setLayout(new BorderLayout());
setJMenuBar(mb);
JPanel jpToolMenu = new JPanel();
jpToolMenu.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
jpToolMenu.add(tb);
getContentPane().add(jpToolMenu, BorderLayout.NORTH);
getContentPane().add(ww, BorderLayout.CENTER);
setCurrentURL("http://www.163.com");}
public void setToolBarURL(String s) {
tb.setTextField(s);}
public void goBack() {
try {
sURL = hl.getLast();
ww.setCurrentURL(sURL);
setToolBarURL(sURL);
}catch(Exception e) {new PopupDialog("错误!", e.getMessage());}
}
public void goForward() {
try {
sURL = hl.getNext();
ww.setCurrentURL(sURL);
setToolBarURL(sURL);
}catch(Exception e) {new PopupDialog("错误", e.getMessage());}
}
public void refreshURL() {
try {
ww.setCurrentURL(sURL);
setToolBarURL(sURL);
}catch(Exception e) {new PopupDialog("错误!", e.getMessage());}
}
public String getCurrentURL() {
return sURL;}
public void setCurrentURL(String current) {
if (!(current.substring(0, 7)).equals("http://")) {
if(!(current.substring(0, 3)).equals("www"));
current = "www."+current;current = "http://"+current;}
sURL = current;
try {
ww.setCurrentURL(sURL);
hl.add(sURL);
setToolBarURL(sURL);
}catch(Exception e) {new PopupDialog("错误!", e.getMessage());}
}
}
问题点数:0、回复次数:38Top
1 楼hackerking(黑客帝国神秘客)回复于 2005-05-22 20:00:49 得分 0
第三个:TOOLBar.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ToolBar extends JToolBar implements ActionListener{
private JButton jbBack = new JButton(new ImageIcon("./Back16.gif"));
private JButton jbForward = new JButton(new ImageIcon("./Forward16.gif"));
private JButton jbRefresh = new JButton(new ImageIcon("./Refresh16.gif"));
private JButton jbGo = new JButton("Go");
private JButton jbShogun = new JButton(new ImageIcon("./s.gif"));
private JTextField jtfLocation = new JTextField(20);
private BrowserFrame bfRef;
public ToolBar() {
this(new BrowserFrame());
}
public ToolBar(BrowserFrame bf) {
bfRef = bf;
setName("Toolbar");
setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
jbBack.setToolTipText("Back");
jbForward.setToolTipText("Forward");
jbRefresh.setToolTipText("Refresh");
jbGo.setToolTipText("Go");
jbShogun.setToolTipText("Homepage");
this.add(jbBack);
this.add(jbForward);
this.add(jbRefresh);
this.add(new JLabel("Location:"));
this.add(jtfLocation);
this.add(jbGo);
this.add(new JLabel(" "));
this.add(jbShogun);
jbBack.addActionListener(this);
jbForward.addActionListener(this);
jbRefresh.addActionListener(this);
jtfLocation.addActionListener(this);
jbGo.addActionListener(this);
jbShogun.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if ((e.getSource() == jbGo) || (e.getSource() == jtfLocation))
bfRef.setCurrentURL(jtfLocation.getText());
else if(e.getSource() == jbRefresh)
bfRef.refreshURL();
else if(e.getSource() == jbBack)
bfRef.goBack();
else if (e.getSource() == jbForward)
bfRef.goForward();
else if (e.getSource() == jbShogun)
bfRef.setCurrentURL("http://www.163.com");
}
public void setTextField(String s) {
jtfLocation.setText(s);
}
}
第四个:MenuBar.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuBar extends JMenuBar implements ActionListener, ItemListener{
private JMenu jmFile = new JMenu("文件");
private JMenu jmEdit = new JMenu("编辑");
private JMenu jmSet = new JMenu("设置");
private JMenu jmBookmarks = new JMenu("标签");
private JMenu jmHelp = new JMenu("帮助");
private JMenuItem jmiNew =new JMenuItem("新建");
private JMenuItem jmiOpen =new JMenuItem("打开");
private JMenuItem jmiSaveas =new JMenuItem("另存为");
private JMenuItem jmiExit = new JMenuItem("退出");
private JMenuItem jmiCopy = new JMenuItem("复制");
private JMenuItem jmiCut = new JMenuItem("剪切");
private JMenuItem jmiPaste = new JMenuItem("粘贴");
private JMenuItem jmiFcolor = new JMenuItem("文字颜色");
private JMenuItem jmiBcolor = new JMenuItem("背景颜色");
private JMenuItem jmiAddBookmark = new JMenuItem("加入标签");
private JCheckBoxMenuItem jmiRemoveBookmark = new JCheckBoxMenuItem("移除标签");
private JMenuItem jmiAbout = new JMenuItem("帮助");
private boolean bRemove = false;
private BrowserFrame bfRef;
private BookmarkList bl = new BookmarkList();
public MenuBar() {
this(new BrowserFrame());
}
public MenuBar(BrowserFrame bf) {
bfRef = bf;
jmFile.add(jmiNEW);
jmFile.add(jmiOpen);
jmFile.add(jmiSaveas);
jmFile.add(jmiExit);
jmEdit.add(jmiCopy);
jmEdit.add(jmiCut);
jmEdit.add(jmiPaste);
jmSet.add(jmiFcolor);
jmSet.add(jmiBcolor);
jmBookmarks.add(jmiAddBookmark);
jmBookmarks.add(jmiRemoveBookmark);
jmHelp.add(jmiAbout);
jmBookmarks.addSeparator();
add(jmFile);
add(jmEdit);
add(jmSet);
add(jmBookmarks);
add(jmHelp);
jmiNew.addActionListener(this);
jmiOpen.addActionListener(this);
jmiSaveas.addActionListener(this);
jmiExit.addActionListener(this);
jmiCopy.addActionListener(this);
jmiCut.addActionListener(this);
jmiPaste.addActionListener(this);
jmiFcolor.addActionListener(this);
jmiBcolor.addActionListener(this);
jmiAbout.addActionListener(this);
jmiAddBookmark.addActionListener(this);
jmiRemoveBookmark.addItemListener(this);
addBookmarks();
}
public void addBookmarks() {
for(int x = 0; x < bl.getSize(); x++) {
JMenuItem jmiBM = new JMenuItem(bl.returnURL(x));
jmBookmarks.add(jmiBM);
jmiBM.addActionListener(this);}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jmiExit)
System.exit(0);
else if (e.getSource() == jmiAbout)
new PopupDialog("Help?", "Help? 自己动手,丰衣足食!!^_^");
else if (e.getSource() == jmiAddBookmark) {
bl.add(bfRef.getCurrentURL());
addBookmarkItem(bfRef.getCurrentURL());
}
else {
if (!bRemove)
bfRef.setCurrentURL(e.getActionCommand());
else {
jmBookmarks.remove((JMenuItem) e.getSource());
try {
bl.remove(e.getActionCommand());
}catch(Exception ex) {new PopupDialog("错误", "移除标签出错!");}
}
}
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == jmiRemoveBookmark)
bRemove = !bRemove;
}
public void addBookmarkItem(String s) {
JMenuItem newItem = new JMenuItem(s);
newItem.addActionListener(this);
jmBookmarks.add(newItem);
}
}
Top
2 楼hackerking(黑客帝国神秘客)回复于 2005-05-22 20:01:28 得分 0
第五个WebNode.java
public class WebNode {
private String site;
private WebNode next;
private WebNode prev;
public WebNode() {
site = null;
next = null;
prev = null;
}
public WebNode(String addr) {
site = addr;
next = null;
prev = null;
}
public void setURL(String url) {
site = url;}
public String getURL() {
return site;
}
public void setNext(WebNode node) {
next = node;
}
public WebNode getNext() {
return next;
}
public void setPrev(WebNode node) {
prev = node;
}
public WebNode getPrev() {
return prev;
}
}
第六个PopuPDialog.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PopupDialog extends JDialog implements ActionListener{
public PopupDialog(String title, String text) {
super( new Frame(""), title, true );
setSize(300,100);
setResizable( false );
setLocation (240, 240);
JLabel message = new JLabel(text, JLabel.CENTER);
getContentPane().add( message, BorderLayout.CENTER );
JButton close = new JButton( " 关闭 " );
JPanel p = new JPanel();
close.addActionListener( this );
p.add(close);
getContentPane().add( p, BorderLayout.SOUTH );
setVisible(true);}
public void actionPerformed( ActionEvent e ){
setVisible( false );}
}
第七个List.java
public abstract class List {
protected WebNode head;
protected int size = 0;
public List() {
head = null;}
public void remove(String s) throws Exception {}
public void add(String url) {
WebNode placeHolder = new WebNode();
placeHolder.setURL(url);
placeHolder.setNext(head);
head=placeHolder;
size++;}
public String getName(int i) throws Exception{
WebNode wn = head;
if (isEmpty())
throw new Exception("记录为空");
for (int x = 0; x < i; x++) {
wn = wn.getNext();
}
return wn.getURL();
}
public int getSize() {return size;}
public boolean isEmpty() { return (head == null); }
}
第八个WebWindow.java
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
import java.awt.*;
import java.io.*;
public class WebWindow extends JScrollPane implements HyperlinkListener{
private JEditorPane je = new JEditorPane();
private BrowserFrame bfRef;
public WebWindow() {
this(new BrowserFrame());}
public WebWindow(BrowserFrame bf) {
bfRef = bf;
je.setEditable(false);
setViewportView(je);
setAutoscrolls(false);}
public void setCurrentURL(String sURL) throws Exception {
try {
je = new JEditorPane(sURL);
je.setEditable(false);
setViewportView(je);
je.addHyperlinkListener(this);
}
catch(Exception e) {throw new Exception("有些小错误!去不了你想去的地方!^_^");
}
}
public void hyperlinkUpdate(HyperlinkEvent evt) {
if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
bfRef.setCurrentURL(evt.getURL().toString());
}
}
}
第九个 HistoryList.java
public class HistoryList extends List{
private WebNode tail;
private WebNode current;
public HistoryList() {
super();
tail = null;
}
public void add(String url) {
super.add(url);
if (size == 1) {
tail = head;
current = head;
}
else {
head.setPrev(current);
current.setNext(head);
current = head;
head.setNext(null);
}
}
public String getLast() throws Exception{
if(current.getPrev() != null)
current = current.getPrev();
else
throw new Exception("无法后退!");
return current.getURL();
}
public String getNext() throws Exception{
if(current.getNext() != null)
current = current.getNext();
else
throw new Exception("不能前进!");
return current.getURL();
}
}
第十个BookmarkList.java
import java.util.*;
import java.io.*;
public class BookmarkList extends List {
public static final String BFILE = "bookmark.lis";
public BookmarkList() {
super();
readIntoList();
}
public void add(String s) {
super.add(s);
writeToFile();}
public void startupAdd(String s){
super.add(s);
}
public void remove(String s) throws Exception {
WebNode holder = head;
boolean isFound = false;
if(isEmpty())
throw new Exception("记录为空");
else if ((holder.getURL()).equals(s)){
head = holder.getNext();
size--;
writeToFile();
isFound = true;
}
else {
while ((holder != null) && (!isFound)){
if ((holder.getNext().getURL()).equals(s)) {
holder.setNext(holder.getNext().getNext());
isFound = true;
size--;
writeToFile();
}
else{
holder = holder.getNext();
}
}
}
if (!isFound) {
throw new Exception("No match found!");
}
}
private void writeToFile() {
try {
FileOutputStream fOut = new FileOutputStream(BFILE);
for (WebNode x = head; x != null; x = x.getNext()) {
fOut.write(x.getURL().getBytes());
fOut.write('\n');
}
fOut.close();
}catch (Exception e) {
new PopupDialog("文件错误", "不能写入文件");
}
}
private void readIntoList() {
try {
FileInputStream fIn = new FileInputStream(new File(BFILE));
byte bt[] = new byte[(int) (new File(BFILE)).length()];
fIn.read(bt);
String s = new String(bt);
StringTokenizer st = new StringTokenizer(s, "\n");
while(st.hasMoreTokens())
startupAdd(st.nextToken());
fIn.close();}catch(Exception e) { }
}
public String returnURL(int loc) {
WebNode wn = head;
for (int x = 0; x < loc; x++)
wn = wn.getNext();
return wn.getURL();
}
}
Top
3 楼sunshine5246(阳光)回复于 2005-05-22 20:17:36 得分 0
顶Top
4 楼wyplay(coding....)回复于 2005-05-22 20:22:23 得分 0
在编译第二个和第四个文件时提示出错。。
.\MenuBar.java:30: cannot find symbol
symbol : variable jmiNEW
location: class MenuBar
jmFile.add(jmiNEW);
^
1 error
学习中。。Top
5 楼oicqdlq(石頭)回复于 2005-05-22 20:47:00 得分 0
正有自己做个浏览器的想法,可以借鉴一下!Top
6 楼yangbc(土豆块)回复于 2005-05-22 22:51:33 得分 0
不要用html类,自己把解释html部分实现一下才叫作一个浏览器吧Top
7 楼jdbc(.Config)回复于 2005-05-22 23:54:44 得分 0
做的还可以
不过里面有些地方实现的不太好,
而且运行时,也会显示很多HTML代码Top
8 楼pigrain(猪小雨)(我是微软最差的MCP)回复于 2005-05-23 00:16:29 得分 0
看别人代码超级痛苦Top
9 楼BugYou004()回复于 2005-05-23 00:40:17 得分 0
咱是来学习的!!
支持Top
10 楼hackerking(黑客帝国神秘客)回复于 2005-05-24 14:21:58 得分 0
有谁帮忙解决一下啊~!Top
11 楼ll42002(灰舌)回复于 2005-05-24 14:36:04 得分 0
我觉得作一个真正的浏览器:
首先得实现HTTP协议解析,
然后是HTML协议解析,
最后是其他程序的支持。
Top
12 楼realmud(鸡鸡掉进大海)回复于 2005-05-24 20:55:57 得分 0
老大,写code 为啥没有comment 啊,痛苦ing...Top
13 楼robin0000000(一股暖风吹我心!!)回复于 2005-05-24 23:03:03 得分 0
location: class MenuBar
jmFile.add(jmiNEW);
(jmiNEW);是不是大写J啊??
Top
14 楼robin0000000(一股暖风吹我心!!)回复于 2005-05-24 23:08:09 得分 0
错了
看错了我
呵呵!
Top
15 楼charmgjj(杰)回复于 2005-05-24 23:16:29 得分 0
学习一下!!!
Top
16 楼love01px(JAVA CUP)回复于 2005-05-25 00:29:41 得分 0
顶
学习ingTop
17 楼flymyway()回复于 2005-05-25 10:11:10 得分 0
继续,只是不支持srcipt,已经很不错Top
18 楼hackerking(黑客帝国神秘客)回复于 2005-05-25 11:12:19 得分 0
在MenuBar中的错误是:NEW改为NewTop
19 楼hellwindy(夜神·月)回复于 2005-05-25 11:35:57 得分 0
浏览器不是能看网页就行了吗?复制粘贴什么的是干嘛?Top
20 楼ariel_521(Ariel)回复于 2005-05-25 11:43:25 得分 0
牛人啊,为啥和我写一样的东西啊?
保存的给你写完了,告诉我怎么显示本地的htlm
注意:要显示页面,不是代码。
public void saveFile()
{
try
{
file = new File(currentFileName);
writefile = new FileWriter(file);
String s=paneURL.getText();
writefile.write(s);
writefile.close();
}
catch(IOException ee)
{
JOptionPane.showMessageDialog(this,"Warning",ee.toString(),
JOptionPane.ERROR_MESSAGE);
}
}
public void saveAsFile()
{
int n=saveAsFileDialog.showSaveDialog(this);
file = saveAsFileDialog.getSelectedFile();
if(n==JFileChooser.APPROVE_OPTION)
{
try
{
currentFileName = saveAsFileDialog.getSelectedFile().getPath();
writefile=new FileWriter(file);
write=new BufferedWriter(writefile);
String s=paneURL.getText();
write.write(s);
write.newLine();
write.flush();
write.close();
writefile.close();
this.setTitle(currentFileName);
}
catch(IOException ee)
{
JOptionPane.showMessageDialog(this,"Warning",ee.toString(),
JOptionPane.ERROR_MESSAGE);
}
}
}Top
21 楼ariel_521(Ariel)回复于 2005-05-25 11:49:47 得分 0
要快啊,着急Top
22 楼ariel_521(Ariel)回复于 2005-06-01 17:12:50 得分 0
upTop
23 楼hackerking(黑客帝国神秘客)回复于 2005-06-02 08:25:21 得分 0
这个问题我也不知道怎么办啊?Top
24 楼ariel_521(Ariel)回复于 2005-06-02 10:56:28 得分 0
啊哈哈,我简直就是一个白痴,现成的东西不会用
public void openFile()
{
int n=openFileDialog.showOpenDialog(this);
if(n==JFileChooser.APPROVE_OPTION)
{
paneURL.setText(null);
file=openFileDialog.getSelectedFile();
try
{
currentFileName = openFileDialog.getSelectedFile().getPath();
url = file.toURL(); //这个是最关键的,要把File类型的file变成URL型的。
in = url.openStream();
paneURL.setPage(url);
stringText = paneURL.getText().trim();
}
catch(IOException ee)
{
paneURL.setText(currentFileName);
JOptionPane.showMessageDialog(this,"Opening Error !!",ee.toString(),
JOptionPane.WARNING_MESSAGE);
}
}
}Top
25 楼hackerking(黑客帝国神秘客)回复于 2005-06-09 19:58:31 得分 0
高手啊~! 再帮我加别的功能啊?Top
26 楼simon0512(虫虫)回复于 2005-06-09 23:15:10 得分 0
upTop
27 楼wangjinwang(王者之疯)回复于 2005-06-09 23:31:01 得分 0
我要学习Top
28 楼wearetheone(beehead)回复于 2005-06-10 02:00:21 得分 0
楼主的坏习惯,写代码不注释,看到我头晕了Top
29 楼brusewu()回复于 2005-07-16 09:35:30 得分 0
请问高手们为什么他的浏览器在打开一个网站之后会出现一些代码,而且排版也有问题啊???Top
30 楼TAIDOU(泰斗)回复于 2005-07-16 09:40:00 得分 0
顶一下
Top
31 楼smltiger(罗小虎)回复于 2005-07-16 10:30:22 得分 0
晕哦,你这只不过是相当于一个组装电脑的人,真正的CPU还是用的别人生产的东西,嘿嘿
QQ:28286880Top
32 楼brusewu()回复于 2005-07-16 10:41:36 得分 0
排版应该怎样解决阿,还有我发现浏览器好像不支持JS,请问高手们怎么解决阿Top
33 楼winstars(雕刻时光)回复于 2005-07-16 10:43:47 得分 0
潜力贴,偶不会,帮你顶一下,不要沉了Top
34 楼armstronghp(@小旋风@)回复于 2005-07-16 18:05:51 得分 0
偶讨厌看这么长的code , 介绍一下思路才是偶想要的。Top
35 楼hygx(失身志不移)(http://91in.blogchina.com)回复于 2005-07-16 20:28:11 得分 0
gzTop
36 楼sx025726(魅力元素)回复于 2005-07-16 21:41:24 得分 0
代码好长啊
不过能借鉴一下
是个不错的学习材料Top
37 楼hifan(Kee)回复于 2005-07-16 21:49:04 得分 0
做的很好哇!Top
38 楼hifan(Kee)回复于 2005-07-16 21:53:40 得分 0
好长。。。
明天看,今天要睡觉了。。。Top




