a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 163|回复: 0

[专业语言] Java认证:java浏览器JEditorPane的使用

[复制链接]
发表于 2012-8-4 12:44:44 | 显示全部楼层 |阅读模式
Java认证:java浏览器JEditorPane的使用
; c% |' I5 K: J( U' Zimport java.awt.*;
% X$ R7 c/ |6 @" l$ g% Jimport javax.swing.*;
+ i, E% E5 u; o3 B2 Simport java.awt.event.*;1 O5 \+ Z+ N- i
import java.io.*;
$ Y3 e) ?/ t0 G5 Kimport javax.swing.event.HyperlinkEvent;
. B. g3 g7 x/ M! S3 B( N4 {$ P2 Yimport javax.swing.event.HyperlinkListener;
- i8 |' a3 L( Tpublic class testEditPane extends JFrame{
) U" \7 q6 M. R% M$ \) z/ uJEditorPane editorPane;
" Q0 F% i& [. iJLabel label;
0 I$ ?+ C8 ^' Z8 g4 G1 @JTextField url;6 L$ A6 l9 z, L* E( E
File file;
/ m* b8 p! c% W" t3 _String str;' q8 e/ N$ t% y0 Q# t
JButton btn;# T0 ?- R$ N/ L" g+ y
public testEditPane()3 A: P5 k. y/ d: v+ o7 u
{
, L4 Y8 \: e/ ?5 u5 j3 Q3 |this.setTitle(“Java 浏览器”);
6 k7 f* d  g) B3 D5 jeditorPane=new JEditorPane();# \4 u- r* F) ?. [
label=new JLabel(“URL”);: ?6 c; X0 f; P# x
editorPane.setContentType(“text/html”);3 z* @, d* o: C, o
url=new JTextField(30);
% u, H" W) ^+ n) l( B& n4 u/ xurl.addKeyListener(new KeyListener() {
# V0 z# p* L) z8 C2 |1 Dpublic void keyPressed(KeyEvent e) {
& J4 M' f) l" K9 f% l$ V/ Z// TODO 自动生成方法存根! j2 z* f; Q' C9 ]/ `
if(e.getKeyChar()==‘\n’)
6 i2 F6 h) E6 d- Q' K: Z' `5 L{
. p# H8 p, h- Y, f* W" J4 a1 A" Tif(url.getText().startsWith(“http://”))6 o- N8 H1 v5 ^
{
4 z7 ~7 [/ Z( utry1 x- @- o8 o1 @& {5 R+ Q. w! d% x
{* m3 q! n+ h5 {8 c  b
editorPane.setPage(url.getText());8 G3 g8 Q0 k* F; w9 c8 ?1 u
editorPane.setEditable(false);
9 f# X8 T% u& b+ Q}catch(Exception em)
. K9 @9 I9 y! c" q5 }. A( Q+ W) D( V8 ^. b! @( G' k5 a' [2 }

9 k1 G+ @. R7 `' F4 @0 d{; j0 a+ J) S9 c1 D0 s" q% g
em.printStackTrace();) s! f( {) O) @! x
}
3 z# M! f+ T4 F1 j6 m* Q% A& e}
7 d% u9 |& F$ p0 J" y: }: [; \else {
' V) t. J; j/ Jurl.setText(“您输入的网址不正确!”);
! e" G8 _& S$ U+ T3 F}# ]" s$ {+ O) a8 C# A3 U. R. M
}  o0 [7 z$ B0 p0 C. L
}
) Y0 G. V. W  C6 w% A- z9 u. zpublic void keyReleased(KeyEvent arg0) {
* X1 B- i7 N7 h; h! c// TODO 自动生成方法存根! X: N; ~, k1 T. [* Y; r
}
  R3 i" Z# y( s$ m% Dpublic void keyTyped(KeyEvent arg0) {3 G+ M+ b) k3 M1 J8 ~
// TODO 自动生成方法存根2 z0 D: b+ F! k' M( B3 T
}});. M1 g6 z. s* U+ U$ J+ u
btn=new JButton(“Go”);
& \( o$ K. x% B9 n9 ~btn.addActionListener(new ActionListener() {6 r% |( h' j. ^' x1 E) x$ C. L
public void actionPerformed(ActionEvent e) {
' G' C, A& _" ~7 U  b) }9 O// TODO 自动生成方法存根
$ y1 e% n0 [2 \$ i! D/ qtry# Z) A; F( F: y
{
1 p: u6 ~4 f' G4 KeditorPane.setPage(url.getText());
' R& m  M/ ?6 x  R/ MeditorPane.setEditable(false);' O0 {2 F/ I! l
}catch(Exception em)7 W9 W( n, s2 B  W' }' O
{
& b- }' m9 n9 R2 `* Z5 Pem.printStackTrace();* s3 A) G" t# L& K
}4 ~8 }8 c( f9 ?- X- G1 [( y1 d' H1 P
}});
$ O  w: T9 u$ M) I9 ~1 E" X# N" AJPanel pnl=new JPanel();
- ^! E9 a4 J. a+ O  n) |! ~( qpnl.add(label,BorderLayout.WEST);8 ^  B# Q9 j( L
pnl.add(url,BorderLayout.CENTER);
% Q" h3 Y8 v" b; ?( s8 Ppnl.add(btn,BorderLayout.EAST);
- ]) M( I% u) c" A. P7 r9 K( Pthis.getContentPane().add(pnl,BorderLayout.NORTH);5 j9 X8 |8 S$ R, \3 @6 @9 _$ g! P5 W" d
editorPane.addHyperlinkListener(new HyperlinkListener() {" A5 d8 j9 ^0 ?) R* \
public void hyperlinkUpdate(HyperlinkEvent e) {! Y6 k, I4 r: s3 R7 ~4 r
// TODO 自动生成方法存根! C( V: u/ c2 ~9 C* ~0 n- U6 G4 F
try {+ L, M4 a" ^8 S, b! n; c4 r9 Z
if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED
9 Y; g: u: _, a' O: }8 Z: W, F; H; R- B4 q- {" |) j7 u- }9 O
editorPane.setPage(e.getURL());
/ v. N' P  B- q5 r/ l, j+ C} catch (Exception ex) {
% x, q& v% t8 v  g6 J
1 R* L: X, X1 b* v# n8 v# A* q# _7 `3 `4 g' ?
ex.printStackTrace(System.err);# t" ]8 a. E. |
}
$ O$ m" |$ V. u}
; U: ?: Z3 h( m( o});8 }4 _9 l  s5 f/ F& }- m
this.setSize(500,500);, E$ U9 j- U1 o, J9 }. T
JScrollPane scrollPane=new JScrollPane();
% G- ^4 A/ {3 \6 O6 B5 x. M+ uscrollPane.setViewportView(editorPane);
9 \# o& o; g, ~* O! K0 uthis.getContentPane().add(scrollPane,BorderLayout.CENTER);6 v/ n) M( q% B8 N" S& m
this.addWindowListener(new WindowAdapter() {
' S  p2 [. ^) Q9 @/ e$ opublic void windowClosing(WindowEvent e) {
% q! V( z) K5 q/ b9 g# CSystem.exit(0); }});% ~- T0 Q3 n' k2 E. b  z0 s
this.setVisible(true);" K  y. g& Z$ ]) S3 w  c" a4 T
}
: H# u5 `# d4 M" }9 E! w3 U/*** L) H+ K% L2 a: j  W, O! e
* @param args
1 e3 ]5 L; w* o7 f) d0 ]$ `*/: B9 Z* T2 Z1 v" w! G8 k2 a4 p1 D9 B
public static void main(String[] args) {1 \: a/ `$ I- ?3 \0 u2 V8 R
// TODO 自动生成方法存根
! o( b5 y* p& W  C+ ^/ w4 O2 W2 nnew testEditPane();' @, A7 q1 N! y
}: }7 y6 B: {4 M- C' `" _) J- S
}
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-26 15:31 , Processed in 0.652315 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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