⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webbrower.java

📁 用java开发的浏览器
💻 JAVA
字号:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JWindow;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import sun.reflect.generics.scope.Scope;

import com.sun.media.sound.Toolkit;


public class WebBrower extends JFrame implements HyperlinkListener,ActionListener{

	//建立工具栏
	JToolBar bar=new JToolBar();
	//网页显示界面
	JTextField jurl=new JTextField(60);
	JEditorPane editorPane1=new JEditorPane();
	JScrollPane scrollPane=new JScrollPane(editorPane1);
	
	JFileChooser choose=new JFileChooser();
	JFileChooser chooser1=new JFileChooser();
	String htmlSource;
	JWindow window=new JWindow(WebBrower.this);
	
	JButton button2=new JButton("窗口还原");
	//Toolkit toolkit=Toolkit.getDefaultToolkit();
	
	//建立菜单栏
	JMenuBar menuBar1=new JMenuBar();
	//建立菜单组
	JMenu fileMenu=new JMenu("文件(F)");
	//菜单项
	JMenuItem saveAsItem=new JMenuItem("另存为(A)...");
	JMenuItem exitItem=new JMenuItem("退出(I)");
	
	JMenu editMenu=new JMenu("编辑(E)");
	JMenuItem backItem=new JMenuItem("后退");
	JMenuItem forwardItem=new JMenuItem("前进");
	
	JMenu viewMenu=new JMenu("视图(V)");
	JMenuItem fullscreenItem=new JMenuItem("全屏(U)");
	JMenuItem sourceItem=new JMenuItem("查看源代码(C)");
	JMenuItem reloadItem=new JMenuItem("刷新(R)");
	
	//建立工具栏
	JToolBar toolBar=new JToolBar();
	//建立工具栏中的按钮组件
	JButton picSave=new JButton("另存为");
	JButton picBack=new JButton("后退");
	JButton picForward=new JButton("前进");
	JButton picView=new JButton("查看源代码");
	JButton picExit=new JButton("退出");
	JLabel label=new JLabel("地址");
	JButton button=new JButton("转向");
	
	Box address=Box.createHorizontalBox();
	
	//ArrayList 对象用来存放历史地址
	private ArrayList history=new ArrayList();
	//整型变量,表示历史地址的访问顺序
	private int historyIndex;
	
