用Java做一个WWW浏览器现在用JB做出了WWW浏览器的界面,
具体的功能模块还进行当中,
比如如何在地址栏中输入网址就可以浏览网页等等....
哈哈,这是我的毕业设计,不知这里有谁用Java做过WWW浏览器的呢?
能给点指点的吗?
谢谢.
具体的功能模块还进行当中,
比如如何在地址栏中输入网址就可以浏览网页等等....
哈哈,这是我的毕业设计,不知这里有谁用Java做过WWW浏览器的呢?
能给点指点的吗?
谢谢.
//
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class HTMLExample {
public static void main(String[] args) {
JEditorPane pane = null;
try {
pane = new JEditorPane(args[0]);
}
catch (IOException ex) {
ex.printStackTrace(System.err);
System.exit(1);
}
pane.setEditable(false);
// Add a hyperlink listener
final JEditorPane finalPane = pane;
pane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent ev) {
try {
if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
finalPane.setPage(ev.getURL());
} catch (IOException ex) { ex.printStackTrace(System.err); }
}
});
JFrame frame = new JFrame();
frame.addWindowListener(new BasicWindowMonitor());
frame.setContentPane(new JScrollPane(pane));
frame.setSize(350,400);
frame.setVisible(true);
}
}
其实做一个浏览器当毕业设计简直是太简单了,我这个例子很简单,但是如果你想做的复杂一点也不难啊,稍微看看书就行。
解压缩
有源代码可以看的
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Stack;/**
*
* "DummyBrowser" is a very simple minimalist web browser, written in Java.
* It uses the JEditPane swing component to render HTML pages,
* and to follow links to other pages. Note that JEditPane can only
* render <b>some</b> HTML, so that pages with HTML content it can't
* handle will either not be correctly rendered, or will cause errors.
*
* <p>
* This program is heavily based on the Java demonstration Browser class,
* written by <a href="http://www.apl.jhu.edu/~hall/java/">Marty Hall
* - www.apl.jhu.edu/~hall/java</a> in 1998.
* It has been modified by Lawrie Brown to add a few more features,
* to suit its use to test web server access in isolated, test
* computer networking and cisco lab classes.
*
* @author Lawrie Brown
* @version 31 Mar 2005
* @see <a href="http://www.unsw.adfa.edu.au/~lpb/">Lawrie Brown's ADFA home</a>
* @see <a href="http://www.apl.jhu.edu/~hall/java/">Marty Hall - demo java Browser</a>
*/ /**
* Modify:
* 2005-4-17 by HITXU.
*/public class DummyBrowser extends JFrame implements HyperlinkListener,
ActionListener { private JButton homeButton, backButton, quitButton;
private JTextField urlField;
private JEditorPane htmlPane;
private String startURL = null;
private Stack history; /**
* main program routine which creates a new instance of DummyBrowser,
* displaying either the default home page describing this program,
* or the file named on the command-line.
*/
public static void main(String[] args) {
if (args.length == 0)
new DummyBrowser("http://www.csdn.net");
else
new DummyBrowser(args[0]);
} /**
* construct a new DummyBrowser instance, displaying the local file
* whose name is supplied to the constructor.
*
* @param file the local file to display
*/
public DummyBrowser(String file) {
super("Dummy Browser");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); history = new Stack();
// setup initial file URL and history stack
if(file != null)
{
startURL = this.getClass().getResource(file).toExternalForm();
history.push(startURL);
}
// build the GUI components for DummyBrowser
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.lightGray);
homeButton = new JButton("Home");
homeButton.addActionListener(this);
backButton = new JButton("Back");
backButton.addActionListener(this);
quitButton = new JButton("Quit");
quitButton.addActionListener(this);
JLabel urlLabel = new JLabel("URL:");
urlField = new JTextField(30);
urlField.setText(startURL);
urlField.addActionListener(this);
topPanel.add(homeButton);
topPanel.add(urlLabel);
topPanel.add(urlField);
topPanel.add(backButton);
topPanel.add(quitButton);
getContentPane().add(topPanel, BorderLayout.NORTH); htmlPane = new JEditorPane();
htmlPane.setEditable(false);
htmlPane.addHyperlinkListener(this);
try {
if(startURL != null)
htmlPane.setPage(startURL);
} catch(IOException ioe) {
warnUser("Error rendering initial HTML page " + startURL + ": " + ioe);
}
JScrollPane scrollPane = new JScrollPane(htmlPane);
getContentPane().add(scrollPane, BorderLayout.CENTER); Dimension screenSize = getToolkit().getScreenSize();
int width = screenSize.width * 8 / 10;
int height = screenSize.height * 8 / 10;
setBounds(width/8, height/8, width, height);
setVisible(true);
} public void actionPerformed(ActionEvent event) {
String url = startURL;
Object source = event.getSource();
if (source == urlField) {
url = urlField.getText();
} else if (source == backButton) {
if (!history.empty()) url = (String)history.pop(); // pop current page
if (!history.empty()) url = (String)history.pop(); // pop previous page
} else if (source == quitButton) {
System.exit(0);
}
// assume Home button pressed if none of above try {
htmlPane.setPage(new URL(url));
urlField.setText(url);
history.push(url);
} catch(IOException ioe) {
warnUser("Can't follow link to " + url + ": " + ioe);
}
} public void hyperlinkUpdate(HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
htmlPane.setPage(event.getURL());
urlField.setText(event.getURL().toExternalForm());
history.push(event.getURL().toExternalForm());
} catch(IOException ioe) {
warnUser("Can't follow link to "
+ event.getURL().toExternalForm() + ": " + ioe);
}
}
} private void warnUser(String message) {
JOptionPane.showMessageDialog(this, message, "Error",
JOptionPane.ERROR_MESSAGE);
} public void windowClosing(WindowEvent event) {
System.exit(0);
}}
C:\>java DummyBrowser www.163.com
Exception in thread "main" java.lang.NullPointerException
at DummyBrowser.<init>(DummyBrowser.java:73)
at DummyBrowser.main(DummyBrowser.java:56)
C:\>java DummyBrowser
Exception in thread "main" java.lang.NullPointerException
at DummyBrowser.<init>(DummyBrowser.java:73)
at DummyBrowser.main(DummyBrowser.java:54)
比swing的好多了,还支持javascript
http://www.icesoft.com/还有,这个是调用mozilla的
http://jrex.mozdev.org/swt的browser也不错,win下调用ie,linux下调用mozilla,mac下调用safari
不过,只要Java的,好像那些WWW浏览器都是用VC做的吧?
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Stack;/**
*
* "DummyBrowser" is a very simple minimalist web browser, written in Java.
* It uses the JEditPane swing component to render HTML pages,
* and to follow links to other pages. Note that JEditPane can only
* render <b>some</b> HTML, so that pages with HTML content it can't
* handle will either not be correctly rendered, or will cause errors.
*
* <p>
* This program is heavily based on the Java demonstration Browser class,
* written by <a href="http://www.apl.jhu.edu/~hall/java/">Marty Hall
* - www.apl.jhu.edu/~hall/java</a> in 1998.
* It has been modified by Lawrie Brown to add a few more features,
* to suit its use to test web server access in isolated, test
* computer networking and cisco lab classes.
*
* @author Lawrie Brown
* @version 31 Mar 2005
* @see <a href="http://www.unsw.adfa.edu.au/~lpb/">Lawrie Brown's ADFA home</a>
* @see <a href="http://www.apl.jhu.edu/~hall/java/">Marty Hall - demo java Browser</a>
*/ /**
* Modified:
* 2005-4-18 by HITXU.
*/public class DummyBrowser extends JFrame implements HyperlinkListener,
ActionListener { private JButton homeButton, backButton, quitButton;
private JTextField urlField;
private JEditorPane htmlPane;
private String startURL = null;
private Stack history; /**
* main program routine which creates a new instance of DummyBrowser,
* displaying either the default home page describing this program,
* or the file named on the command-line.
*/
public static void main(String[] args) {
if (args.length == 0)
new DummyBrowser(null);
else
new DummyBrowser(args[0]);
} /**
* construct a new DummyBrowser instance, displaying the local file
* whose name is supplied to the constructor.
*
* @param file the local file to display
*/
public DummyBrowser(String file) {
super("Dummy Browser");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); history = new Stack();
// setup initial file URL and history stack
if(file != null)
{
startURL = this.getClass().getResource(file).toExternalForm();
history.push(startURL);
}
// build the GUI components for DummyBrowser
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.lightGray);
homeButton = new JButton("Home");
homeButton.addActionListener(this);
backButton = new JButton("Back");
backButton.addActionListener(this);
quitButton = new JButton("Quit");
quitButton.addActionListener(this);
JLabel urlLabel = new JLabel("URL:");
urlField = new JTextField(30);
urlField.setText(startURL);
urlField.addActionListener(this);
topPanel.add(homeButton);
topPanel.add(urlLabel);
topPanel.add(urlField);
topPanel.add(backButton);
topPanel.add(quitButton);
getContentPane().add(topPanel, BorderLayout.NORTH); htmlPane = new JEditorPane();
htmlPane.setEditable(false);
htmlPane.addHyperlinkListener(this);
try {
if(startURL != null)
htmlPane.setPage(startURL);
} catch(IOException ioe) {
warnUser("Error rendering initial HTML page " + startURL + ": " + ioe);
}
JScrollPane scrollPane = new JScrollPane(htmlPane);
getContentPane().add(scrollPane, BorderLayout.CENTER); Dimension screenSize = getToolkit().getScreenSize();
int width = screenSize.width * 8 / 10;
int height = screenSize.height * 8 / 10;
setBounds(width/8, height/8, width, height);
setVisible(true);
} public void actionPerformed(ActionEvent event) {
String url = startURL;
Object source = event.getSource();
if (source == urlField) {
url = urlField.getText();
} else if (source == backButton) {
if (!history.empty()) url = (String)history.pop(); // pop current page
if (!history.empty()) url = (String)history.pop(); // pop previous page
} else if (source == quitButton) {
System.exit(0);
}
// assume Home button pressed if none of above try {
htmlPane.setPage(new URL(url));
urlField.setText(url);
history.push(url);
} catch(IOException ioe) {
warnUser("Can't follow link to " + url + ": " + ioe);
}
} public void hyperlinkUpdate(HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
htmlPane.setPage(event.getURL());
urlField.setText(event.getURL().toExternalForm());
history.push(event.getURL().toExternalForm());
} catch(IOException ioe) {
warnUser("Can't follow link to "
+ event.getURL().toExternalForm() + ": " + ioe);
}
}
} private void warnUser(String message) {
JOptionPane.showMessageDialog(this, message, "Error",
JOptionPane.ERROR_MESSAGE);
} public void windowClosing(WindowEvent event) {
System.exit(0);
}}
浏览器源码.Java的
http://www.icesoft.com/还有,这个是调用mozilla的
http://jrex.mozdev.org/swt的browser也不错,win下调用ie,linux下调用mozilla,mac下调用safari难道这里有牛人准备自己写一个html显示控件,不但要渲染页面,还要控制JAVASCRIPT?半年哪够?要是只要半年,mozilla还要那么多人,写那些年?
swing的EditPane很差的,很多东西都不支持。
http://www.ojava.net/read.php?tid=24&fpage=1
浏览器源码.Java的这个网页打不开啊???
这与JDK有点不同的!
这与JDK有点不同的!
??????
Mark 好贴,好资源,HOHOPS:如果什么都从头开始搞,从低层做起,那中国的计算机算是完了.
=================================================================哈哈,恰恰相反,如果什么都用别人的,那中国的计算机才真正完了。你说说,现在中国的计算机业还剩下什么了?
import java.awt.event.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;// The Mini Web Browser.
public class MiniBrowser extends JFrame
implements HyperlinkListener
{
// These are the buttons for iterating through the page list.
private JButton backButton, forwardButton; // Page location text field.
private JTextField locationTextField; // Editor pane for displaying pages.
private JEditorPane displayEditorPane; // Browser's list of pages that have been visited.
private ArrayList pageList = new ArrayList(); // Constructor for Mini Web Browser.
public MiniBrowser()
{
// Set application title.
super("Mini Browser"); // Set window size.
setSize(640, 480); // Handle closing events.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
actionExit();
}
}); // Set up file menu.
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem fileExitMenuItem = new JMenuItem("Exit",
KeyEvent.VK_X);
fileExitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionExit();
}
});
fileMenu.add(fileExitMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar); // Set up button panel.
JPanel buttonPanel = new JPanel();
backButton = new JButton("< Back");
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionBack();
}
});
backButton.setEnabled(false);
buttonPanel.add(backButton);
forwardButton = new JButton("Forward >");
forwardButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionForward();
}
});
forwardButton.setEnabled(false);
buttonPanel.add(forwardButton);
locationTextField = new JTextField(35);
locationTextField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
actionGo();
}
}
});
buttonPanel.add(locationTextField);
JButton goButton = new JButton("GO");
goButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionGo();
}
});
buttonPanel.add(goButton); // Set up page display.
displayEditorPane = new JEditorPane();
displayEditorPane.setContentType("text/html");
displayEditorPane.setEditable(false);
displayEditorPane.addHyperlinkListener(this); getContentPane().setLayout(new BorderLayout());
getContentPane().add(buttonPanel, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(displayEditorPane),
BorderLayout.CENTER);
} // Exit this program.
private void actionExit() {
System.exit(0);
} // Go back to the page viewed before the current page.
private void actionBack() {
URL currentUrl = displayEditorPane.getPage();
int pageIndex = pageList.indexOf(currentUrl.toString());
try {
showPage(
new URL((String) pageList.get(pageIndex - 1)), false);
}
catch (Exception e) {}
} // Go forward to the page viewed after the current page.
private void actionForward() {
URL currentUrl = displayEditorPane.getPage();
int pageIndex = pageList.indexOf(currentUrl.toString());
try {
showPage(
new URL((String) pageList.get(pageIndex + 1)), false);
}
catch (Exception e) {}
} // Load and show the page specified in the location text field.
private void actionGo() {
URL verifiedUrl = verifyUrl(locationTextField.getText());
if (verifiedUrl != null) {
showPage(verifiedUrl, true);
} else {
showError("Invalid URL");
}
} // Show dialog box with error message.
private void showError(String errorMessage) {
JOptionPane.showMessageDialog(this, errorMessage,
"Error", JOptionPane.ERROR_MESSAGE);
} // Verify URL format.
private URL verifyUrl(String url) {
// Only allow HTTP URLs.
if (!url.toLowerCase().startsWith("http://"))
return null; // Verify format of URL.
URL verifiedUrl = null;
try {
verifiedUrl = new URL(url);
} catch (Exception e) {
return null;
} return verifiedUrl;
} /* Show the specified page and add it to
the page list if specified. */
private void showPage(URL pageUrl, boolean addToList)
{
// Show hour glass cursor while crawling is under way.
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); try {
// Get URL of page currently being displayed.
URL currentUrl = displayEditorPane.getPage(); // Load and display specified page.
displayEditorPane.setPage(pageUrl); // Get URL of new page being displayed.
URL newUrl = displayEditorPane.getPage(); // Add page to list if specified.
if (addToList) {
int listSize = pageList.size();
if (listSize > 0) {
int pageIndex =
pageList.indexOf(currentUrl.toString());
if (pageIndex < listSize - 1) {
for (int i = listSize - 1; i > pageIndex; i--) {
pageList.remove(i);
}
}
}
pageList.add(newUrl.toString());
} // Update location text field with URL of current page.
locationTextField.setText(newUrl.toString()); // Update buttons based on the page being displayed.
updateButtons();
}
catch (Exception e)
{
// Show error messsage.
showError("Unable to load page");
}
finally
{
// Return to default cursor.
setCursor(Cursor.getDefaultCursor());
}
} /* Update back and forward buttons based on
the page being displayed. */
private void updateButtons() {
if (pageList.size() < 2) {
backButton.setEnabled(false);
forwardButton.setEnabled(false);
} else {
URL currentUrl = displayEditorPane.getPage();
int pageIndex = pageList.indexOf(currentUrl.toString());
backButton.setEnabled(pageIndex > 0);
forwardButton.setEnabled(
pageIndex < (pageList.size() - 1));
}
} // Handle hyperlink's being clicked.
public void hyperlinkUpdate(HyperlinkEvent event) {
HyperlinkEvent.EventType eventType = event.getEventType();
if (eventType == HyperlinkEvent.EventType.ACTIVATED) {
if (event instanceof HTMLFrameHyperlinkEvent) {
HTMLFrameHyperlinkEvent linkEvent =
(HTMLFrameHyperlinkEvent) event;
HTMLDocument document =
(HTMLDocument) displayEditorPane.getDocument();
document.processHTMLFrameHyperlinkEvent(linkEvent);
} else {
showPage(event.getURL(), true);
}
}
} // Run the Mini Browser.
public static void main(String[] args) {
MiniBrowser browser = new MiniBrowser();
browser.show();
}
}
---------------------------我的分割线 盗版必究-----------------------------swing的JEditPane功能很弱的,不会支持FLASH,但图片是支持的.要不就用第三方的控件,要不就自己写?还是都算了吧,就这样了.