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

📄 sample23_5.java

📁 Java SE 6.0前19-25章示的示例代码,简单易学
💻 JAVA
字号:
package wyf.jc;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
//定义该类继承自JFrame
public class Sample23_5 extends JFrame implements ActionListener,HyperlinkListener
{
	//创建JPanel对象
	private JPanel jp=new JPanel();
	//创建用来显示网页的编辑器面板
	private JEditorPane jep=new JEditorPane();
	//创建用来输入地址的文本框
	private JTextField jtf=new JTextField();
	//创建按扭
	private JButton jb=new JButton("转到");
	//创建滚动窗口
	private JScrollPane jsp=new JScrollPane(jep);
	public Sample23_5()
	{
		//设置编辑器面板为不可编辑
		jep.setEditable(false);
		//设置JPanel的布局管理器
		jp.setLayout(null);
		//设置控件的大小位置
		jtf.setBounds(10,10,500,26);
		jb.setBounds(520,10,80,26);
		jsp.setBounds(5,40,602,430);
		//将控件添加进容器
		jp.add(jtf);
		jp.add(jb);
		jp.add(jsp);
		//为按钮与文本框注册ActionEvent事件监听器
		jb.addActionListener(this);
		jtf.addActionListener(this);
		//为编辑器面板注册HyperlinkEvent事件监听器
		jep.addHyperlinkListener(this);
		//将JPanel添加进窗体
		this.add(jp);
		//设置窗体的标题、大小位置以及可见性
		this.setTitle("浏览器示例");
		this.setResizable(false);
		this.setBounds(100,100,620,500);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent e)
	{
		//获取URL地址的字符串表示
		String url=jtf.getText().trim();
		try
		{
			//通过URL设置当前页面
			jep.setPage(url);			
		}
		catch(IOException ioe)
		{	
			//指定页面不能访问时报错
			this.errorMsg();
			ioe.printStackTrace();
		}
	}
	public void hyperlinkUpdate(HyperlinkEvent e)
	{
		try
		{
			//判断事件类型是否激活页面类型,若为激活页面类型则加载网页
			if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
			{//通过超链接设置当前页面
				jep.setPage(e.getURL());
			}
		}
		catch(IOException ioe)
		{	
			//指定页面不能访问时报错
			this.errorMsg();
			ioe.printStackTrace();
		}
	}
	//当指定页面不能访问时报错误信息的方法
	private void errorMsg()
	{
		//指定的URL地址非法时错误提示的信息
		try
		{
			File file=new File("error.html");
			jep.setPage(file.toURL());
		}
		catch(IOException ioe)
		{	
			ioe.printStackTrace();
		}
	}
	public static void main(String[] args)
	{
		//创建Sample23_5窗体对象
		new Sample23_5();
	}
}

⌨️ 快捷键说明

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