a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 244|回复: 3

[基础知识] java浏览器JEditorPane的使用说明

[复制链接]
发表于 2012-8-4 12:37:27 | 显示全部楼层 |阅读模式
import    java.awt.*;   import    javax.swing.*;7 i+ H  q. c- |" Q
  import java.awt.event.*;
$ o' Q, X6 ~# c' V8 P" M3 E! u1 G  import    java.io.*;
- D. b3 e3 h% w/ @7 H+ k- g: H; q6 B  import javax.swing.event.HyperlinkEvent;
7 w4 i/ K  \% p5 h9 ~  import javax.swing.event.HyperlinkListener;- H* W. W0 T% V7 x! S. w
  public class testEditPane extends JFrame{: ^9 T' Y0 T3 [3 @
  JEditorPane    editorPane;( M* l& K( n+ I8 E2 ?7 N; o5 |
  JLabel label;: \! h' R1 \# M( }
  JTextField url;( S+ ?3 j, o1 }  P4 Y* @9 ?( P
  File    file;
& u$ c. v7 q9 r8 E# l) ~/ N  String str;
. q  E9 ]0 e: V9 ]6 n7 D  JButton btn;& F9 q; f' F+ K; K) x" M# f7 U  I
  public testEditPane()2 a( i: @2 R- K" T
  {
" r! ^) m2 h1 E' W) b  this.setTitle("Java 浏览器");
) g. u! D& U, N& I/ X, R  editorPane=new JEditorPane();
0 q" q4 K5 w2 a1 F7 d$ a" ~. i  label=new JLabel("URL");
, X' ?3 h0 V' o# n  ]  editorPane.setContentType("text/html");
0 g3 k/ f/ ~; \$ n  url=new JTextField(30);
回复

使用道具 举报

 楼主| 发表于 2012-8-4 12:37:28 | 显示全部楼层

java浏览器JEditorPane的使用说明

url.addKeyListener(new KeyListener() {1 ^. Z/ f; R: W) Q/ r: S. r
  public void keyPressed(KeyEvent e) {- ~$ t& U0 I6 X( f) g2 d
  // TODO 自动生成方法存根
( I: U. J, @( G5 m  if(e.getKeyChar()=='\n')
4 _' Z  D$ N  M; l! q7 C  {
) @9 I  X4 z; Y! Y5 [: b9 {: |; o  if(url.getText().startsWith("http://"))) B; l6 ]( s' @% o
  {
' B  v0 W1 P( ~4 z: Z) O. s! |  try; I. a4 T: Z  r: `; F" ~
  {0 I& U" m7 p1 r5 E
  editorPane.setPage(url.getText());' [* I/ q- b' C: ?# P$ r5 R
  editorPane.setEditable(false);* L! {$ M* v, _* `4 M3 F
  }catch(Exception em)5 G* f6 J% t! m' _3 z2 I5 b7 C
  {
* W, Y; \/ ~  b# B  em.printStackTrace();. P9 I2 R1 P% K5 @
  }+ W2 M7 q! M, Z; t' J) P" K
  }0 D  _- X) E5 Y" E
  else {
1 n  |& o% w- c. M0 o7 |. I  url.setText("您输入的网址不正确!");* p6 k4 A: u/ T0 ~+ ?6 Q0 I
  }
/ L4 m/ b4 m+ t2 \  }
9 H) I# q+ `" e1 M$ y  }
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-8-4 12:37:29 | 显示全部楼层

java浏览器JEditorPane的使用说明

public void keyReleased(KeyEvent arg0) {
; Q. s1 ~* q% H/ t$ l7 O  // TODO 自动生成方法存根+ L3 U% _' _3 Y& w5 b3 C
  }  Z0 g( ~5 V; ~" i9 e' d( y/ V& P- e
  public void keyTyped(KeyEvent arg0) {7 l2 a: D) {8 p  |) ~+ P9 b, |4 B
  // TODO 自动生成方法存根
$ w  d; V9 z1 d" ?6 N  }});
" ?# W. H. |) Q4 @2 Y2 g2 R1 J  btn=new JButton("Go");9 Y/ D5 }5 l% m$ b/ [
  btn.addActionListener(new ActionListener() {
/ n! r" T9 A! v( S1 O  public void actionPerformed(ActionEvent e) {- w4 G. ^: ~% j, ~& @
  // TODO 自动生成方法存根
' x; t) q# w; ^  try
( f0 x9 Y2 W6 o: r  {& G  t1 C/ o- H2 C) a' x3 n( @1 U9 r' ?
  editorPane.setPage(url.getText());
* p, W. Y) ^' Q8 |  editorPane.setEditable(false);
1 ?' Q4 j  N7 v7 Z% ?  }catch(Exception em)( S2 x/ `8 q7 H0 ]* k+ E4 N  y8 N
  {
- p2 @) s; C1 C& W/ T4 z  em.printStackTrace();
* u# N$ p  u9 k  }
. u& i+ h0 D' P7 e: d  `  }});
: r- f- ]/ w; t4 ~* ]1 e  JPanel pnl=new JPanel();# u8 r1 f4 [" @
  pnl.add(label,BorderLayout.WEST);* |9 Y# h8 J6 s2 D- A3 F5 y
  pnl.add(url,BorderLayout.CENTER);
& K2 g- W; I8 c  v# M3 S  pnl.add(btn,BorderLayout.EAST);$ [; u1 `9 D% F3 `/ I
  this.getContentPane().add(pnl,BorderLayout.NORTH);
' v) y) r; h* B  editorPane.addHyperlinkListener(new HyperlinkListener() {
2 S  T2 N. ]. a* [8 {  public void hyperlinkUpdate(HyperlinkEvent e) {! c# l8 a- O0 y) B, I* ?. ]
  // TODO 自动生成方法存根
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-8-4 12:37:30 | 显示全部楼层

java浏览器JEditorPane的使用说明

try {
- y& g% e) S% R0 R, U" x4 s. K  if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED
+ G: U' H, D& \5 P! U. N  )/ ?! S- _1 F6 n  @' r9 Z# ~
  editorPane.setPage(e.getURL());3 A/ Q  [" Z3 V3 }* v7 G
  } catch (Exception ex) {
& z8 ~6 T) t+ r7 t1 a) s  ex.printStackTrace(System.err);5 S0 q4 @9 n2 g$ t
  }
8 F( ~- t4 c" D& b+ O  }
5 f8 e' @3 g' v! {1 O6 ~' S) G! ?  });
9 x  k2 Y) d$ c& u9 v6 w  this.setSize(500,500);$ z/ r$ p( N- [- d8 c$ U% l! Y
  JScrollPane scrollPane=new JScrollPane();
) _1 B; v% ~5 y! s  scrollPane.setViewportView(editorPane);
9 {$ p' }0 Q. Z6 B! [  this.getContentPane().add(scrollPane,BorderLayout.CENTER);
3 ]9 I2 }6 |$ L+ c  this.addWindowListener(new WindowAdapter()    {: ]* U/ v+ w9 R" b+ G2 d3 M
  public    void    windowClosing(WindowEvent    e)    {: F1 y5 x% I6 t
  System.exit(0); }});. m/ s* C$ t2 J$ J; g, R
  this.setVisible(true);# k  s) Y) z2 ^( B
  }6 j. k8 c) }/ I/ C
  /**
4 x9 m9 C4 q4 K8 q! P" [  * @param args  @3 m, T: {& z2 p7 D$ I* T# N
  */
3 ^, N. R+ T' v3 [( Q  public static void main(String[] args) {1 f3 y3 y- k3 @4 H3 x: C& ^
  // TODO 自动生成方法存根
- m; j2 p6 E$ K8 Z7 s  new testEditPane();
7 x' u6 e$ Y6 Y  }
' @3 o. B4 N9 u4 u  a& o/ E  f/ H/ X! [2 J  }
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-7 01:16 , Processed in 0.188248 second(s), 27 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表