JAVA做的IE浏览器

package com.yw.ie;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class WebBrowser extends JFrame{
 JPanel contentPane;
 BorderLayout borderLayoutAll=new BorderLayout();
 JLabel jLabelPrompt=new JLabel(); //状态提示框
 BorderLayout borderLayoutMain=new BorderLayout();
 JPanel jPanelMain=new JPanel();
 JTextField textFieldURL=new JTextField(); //URL输入框
 JEditorPane jEditorPane=new JEditorPane(); //显示网页容器
 
 /**
  * 构造函数
  * @param args
  */
 public WebBrowser(){
  
   try {
    Init();
   } catch (Exception e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   }
  
 }
 private void Init() throws Exception{
  contentPane=(JPanel) getContentPane();
  contentPane.setLayout(borderLayoutAll);
  jPanelMain.setLayout(borderLayoutMain);
  jLabelPrompt.setText("请输入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("迷你IE");
  this.setVisible(true);
 }
 void textFieldURL_actionPerformed(ActionEvent e){
  try {
   jEditorPane.setPage(textFieldURL.getText());//显示URL
  } catch (IOException e1) {
   JOptionPane msg=new JOptionPane();
   JOptionPane.showMessageDialog(this, "URL地址不正确:"+textFieldURL.getText(),"输入不正确",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 (Exception io) {
    // TODO: handle exception
    JOptionPane msg=new JOptionPane();
    JOptionPane.showMessageDialog(this,"打开该链接失败!","输入不正确",0);
   }
   
  }
 }
 protected void processWindowEvent (WindowEvent e){
  super.processWindowEvent(e);
  if(e.getID()==WindowEvent.WINDOW_CLOSING){
   System.exit(0);
  }
 }
 public static void main(String[] args) {
  
  new WebBrowser();
 }
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值