	//初始化界面
	public WebBrower()
	{
		setTitle("网页浏览器");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		editorPane1.addHyperlinkListener(this);
		
		fileMenu.setMnemonic('F');
		saveAsItem.setMnemonic('S');
		saveAsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
		
		exitItem.setMnemonic('Q');
		exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
		
		//将菜单项加入到菜单组中
		fileMenu.add(saveAsItem);
		//在菜单项中添加隔离
		fileMenu.addSeparator();
		fileMenu.add(exitItem);
		
		backItem.setMnemonic('B');
		backItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK));
		
		forwardItem.setMnemonic('D');
		forwardItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
		
		editMenu.setMnemonic('E');
		editMenu.add(backItem);
		editMenu.add(forwardItem);
		viewMenu.setMnemonic('V');
		
		fullscreenItem.setMnemonic('U');
		fullscreenItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.CTRL_MASK));
		sourceItem.setMnemonic('C');
		sourceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
		reloadItem.setMnemonic('R');
		reloadItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
		
		Container contentPane=getContentPane();
		//设置大小
		scrollPane.setPreferredSize(new Dimension(100,500));
		contentPane.add(scrollPane,BorderLayout.SOUTH);
		
		//在工具栏添加按钮组件
		toolBar.add(picSave);
		toolBar.addSeparator();
		toolBar.add(picBack);
		//toolBar.addSeparator();
		toolBar.add(picForward);
		toolBar.addSeparator();
		toolBar.add(picView);
		toolBar.addSeparator();
		toolBar.add(picExit);
		
		contentPane.add(bar,BorderLayout.CENTER);
		contentPane.add(toolBar,BorderLayout.NORTH);
		
		viewMenu.add(fullscreenItem);
		viewMenu.add(sourceItem);
		viewMenu.addSeparator();
		viewMenu.add(reloadItem);
		
		menuBar1.add(fileMenu);
		menuBar1.add(editMenu);
		menuBar1.add(viewMenu);
		
		setJMenuBar(menuBar1);
		
		address.add(label);
		address.add(jurl);
		address.add(button);
		bar.add(address);
		
		//为组件添加事件监听
		saveAsItem.addActionListener(this);
		picSave.addActionListener(this);
		exitItem.addActionListener(this);
		picExit.addActionListener(this);
		backItem.addActionListener(this);
		picBack.addActionListener(this);
		forwardItem.addActionListener(this);
		picForward.addActionListener(this);
		fullscreenItem.addActionListener(this);
		sourceItem.addActionListener(this);
		picView.addActionListener(this);
		reloadItem.addActionListener(this);
		button.addActionListener(this);
		jurl.addActionListener(this);
		
		
	}
	public void actionPerformed(ActionEvent e)
	{
		String url="";
		if(e.getSource()==button)
		{
			//获得地址栏的内容
			url=jurl.getText();
			//地址不为空,且以http://开头
			if(url.length()>0&&url.startsWith("http://"))
			{
				try
				{
					//JEditorPane组件显示url的内容链接
					editorPane1.setPage(url);
					//将url的内容添加到ArrayList对象的history中
					history.add(url);
					//historyIndex的数值设为histroy对象的长度-1
					historyIndex=history.size()-1;
					//设置成非编辑状态
					//重新布局
					editorPane1.revalidate();
				}
				catch(Exception ex)
				{
					//如果链接失败,提示错误
					JOptionPane.showMessageDialog(WebBrower.this, "无法打开该搜索页","网页浏览器",JOptionPane.ERROR_MESSAGE);
					
				}
			}
			//地址不为空,且不以http://开头
			else if(url.length()>0&&!url.startsWith("http://"))
			{
				//在url前面添加http://
				url="http://"+url;
				try
				{
					editorPane1.setPage(url);
					history.add(url);
					historyIndex=history.size()-1;
					editorPane1.revalidate();
				}
				catch (Exception ex) {
					// TODO: handle exception
					JOptionPane.showMessageDialog(WebBrower.this, "无法打开该搜索页","网页浏览器",JOptionPane.ERROR_MESSAGE);
				}
			}
			//url为空
			else if(url.length()==0)
			{
				JOptionPane.showMessageDialog(WebBrower.this, "请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
			}
		}
		//输入地址后按回车键
		else if(e.getSource()==jurl)
		{
			url=jurl.getText();
			if(url.length()>0&&url.startsWith("http://"))
			{
				try
				{
					editorPane1.setPage(url);
					history.add(url);
					historyIndex=history.size()-1;
					editorPane1.revalidate();
					jurl.setMaximumSize(jurl.getPreferredSize());
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(WebBrower.this, "无法打开该搜索页","网页浏览器",JOptionPane.ERROR_MESSAGE);
				}
			}
			else if(url.length()>0&&!url.startsWith("http://"))
			{
				url="http://"+url;
				try
				{
					editorPane1.setPage(url);
					history.add(url);
					historyIndex=history.size()-1;
					editorPane1.revalidate();
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(WebBrower.this, "无法打开该搜索页","网页浏览器",JOptionPane.ERROR_MESSAGE);
				}
			}
			else if(url.length()==0)
			{
				JOptionPane.showMessageDialog(WebBrower.this, "请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
			}
		}
		else if(e.getSource()==picSave||e.getSource()==saveAsItem)
		{
			url=jurl.getText().toString().trim();
			if(url.length()>0&&!url.startsWith("http://"))
			{
				url="http://"+url;
				
			}
			if(!url.equals(""))
			{
				saveFile(url);
			}
			else
			{
				JOptionPane.showMessageDialog(WebBrower.this, "请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
			}
		}
		else if(e.getSource()==exitItem||e.getSource()==picExit)
		{
			System.exit(0);
		}
		else if(e.getSource()==backItem||e.getSource()==picExit)
		{
			historyIndex--;
			if(historyIndex<0)
			{
				historyIndex=0;
				
			}
			url=jurl.getText();
			try
			{
				//获得history对象中本地址之前访问的地址
				url=(String)history.get(historyIndex);
				editorPane1.setPage(url);
				jurl.setText(url.toString());
				editorPane1.revalidate();
			}
			catch(Exception ex)
			{}
		}
		else if(e.getSource()==forwardItem||e.getSource()==picForward)
		{
			historyIndex++;
			if(historyIndex>=history.size())
				historyIndex=history.size()-1;
			url=jurl.getText();
			try
			{
				//获得history对象中本地址之后的地址
				url=(String)history.get(historyIndex);
				editorPane1.setPage(url);
				jurl.setText(url.toString());
				editorPane1.revalidate();
			}
			catch(Exception ex)
			{}
		}
		//全屏
		else if(e.getSource()==fullscreenItem)
		{
			boolean add_button2=true;
			//获得屏幕大小
			//Dimension size=Toolkit.getDefaultToolkit.getScreenSize();
			Container content=window.getContentPane();
			content.add(bar,"North");
			content.add(scrollPane,"Center");
			if(add_button2==true)
			{
				bar.add(button2);
			}
			button2.addActionListener(new ActionListener()
			{

				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					WebBrower.this.setEnabled(true);
					window.remove(bar);
					window.remove(toolBar);
					window.remove(scrollPane);
					window.setVisible(false);
					
					scrollPane.setPreferredSize(new Dimension(100,500));
					getContentPane().add(scrollPane,BorderLayout.SOUTH);
					getContentPane().add(bar,BorderLayout.CENTER);
					getContentPane().add(toolBar,BorderLayout.NORTH);
					bar.remove(button2);
					pack();
				}				
			}
			);
		}
			else if(e.getSource()==sourceItem||e.getSource()==picView)
			{
				url=jurl.getText().toString().trim();
				if(url.length()>0&&!url.startsWith("http://"))
				{
					url="http://"+url;
				}
				if(!url.equals(""))
				{
					//获得源代码				
					 getHtmlSource(url);
					 //生成显示源代码的框架对象
					 ViewSourceFrame vsframe=new ViewSourceFrame(htmlSource);
					 vsframe.setBounds(0,0,800,500);
					 vsframe.setVisible(true);
				
				}
				else 
				{
					JOptionPane.showMessageDialog(WebBrower.this, "请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
				}		 
				
			}
			else if(e.getSource()==reloadItem)
			{
				url=jurl.getText();
				if(url.length()>0&&url.startsWith("http://"))
				{
					try{
				
					editorPane1.setPage(url);
					editorPane1.revalidate();
				}
				catch(Exception ex)
				{}
				}
				else if(url.length()>0&&!url.startsWith("http://"))
				{
					url="http://"+url;
					try
					{
					editorPane1.setPage(url);
					editorPane1.revalidate();
					}
					catch(Exception ex)
					{}
				}
				
			}
			
		
	}
	
	
	
	private void getHtmlSource(String url) {
		// TODO Auto-generated method stub
		String linesep,htmlLine;
		linesep=System.getProperty("line.separator");
		htmlLine="";
		try
		{
			java.net.URL source=new URL(url);
			InputStream in=new BufferedInputStream(source.openStream());
			BufferedReader br=new BufferedReader(new InputStreamReader(in));
			while((htmlLine=br.readLine())!=null)
			{
				htmlSource=htmlSource+htmlLine+linesep;
			}
		}
		catch(java.net.MalformedURLException muex)
		{
			JOptionPane.showMessageDialog(WebBrower.this,muex.toString(),"网页浏览器",JOptionPane.ERROR_MESSAGE);
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(WebBrower.this, e.toString(),"网页浏览器",JOptionPane.ERROR_MESSAGE);
		}
		
	}
	private void saveFile(final String url) {
		final String linesep=System.getProperty("line.separator");
		chooser1.setCurrentDirectory(new File("."));
		chooser1.setDialogType(JFileChooser.SAVE_DIALOG);
		chooser1.setDialogTitle("另存为...");
		if(chooser1.showSaveDialog(this)!=JFileChooser.APPROVE_OPTION)
			return;
		this.repaint();
		Thread thread=new Thread(){
			public void run()
			{
				try
				{
					java.net.URL source=new URL(url);
					InputStream in=new BufferedInputStream(source.openStream());
					BufferedReader br=new BufferedReader(new InputStreamReader(in));
					File fileName=chooser1.getSelectedFile();
					FileWriter out=new FileWriter(fileName);
					BufferedWriter bw=new BufferedWriter(out);
					String line;
					while((line=br.readLine())!=null)
					{
						bw.write(line);
						bw.newLine();
					}
					bw.flush();
					bw.close();
					out.close();
					String dMessage=url+"已经被保存至"+linesep+fileName.getAbsolutePath();
					String dTitle="另存为" ;
					int dType=JOptionPane.INFORMATION_MESSAGE;
					JOptionPane.showMessageDialog(null, dMessage,dTitle,dType);
										
				}
				catch(java.net.MalformedURLException muex)
				{
					JOptionPane.showMessageDialog(null, muex.toString(),"网页浏览器",JOptionPane.ERROR_MESSAGE);					
				}
				catch(Exception ex)
				{}
			}
		};
		thread.start();
		
		
	}
	public void hyperlinkUpdate(HyperlinkEvent e) {
		// TODO Auto-generated method stub
		try
		{
			if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
			{
				editorPane1.setPage(e.getURL());
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	//生成一个IE对象
	public static void main(String[] args) {
//		try
//		{
//			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
//		}
//		catch(Exception e)
//		{}		
		WebBrower webBrower=new WebBrower();
		webBrower.pack();
		webBrower.setVisible(true);
		
	}
	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -