WebBrowser

swing做的web浏览器,应该不会有插件。
等我有时间了把他修改成swt的。
java 代码
 
  1. import java.io.IOException;  
  2. import java.net.URL;  
  3. import javax.swing.*;  
  4. import java.awt.*;  
  5. import java.awt.event.*;  
  6. import javax.swing.event.*;  
  7.   
  8. public class WebBrowser extends JFrame {  
  9.     JPanel contentPane; // 包含整个框架的容器  
  10.     BorderLayout borderLayoutAll = new BorderLayout();  
  11.     JLabel jLabelPrompt = new JLabel(); // 状态提示框  
  12.     JPanel jPanelMain = new JPanel();  
  13.     BorderLayout borderLayoutMain = new BorderLayout();  
  14.     JTextField textFieldURL = new JTextField(); // URL输入框  
  15.     JEditorPane jEditorPane = new JEditorPane(); // 显示网页内容的容器  
  16.   
  17.     public WebBrowser() { // 定义构造方法  
  18.         try {  
  19.             Init(); // 初始化并显示界面  
  20.         } catch (Exception e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.   
  25.     private void Init() throws Exception { // 界面初始化  
  26.         contentPane = (JPanel) getContentPane();  
  27.         contentPane.setLayout(borderLayoutAll);  
  28.         jPanelMain.setLayout(borderLayoutMain);  
  29.         jLabelPrompt.setText("请输入URL");  
  30.         textFieldURL.setText(""); // 清空文本框  
  31.         textFieldURL.addActionListener(new java.awt.event.ActionListener() {  
  32.             public void actionPerformed(ActionEvent e) {  
  33.                 textFieldURL_actionPerformed(e);  
  34.             }  
  35.         });  
  36.         jEditorPane.setEditable(false); // 设置不可编辑  
  37.         jEditorPane  
  38.                 .addHyperlinkListener(new javax.swing.event.HyperlinkListener() {  
  39.                     public void hyperlinkUpdate(HyperlinkEvent e) {  
  40.                         jEditorPane_hyperlinkUpdate(e);  
  41.                     }  
  42.                 });  
  43.         JScrollPane scrollPane = new JScrollPane();  
  44.         scrollPane.getViewport().add(jEditorPane);  
  45.         jPanelMain.add(textFieldURL, "North");  
  46.         jPanelMain.add(scrollPane, "Center");  
  47.         contentPane.add(jLabelPrompt, "North");  
  48.         contentPane.add(jPanelMain, "Center");  
  49.         enableEvents(AWTEvent.WINDOW_EVENT_MASK);  
  50.         this.setSize(new Dimension(600500));  
  51.         this.setTitle("迷你IE ");  
  52.         this.setVisible(true);  
  53.     }  
  54.   
  55.     void textFieldURL_actionPerformed(ActionEvent e) { // 输入地址后响应回车  
  56.         try {  
  57.             jEditorPane.setPage(textFieldURL.getText()); // 显示URL  
  58.         } catch (IOException ex) {  
  59.             JOptionPane msg = new JOptionPane();  
  60.             JOptionPane.showMessageDialog(this"URL地址不正确:"  
  61.                     + textFieldURL.getText(), "输入不正确!"0);  
  62.         }  
  63.     }  
  64.   
  65.     void jEditorPane_hyperlinkUpdate(HyperlinkEvent e) { // 响应页面打开超链接消息  
  66.         if (e.getEventType() == javax.swing.event.HyperlinkEvent.EventType.ACTIVATED) {  
  67.             try {  
  68.                 URL url = e.getURL(); // 从消息中得到URL  
  69.                 jEditorPane.setPage(url); // 显示页面内容  
  70.                 textFieldURL.setText(url.toString()); // 显示URL  
  71.             } catch (IOException io) {  
  72.                 JOptionPane msg = new JOptionPane();  
  73.                 JOptionPane.showMessageDialog(this"打开该链接失败!""输入不正确!"0);  
  74.             }  
  75.         }  
  76.     }  
  77.   
  78.     protected void processWindowEvent(WindowEvent e) { //处理窗体事件  
  79.         super.processWindowEvent(e);  
  80.         if (e.getID() == WindowEvent.WINDOW_CLOSING) {  
  81.             System.exit(0); // 关闭  
  82.         }  
  83.     }  
  84.   
  85.     public static void main(String[] args) { // Main函数  
  86.         new WebBrowser();  
  87.     }  
  88. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值