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

📄 webbrowser.java

📁 J2SE作业 Java编写的浏览器 实现基本浏览器功能 适合初学者学习UDP协议
💻 JAVA
字号:
//网页浏览器 JEditorPane
import java.io.IOException;
import java.net.URL;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class WebBrowser extends JFrame{
	JPanel contentPane;
	BorderLayout borderLayoutAll=new BorderLayout();
	JLabel jLabelPrompt=new JLabel();
	JPanel jPanelMain=new JPanel();
	BorderLayout borderLayoutMain=new BorderLayout();
	JTextField textFieldURL=new JTextField();
	JEditorPane jEditorPane=new JEditorPane();
	public WebBrowser(){
		try{
			Init();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	private void Init() throws Exception{
		contentPane =(JPanel) getContentPane();
		contentPane.setLayout(borderLayoutAll);
		jPanelMain.setLayout(borderLayoutMain);
		jLabelPrompt.setText("Enter URL");
		textFieldURL.setText("");
		textFieldURL.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(ActionEvent e){
				textFieldURL_actionPerformed(e);
			}
		});
		jEditorPane.setEditable(false);
		jEditorPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener(){
			public void hyperlinkUpdate(HyperlinkEvent e){
				jEditorPane_hyperlinkUpdate(e);
			}
		});
		JScrollPane scrollPane=new JScrollPane();
		scrollPane.getViewport().add(jEditorPane);
		jPanelMain.add(textFieldURL,"North");
		jPanelMain.add(scrollPane,"Center");
		contentPane.add(jLabelPrompt,"North");
		contentPane.add(jPanelMain,"Center");
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		this.setSize(new Dimension(600,500));
		this.setTitle("Winilulu Internet Explorer");
		this.setVisible(true);
		
	}
	
	void textFieldURL_actionPerformed(ActionEvent e)	{
		try{
			if(textFieldURL.getText().startsWith("http://")==false)
					textFieldURL.setText("http://"+textFieldURL.getText());			
			jEditorPane.setPage(textFieldURL.getText());
			
		}catch(IOException ex){
			JOptionPane msg=new JOptionPane();
			JOptionPane.showMessageDialog(this, "URL wrong : "+textFieldURL.getText(),"Error!",0);
			
		}
		
	}
	
	void jEditorPane_hyperlinkUpdate(HyperlinkEvent e){
		if(e.getEventType()==javax.swing.event.HyperlinkEvent.EventType.ACTIVATED){
			try{
				URL url=e.getURL();
				jEditorPane.setPage(url);
				textFieldURL.setText(url.toString());
			}catch(IOException io){
				JOptionPane msg=new JOptionPane();
				JOptionPane.showMessageDialog(this, "Failur!","Error!",0);
			}
		}
		
	}
	
	protected void processWindowEvent(WindowEvent e){
		super.processWindowEvent(e);
		if(e.getID()==WindowEvent.WINDOW_CLOSING){
			System.exit(0);
		}
		
	}

	
	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		// TODO 自动生成方法存根
		JSplashWindowPlus splash=new JSplashWindowPlus();
		splash.start();
		try{
			Thread.sleep(2000);
			
		}
		catch(Exception ex){
			ex.printStackTrace();
			
		}
		new WebBrowser();

	}

}

⌨️ 快捷键说明

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