الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Simple web Browser

مغلق
بدأه a.abusa في 26 أبريل 2007 · 1 رد · 520 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته ،،

الأخوة الأفاضل خبراء برمجة الجافا .. أفيدونا ان كان لديكم علم حول موضوع المتصفح

الحقيقة انتهيت من عمل برنامج متصفح ولكن يلزمه بعض التعديلات فمن لديه اقتراح أو أي شيء حول البرنامج فليتفضل علينا ويضيف ..

ولدي مشكلتين رئيسيتين في هذا الموضوع ، الأولى أنه ان كان الجهاز يستخدم اتصال انترنت ماخود من سيرفر (أي يحتاج لبروكسي) فإن البرنامج لا يعمل ، والثانية طريقة عرض المواقع حيث أن المواضع التي تحتوي فورمز لا يتم عرضها بشكل صحيح ، مثال على ذلك : لو قمنا بفتح المنتدى فلن يظهر بالشكل السليم ..

مرفق لديكم كود البرنامج ، طبعاً لضيق الوقت تم عمله على هيئة كلاس واحد وسأعمل على تقسيمه الى كلاسين أو اكثر لاحقاً ..

كود البرنامج :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.net.URL;
import java.util.ArrayList;
/**
 * WebBroser class is used to make a Web Browser to browse an internet pages .
 * 
 * @author (your name) 
 * @version (06.March.2007)
 */
