会计考友 发表于 2012-8-4 12:37:27

java浏览器JEditorPane的使用说明

import    java.awt.*;   import    javax.swing.*;
  import java.awt.event.*;
  import    java.io.*;
  import javax.swing.event.HyperlinkEvent;
  import javax.swing.event.HyperlinkListener;
  public class testEditPane extends JFrame{
  JEditorPane    editorPane;
  JLabel label;
  JTextField url;
  File    file;
  String str;
  JButton btn;
  public testEditPane()
  {
  this.setTitle("Java 浏览器");
  editorPane=new JEditorPane();
  label=new JLabel("URL");
  editorPane.setContentType("text/html");
  url=new JTextField(30);

会计考友 发表于 2012-8-4 12:37:28

java浏览器JEditorPane的使用说明

url.addKeyListener(new KeyListener() {
  public void keyPressed(KeyEvent e) {
  // TODO 自动生成方法存根
  if(e.getKeyChar()=='\n')
  {
  if(url.getText().startsWith("http://"))
  {
  try
  {
  editorPane.setPage(url.getText());
  editorPane.setEditable(false);
  }catch(Exception em)
  {
  em.printStackTrace();
  }
  }
  else {
  url.setText("您输入的网址不正确!");
  }
  }
  }

会计考友 发表于 2012-8-4 12:37:29

java浏览器JEditorPane的使用说明

public void keyReleased(KeyEvent arg0) {
  // TODO 自动生成方法存根
  }
  public void keyTyped(KeyEvent arg0) {
  // TODO 自动生成方法存根
  }});
  btn=new JButton("Go");
  btn.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
  // TODO 自动生成方法存根
  try
  {
  editorPane.setPage(url.getText());
  editorPane.setEditable(false);
  }catch(Exception em)
  {
  em.printStackTrace();
  }
  }});
  JPanel pnl=new JPanel();
  pnl.add(label,BorderLayout.WEST);
  pnl.add(url,BorderLayout.CENTER);
  pnl.add(btn,BorderLayout.EAST);
  this.getContentPane().add(pnl,BorderLayout.NORTH);
  editorPane.addHyperlinkListener(new HyperlinkListener() {
  public void hyperlinkUpdate(HyperlinkEvent e) {
  // TODO 自动生成方法存根

会计考友 发表于 2012-8-4 12:37:30

java浏览器JEditorPane的使用说明

try {
  if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED
  )
  editorPane.setPage(e.getURL());
  } catch (Exception ex) {
  ex.printStackTrace(System.err);
  }
  }
  });
  this.setSize(500,500);
  JScrollPane scrollPane=new JScrollPane();
  scrollPane.setViewportView(editorPane);
  this.getContentPane().add(scrollPane,BorderLayout.CENTER);
  this.addWindowListener(new WindowAdapter()    {
  public    void    windowClosing(WindowEvent    e)    {
  System.exit(0); }});
  this.setVisible(true);
  }
  /**
  * @param args
  */
  public static void main(String[] args) {
  // TODO 自动生成方法存根
  new testEditPane();
  }
  }
页: [1]
查看完整版本: java浏览器JEditorPane的使用说明