如何使用Java浏览器JEditorPane

原创|其它|编辑:郝浩|2009-11-25 11:31:33.000|阅读 926 次

概述:本文介绍了如何使用Java浏览器JEditorPane。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  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);
  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("您输入的网址不正确!");
  }
  }
  }
  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 自动生成方法存根
  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();
  }
  }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP