天天看點

java 編寫浏覽器_JAVA編寫一個簡單的浏覽器!

我要實作的功能是:

1,在菜單欄中有:檔案(建立、打開、另存為、退出);編輯(複制、剪切、粘貼);設定(文字顔色、背景顔色);幫助(幫助)。

2.工具欄

3.視窗

現在我我的檔案中的建立、打開、另存為;編輯;和設定沒有完成 望高手幫忙~!還有我的浏覽器現在不能浏覽本地HTML檔案~1 我的郵箱是:[email protected] 大家可以把源代碼發到我的郵箱中,分一樣給!

源代碼:

第一個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());}

}

}

第三個: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);

}

}

第五個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();

}

}

在編譯第二個和第四個檔案時提示出錯。。

.\MenuBar.java:30: cannot find symbol

symbol : variable jmiNEW

location: class MenuBar

jmFile.add(jmiNEW);

^

1 error

學習中。。

牛人啊,為啥和我寫一樣的東西啊?

儲存的給你寫完了,告訴我怎麼顯示本地的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);

}

}

}

啊哈哈,我簡直就是一個白癡,現成的東西不會用

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);

}

}

}