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

📄 mywebbrowser.java

📁 Java 写的, Web Browser, 网络浏览器
💻 JAVA
字号:
package cn.eagle.mywebbrowser;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import cn.eagle.mywebbrowser.listener.ResponseListener;

public class MyWebBrowser extends JFrame implements ActionListener, HyperlinkListener {

	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;
	
//	public JTextField tfAddress = null;
	public JEditorPane epPage = null;
	public ToolPanel toolPanel = null;
	
	private String url = null;
	private ArrayList<String> urls = new ArrayList<String>();
	private int index = -1;
	
	public MyWebBrowser() {
//		tfAddress = new JTextField(60);
		epPage = new JEditorPane();
//		tfAddress.addActionListener(this);
		toolPanel = new ToolPanel();
	}
	
	public void launchFrame() {
		setTitle("网络浏览器");				// 汉字乱码???
		setSize(WIDTH, HEIGHT);
	//	setDefaultCloseOperation();
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		setLayout(new BorderLayout());
	//	add(tfAddress, BorderLayout.NORTH);
		add(toolPanel, BorderLayout.NORTH);
		add(new JScrollPane(epPage), BorderLayout.CENTER);
		toolPanel.addResponseListener(new WebResponseListener());
		toolPanel.setBackState(false);
		toolPanel.setForwardState(false);
		epPage.addHyperlinkListener(this);  // Observer pattern...
		
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {  // 响应enter事件...
//		url = tfAddress.getText();
//		if ( url.length() > 0 ) {
//			if ( url.startsWith("http://") == false ) 
//				url = "http://" + url;
//			try {
//				epPage.setPage(url);
//				epPage.setEditable(false);
//				epPage.revalidate();			// 这个重新激活是什么???	
//			} catch (IOException e1) {
//				e1.printStackTrace();
//			}
//			repaint();
//		}
	}
	
	public void showPage(String url) {
		if ( url.length() > 0 ) {
			if ( url.startsWith("http://") == false ) 
				url = "http://" + url;
			urls.add(url);
			index = urls.size() - 1;
			try {
				epPage.setPage(url);
				epPage.setEditable(false);
				epPage.revalidate();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void backPage() {
		if ( index - 1 < 0 ) return;
		try {
			epPage.setPage(urls.get(--index));
			epPage.setEditable(false);
			epPage.revalidate();
			toolPanel.setTfAddress(urls.get(index));
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	public void forwardPage() {
		if ( index + 1 >= urls.size() ) return;
		try {
			epPage.setPage(urls.get(++index));
			epPage.setEditable(false);
			epPage.revalidate();
			toolPanel.setTfAddress(urls.get(index));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void viewSourceCode(String url) {
		StringBuffer sb = new StringBuffer();
		String separator = System.getProperty("line.separator");
		String line;
		try {
			URL source = new URL(url);
			BufferedReader br = new BufferedReader(new InputStreamReader(source.openStream()));
			while ( (line = br.readLine()) != null ) {
				sb.append(line + separator);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		new SourceCodeDialog(sb.toString()).setVisible(true);
		
	}
	
	public void saveAsFile(final String url) {
		final JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File("."));
		if ( chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION ) return;
		new Thread() {
			public void run() {
				try {
					URL source = new URL(url);
					File filename = chooser.getSelectedFile();
					BufferedReader br = new BufferedReader(new InputStreamReader(source.openStream()));
					PrintWriter pw = new PrintWriter(new FileWriter(filename));
					String line;
					while ( (line = br.readLine()) != null ) {
						pw.println(line);
					}
					pw.close();
					br.close();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}.start();		
	}
	
	// 不知道, 这个Listener 起什么作用...
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED ) {
			try {
				epPage.setPage(e.getURL());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}		
	}
	
	
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new MyWebBrowser().launchFrame();
	}

	private class WebResponseListener implements ResponseListener {
		public void response(String command) {
			if ( command.startsWith("url") == true ) {
				showPage(command.substring(4));
			} else if ( command.startsWith("saveAs") == true ) {
				if ( index >= 0 ) {
					saveAsFile(urls.get(index));
				}
			} else if ( command.startsWith("back") == true ) {
				backPage();			
			} else if ( command.startsWith("forward") == true ) {
				forwardPage();
			} else if ( command.startsWith("quit") == true ) {
				
			} else if ( command.startsWith("viewSource") == true ) {
				if ( index >= 0 ) {
					viewSourceCode(urls.get(index));
				}
			}
			if ( index <= 0 || urls.size() == 0 )
				toolPanel.setBackState(false);
			else
				toolPanel.setBackState(true);
			if ( index == urls.size() - 1 )
				toolPanel.setForwardState(false);
			else
				toolPanel.setForwardState(true);
		}		
	}
		
}


class ToolPanel extends JPanel implements ActionListener {
	
	public JToolBar toolBar = new JToolBar();
	public JButton btnSaveAs = new JButton("另存为");
	public JButton btnBack = new JButton("后退");
	public JButton btnForward = new JButton("前进");
	public JButton btnViewSource = new JButton("查看源码");
	public JButton btnQuit = new JButton("推出");
	
	public JToolBar addressBar = new JToolBar();
	public JLabel lblAddress = new JLabel("地址栏");
	public JTextField tfAddress = new JTextField(60);
	public JButton btnOk = new JButton("转向"); 
	
	//public ResponseListener l = null;
	public ArrayList<ResponseListener> ls = new ArrayList<ResponseListener>();
	
	public ToolPanel() {	
		setLayout(new BorderLayout());
		toolBar.add(btnSaveAs);
		toolBar.addSeparator();
		toolBar.add(btnBack);
		toolBar.add(btnForward);
		toolBar.addSeparator();
		toolBar.add(btnViewSource);
		toolBar.addSeparator();
		toolBar.add(btnQuit);
		add(toolBar, BorderLayout.NORTH);
		btnSaveAs.addActionListener(this);
		btnBack.addActionListener(this);
		btnForward.addActionListener(this);
		btnViewSource.addActionListener(this);
		btnQuit.addActionListener(this);
		
		addressBar.add(lblAddress);
		addressBar.add(tfAddress);
		addressBar.add(btnOk);
		add(addressBar, BorderLayout.CENTER);
		tfAddress.addActionListener(this);
		btnOk.addActionListener(this);
		
	}		
	
	public void setBackState(boolean b) {
		btnBack.setEnabled(b);
	}
	
	public void setForwardState(boolean b) {
		btnForward.setEnabled(b);
	}
	
	public void setTfAddress(String content) {
		tfAddress.setText(content);
	}
	
	public void addResponseListener(ResponseListener l) {
		ls.add(l);
	}

	public void actionPerformed(ActionEvent e) {
		String command = "";
		if ( e.getSource() == btnSaveAs ) {
			command = "saveAs";
		} else if ( e.getSource() == btnBack ) {
			command = "back";
		} else if ( e.getSource() == btnForward ) {
			command = "forward";
		} else if ( e.getSource() == btnViewSource ) {
			command = "viewSource";
		} else if ( e.getSource() == btnQuit ) {
			command = "quit";
		} else if ( e.getSource() == tfAddress ) {
			command = "url " + tfAddress.getText().trim();
		} else if ( e.getSource() == btnOk ) {
			command = "url " + tfAddress.getText().trim();
		}
		for ( ResponseListener l : ls ) {
			l.response(command);
		}
	}
	
	
}

class SourceCodeDialog extends JDialog {
	
	public SourceCodeDialog(String content) {
		JTextArea contentPanel = new JTextArea();
		contentPanel.setText(content);
		add(new JScrollPane(contentPanel));
		this.setBounds(400, 400, 400, 400);
	}
	
} 






⌨️ 快捷键说明

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