瀏覽器

import java.awt.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.io.*;

public class myBrowser implements ActionListener,HyperlinkListener {

 JLabel msgLbl;
 JTextField urlText;
 JEditorPane content;
 JScrollPane JSPanel;
 JPanel panel;
 Container con;
 JFrame mainJframe;
 
 public myBrowser(){
  mainJframe = new JFrame("我的瀏覽器");
  mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  con = mainJframe.getContentPane();
  
  msgLbl = new JLabel("輸入地址:");
  urlText = new JTextField();
  urlText.setColumns(20);
  urlText.addActionListener(this);
  panel = new JPanel();
  panel.setLayout(new FlowLayout());
  panel.add(msgLbl);
  panel.add(urlText);
  
  content = new JEditorPane();
  content.setEditable(false);
  content.addHyperlinkListener(this);
  JSPanel = new JScrollPane(content);
  
  con.add(panel,BorderLayout.NORTH);
  con.add(JSPanel,BorderLayout.CENTER);
  mainJframe.setSize(800,600);
  mainJframe.setVisible(true);
 
  
 }
 public void actionPerformed(ActionEvent e){
  try{
   URL url = new URL(urlText.getText());
   content.setPage(urlText.getText());
  }catch(MalformedURLException e1){
   JOptionPane.showMessageDialog(mainJframe, "連接錯誤1");
  }catch(IOException e1){
   JOptionPane.showMessageDialog(mainJframe, "連接錯誤2");
  }
 }
 
 public void hyperlinkUpdate(HyperlinkEvent e){
  if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
   try{
    URL url = e.getURL();
    content.setPage(url);
    urlText.setText(e.getURL().toString());
   }catch(MalformedURLException e1){
    JOptionPane.showMessageDialog(mainJframe, "連接錯誤3");
   }catch(IOException e1){
    JOptionPane.showMessageDialog(mainJframe, "連接錯誤4");
   }
  }
 }
 public static void main(String args[]){
  new myBrowser();
 }
}



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