JAVA實現簡易瀏覽器

在這裏插入圖片描述
在這裏插入圖片描述

package Browser;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class Browsertest extends JFrame {

	private JTextField enterfield;
	private JEditorPane contentsArea;
	public Browsertest()
	{
		super("Web瀏覽器");
		Container container = getContentPane();
		enterfield = new JTextField("http://www.google.com");
		enterfield.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent event)
					{
						getThePage(event.getActionCommand());
					}
				} );
		container.add(enterfield,BorderLayout.NORTH);
		contentsArea = new JEditorPane();
		contentsArea.setEditable(false);
		contentsArea.addHyperlinkListener(
				new HyperlinkListener() {
					public void hyperlinkUpdate(HyperlinkEvent event)
					{
						if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
							getThePage(event.getURL().toString());
					}
				});
		container.add(new JScrollPane(contentsArea),BorderLayout.CENTER);
		setSize(1000,800);
		setVisible(true);
	}
	private void getThePage(String location) {
		try {
			contentsArea.setPage(location);
			enterfield.setText(location);
		}catch(IOException ioException) {
			System.out.println("Bad URL");
		}
		
	}
	
	public static void main(String args[]) {
		Browsertest app = new Browsertest();
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章