Jump to content

understanding the code

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
Hi
please help me ... I found this code in the internet and I think it will help me but I can't understand it very well...can anyone help me please??:rules:

This code is to make simple web browser for u ...:w00t:


import javax.swing.text.html.*;

import javax.swing.text.*;

import javax.swing.event.*;

import javax.swing.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;


/**

   A simple Web Browser with minimal functionality.

   @author Jose M. Vidal

*/

public class Browser {


	/** Set the page.

		@param jep the pane on which to display the url

		@param url the url to display */

 	protected static void setPage(JEditorPane jep, String url){

		try {

			jep.setPage(url);

		}

		catch (IOException e) {

			System.err.println(e);

			System.exit(-1);

		}


	}


	/** An inner class which listens for keypresses on the Back button. */

	class backButtonListener implements ActionListener {

		protected JEditorPane jep;

		protected JLabel label;

		protected JButton backButton;

		protected Vector history;

		public backButtonListener(JEditorPane jep, JButton backButton, Vector history, JLabel label){

			this.jep = jep;

			this.backButton = backButton;

			this.history = history;

			this.label = label;

		}


		/** The action is to show the last url in the history.

		 @param e the event*/

		public void actionPerformed(ActionEvent e){

			try{

				//the current page is the last, remove it

				String curl = (String)history.lastElement();

				history.removeElement(curl);

					

				curl = (String)history.lastElement();

				System.out.println("Back to " + curl);

				setPage(jep,curl);

				label.setText("<html><b>URL:</b> "+ curl);

				if (history.size() == 1)

					backButton.setEnabled(false);

			}

			catch (Exception ex){

				System.out.println("Exception " + ex);

			}

		}

	}


	/** An inner class that listens for hyperlinkEvent.*/

	class LinkFollower implements HyperlinkListener {

		protected JEditorPane jep;

		protected JLabel label;

		protected JButton backButton;

		protected Vector history;

		public LinkFollower(JEditorPane jep, JButton backButton, Vector history, JLabel label){

			this.jep = jep;

			this.backButton = backButton; 

			this.history = history;

			this.label = label;

		}

		/** The action is to show the page of the URL the user clicked on.

			@param evt the event. We only care when its type is ACTIVATED. */

		public void hyperlinkUpdate(HyperlinkEvent evt){

			if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED){

				try {

					String currentURL = evt.getURL().toString();

					history.add(currentURL);

					backButton.setEnabled(true);

					System.out.println("Going to " + currentURL);

					setPage(jep,currentURL);

					label.setText("<html><b>URL:</b> "+ currentURL);

				}

				catch (Exception e) {

					System.out.println("ERROR: Trouble fetching url");

				}

			}

		}


	}


	/** The contructor runs the browser. It displays the main frame with the

		fetched initialPage

		@param initialPage the first page to show */

 	public Browser(String initialPage){


		/** A vector of String containing the past urls */

		Vector history = new Vector();

		history.add(initialPage);

		

		// set up the editor pane

		JEditorPane jep = new JEditorPane();

		jep.setEditable(false);

		setPage(jep, initialPage);


		// set up the window

		JScrollPane scrollPane = new JScrollPane(jep);     

		JFrame f = new JFrame("Simple Web Browser");

		f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);


		//Exit the program when user closes window.

		f.addWindowListener(new WindowAdapter() {

				public void windowClosing(WindowEvent e){

					System.exit(0);

				}

			});


		//Label where we show the url

		JLabel label = new JLabel("<html><b>URL:</b> "+ initialPage);


		

		JButton backButton = new JButton ("Back");

		backButton.setActionCommand("back");

		backButton.setToolTipText("Go to previous page");

		backButton.setEnabled(false);

		backButton.addActionListener(new backButtonListener(jep, backButton, history, label));


		JButton exitButton = new JButton ("Exit");

		exitButton.setActionCommand("exit");

		exitButton.setToolTipText("Quit this application");

		exitButton.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {

					System.exit(0);

				}

			});


		//A toolbar to hold all our buttons

		JToolBar toolBar = new JToolBar();

		toolBar.add(backButton);

		toolBar.add(exitButton);



		jep.addHyperlinkListener(new LinkFollower(jep, backButton, history, label));


		//Set up the toolbar and scrollbar in the contentpane of the frame

		JPanel contentPane = (JPanel)f.getContentPane();

		contentPane.setLayout(new BorderLayout());

		contentPane.setPreferredSize(new Dimension(400, 100));

		contentPane.add(toolBar, BorderLayout.NORTH);

		contentPane.add(scrollPane, BorderLayout.CENTER);

		contentPane.add(label, BorderLayout.SOUTH);


		f.pack();

		f.setSize(640, 360);

		f.setVisible(true);



	}


	/** Create a Browser object. Use the command-line url if given */

	public static void main(String[] args) {

		String initialPage = new String("http://www.cse.sc.edu");


		if (args.length > 0) initialPage = args[0];


		Browser b = new Browser(initialPage);

	}

	

}

		


#2
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
pleeeeeeaaaaaase any help

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Is there any particular part of the code that is confusing you?
Wow I changed my sig!

#4
prof.deedee

prof.deedee

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
yes what setPage method do??