public class WebBrowser implements ActionListener

	private ArrayList<String> back_array = new ArrayList<String>();
	private ArrayList<String> next_array = new ArrayList<String>();
	private JScrollPane scroll;
	private JLabel status;
	private JFrame frame;
	private Container contentPane;
	private JTextField locationInput;
	private JEditorPane editPane;
	private JButton back_Button , next_Button , stop_Button, home_Button , refresh_Button , goButton;
	private String home = "http://www.google.com

		editPane = new JEditorPane();
		editPane.setEditable(false);
		/**
		 * make Hyperlink activation .
		 */
		editPane.addHyperlinkListener(new HyperlinkListener(){
			public void hyperlinkUpdate(HyperlinkEvent evt) {
				if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
					loadURL(evt.getURL());
				}
			}
		});
		scroll = new JScrollPane(editPane);
		contentPane.add(scroll,BorderLayout.CENTER);

		JToolBar toolbar = new JToolBar();
		toolbar.setLayout(new BorderLayout());
		toolbar.setFloatable(false);
		toolbar.setBorderPainted(false);
		locationInput = new JTextField(home, 40);
		locationInput.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				URL url;
				try {
					String location = locationInput.getText().trim();
					if (location.length() == 0)
					throw new Exception();
					if (!location.contains("://"))
					location = "http://" + location;
					back_array.add(location);
					next_array.add(location);
					url = new URL(location);
				}
				catch (Exception e) {
					return;
				}
				loadURL(url);
				locationInput.selectAll();
				locationInput.requestFocus();
				status.setText(locationInput.getText());
			}
		});

		JToolBar toolbar2 = new JToolBar();

		back_Button = new JButton("Back",new ImageIcon("Icons\\back.JPG"));
		back_Button.setToolTipText("Back");
		back_Button.addActionListener(this);

	  
		next_Button = new JButton("Next",new ImageIcon("Icons\\next.JPG"));
		next_Button.setToolTipText("Next");
		next_Button.addActionListener(this);

		stop_Button = new JButton(new ImageIcon("Icons\\stop.JPG"));
		stop_Button.setToolTipText("Stop");
		stop_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
			   editPane.removeAll();
			}
		});

		refresh_Button = new JButton(new ImageIcon("Icons\\refresh.JPG"));
		refresh_Button.setToolTipText("Refresh");
		refresh_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				URL url;
				try {
					String location = locationInput.getText();
					location = "http://" + location;
					back_array.add(location);
					next_array.add(location);
					url = new URL(location);
				}
				catch (Exception e) {
					return;
				}
				 loadURL(url);
				 locationInput.selectAll();
				 locationInput.requestFocus();
				 locationInput.setText(locationInput.getText());
				 status.setText(locationInput.getText());
			}
		});

		home_Button = new JButton(new ImageIcon("Icons\\home.JPG"));
		home_Button.setToolTipText("Go to Home page");
		home_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				URL url;
				try {
					String location = home;
					location = "http://" + location;
					back_array.add(location);
					next_array.add(location);
					url = new URL(location);
				}
				catch (Exception e) {
					return;
				}
				 loadURL(url);
				 locationInput.selectAll();
				 locationInput.requestFocus();
				 locationInput.setText(home);
				 status.setText(home);
			}
		});

		toolbar2.setLayout(new BoxLayout (toolbar2,BoxLayout.X_AXIS));
		toolbar2.setBorder(new EmptyBorder( 10, 10, 10, 10));

		toolbar2.add(back_Button);
		toolbar2.addSeparator(new Dimension(4,0));
		toolbar2.add(next_Button);
		toolbar2.addSeparator(new Dimension(4,0));
		toolbar2.add(stop_Button);
		toolbar2.addSeparator(new Dimension(4,0));
		toolbar2.add(refresh_Button);
		toolbar2.addSeparator(new Dimension(4,0));
		toolbar2.add(home_Button);
		toolbar2.setFloatable(false);
		toolbar2.setBorderPainted(false);

		goButton = new JButton(new ImageIcon("Icons\\go.JPG"));
		goButton.setToolTipText("Go");
		goButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				URL url;
				try {
					String location = locationInput.getText().trim();
					if (location.length() == 0)
					throw new Exception();
					if (!location.contains("://"))
					location = "http://" + location;
					back_array.add(location);
					next_array.add(location);
					url = new URL(location);
				}
				catch (Exception e) {
					return;
				}
				loadURL(url);
				locationInput.selectAll();
				locationInput.requestFocus();
				status.setText(locationInput.getText());
			}
		});

		JLabel label = new JLabel(" Location: ");

		toolbar.add(toolbar2,BorderLayout.NORTH);
		toolbar.add(label,BorderLayout.WEST);

		toolbar.add(locationInput,BorderLayout.CENTER);
		toolbar.add(goButton,BorderLayout.EAST);

		contentPane.add(toolbar,BorderLayout.NORTH);

		status = new JLabel();
		status.setText(locationInput.getText());
		status.setForeground(Color.blue);
		contentPane.add(status,BorderLayout.SOUTH);

		frame.pack();
		makeMenu();
		frame.setSize(600,500);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	/**
	* Loads the document at the specified URL into the edit pane.
	*/
   private void loadURL(URL url) {
	  try {
		 editPane.setPage(url);
	  }
	  catch (Exception e) {
		  editPane.setText( "Sorry, the requested document was not found\n"
			   +"or cannot be displayed.\n\nError:" + e);
	  }
   }

   /**
	* Change home page .
	*/
   public void setHome(){
		home = JOptionPane.showInputDialog("Enter your new home ?");
	}
	/**
	 * get home page .
	 */
	public String getHome(){
		return home;
	}
	 /**
	  * return back page .
	  */
	public String getBack(){
		if(back_array.size() > 1){
			next_array.add(back_array.get(back_array.size()-1));
			back_array.remove(back_array.size()-1);
			back_array.trimToSize();
			return back_array.get(back_array.size()-1);
		} 
		else{
			return home;
		}
	}
	/**
	 * return next page .
	 */
	public String getNext(){
	   if(next_array.size() > 1){
			back_array.add(next_array.get(next_array.size()-1));
			next_array.remove(next_array.size()-1);
			next_array.trimToSize();
			return next_array.get(next_array.size()-1);
		}
		else{
			return home;
		}
	}

	/**
	 * make menu bar and add some mnus to it .
	 */

	public void makeMenu(){
		menuBar = new  JMenuBar();
		frame.setJMenuBar(menuBar);

		fileMenu = new JMenu("File");
		menuBar.add(fileMenu);
		makeFileMenu();

		editMenu = new JMenu("Edit");
		menuBar.add(editMenu);
		makeEditMenu();

		viewMenu = new JMenu("View");
		menuBar.add(viewMenu);
		makeViewMenu();

		helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);
		makeHelpMenu();
	}
	/**
	 * make file menu and add items
	 */
	private void makeFileMenu(){

		newItem = new JMenuItem("New");
		newItem.setBackground(Color.white);
		newItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  makeFrame();
			}
		});
		fileMenu.add(newItem);
		fileMenu.add(new JSeparator());
		closeAll = new JMenuItem("Close All");
		closeAll.setBackground(Color.white);
		closeAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int ask = JOptionPane.showConfirmDialog(null,
				"Are you sure you want to close all Pages?\n"+"Close All Pages");
				if (ask == JOptionPane.YES_OPTION){
					System.exit(0);
				}	 
			}
		});
		fileMenu.add(closeAll);
		fileMenu.add(new JSeparator());//	add a Separator .
		exit = new JMenuItem("Exit");
		exit.setBackground(Color.white);
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 System.exit(0);
			}
		});
		fileMenu.add(exit);
	}

	/**
	 * Make help menu
	 */
	private void makeHelpMenu(){
		helpItem = new JMenuItem("help");
		helpItem.setBackground(Color.white);
		helpItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 JOptionPane.showMessageDialog(null,"Thanks for use my program ",
				 "Help",JOptionPane.PLAIN_MESSAGE);
			}
		});
		helpMenu.add(helpItem);
		helpMenu.add(new JSeparator());   //	add a Separator .
		aboutItem = new JMenuItem("About");
		aboutItem.setBackground(Color.white);
		aboutItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 JOptionPane.showMessageDialog(null,"Web Browser\n","About Web Browser",
				 JOptionPane.WARNING_MESSAGE);
			}
		});
		helpMenu.add(aboutItem);
	}

	/**
	 * make edit menu and add some items
	 */
	public void makeEditMenu(){

		changeHome = new JMenuItem("change Home page");
		changeHome.setBackground(Color.white);
		changeHome.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setHome();
			}
		});
		editMenu.add(changeHome);
	}
	/**
	 * make view menu and add items
	 */
	public void makeViewMenu(){

		backItem = new JMenuItem("Back");
		backItem.setBackground(Color.white);
		backItem.addActionListener(this); 
		viewMenu.add(backItem);

		viewMenu.add(new JSeparator());//	add Separator .

		nextItem = new JMenuItem("Next");
		nextItem.setBackground(Color.white);
		nextItem.addActionListener(this); 
		viewMenu.add(nextItem);
	}
	/**
	 * make menu items activation .
	 */
	public void actionPerformed(ActionEvent evt) {

		URL url;
		if(evt.getSource() == backItem || evt.getSource() == back_Button){
			get = getBack();
		}
		else if(evt.getSource() == nextItem || evt.getSource() == next_Button){
			get = getNext();
		}
		try {
			String location = get;
			url = new URL(location);
		}
		catch (Exception e) {
			return;
		}
		loadURL(url);
		locationInput.selectAll();
		locationInput.requestFocus();
		locationInput.setText(get);
		status.setText(get);
	} 
}

تم تعديل هذه المشاركة بواسطة nardien28 في 26 أبريل 2007 في 18:57

#2

السلام عليكم

الحقيقة فكرتك حلوه كتييييييييييييييير وانا جربتو والمتصفح كتير كويس

لكن الصراحة انا ما قدرت اساعدك في ه الموضوع لانو مافي عندي خبرة فيه

لكن اطلب من كل عضو ومن المشرفين ان لا يبخلو عليك في المساعده

وذلك حتى تعم الفائده على الجميع

وبالتوفيق لجميع الاعضاء

Moustafa

هذا الموضوع مغلق.

مواضيع مشابهة