jeditorpane java_如何使用Java浏览器JEditorPane

展开全部

import java.awt.*;

import java.awt.event.*;

import java.io.IOException;

import java.net.*;

import javax.swing.*;

import javax.swing.event.HyperlinkEvent;

import javax.swing.event.HyperlinkListener;

public class HTTPBrowserDemo extends JFrame {

private JLabel jlAddress,jlInfo;

private JTextField jtfAddress;

private JEditorPane jtpShow;

private JPanel panel;

private JButton btnGO;

public static void main(String[] args) {

HTTPBrowserDemo hbd=new HTTPBrowserDemo();

}

HTTPBrowserDemo(){

jlAddress=new JLabel("地址62616964757a686964616fe4b893e5b19e31333363393732");

jlInfo=new JLabel();

jtpShow=new JEditorPane();

panel=new JPanel();

jtfAddress=new JTextField(20);

btnGO=new JButton("转到");

add(panel,BorderLayout.NORTH);

add(jtpShow,BorderLayout.CENTER);

add(jlInfo,BorderLayout.SOUTH);

panel.add(jlAddress);

panel.add(jtfAddress);

panel.add(btnGO);

setVisible(true);

setSize(350, 280);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

btnGO.addActionListener(new ShowHTMLListener());

jtpShow.setEditable(false);

jtpShow.addHyperlinkListener(new MyHyperlinkListener());

}

class ShowHTMLListener implements ActionListener{

public void actionPerformed(ActionEvent e){

String str=jtfAddress.getText();

try {

if (!str.startsWith("http://")){

str="http://"+str;

}

jlInfo.setText("连接中...");

URL address=new URL(str);

jtpShow.setPage(address);

jlInfo.setText("完成");

} catch (MalformedURLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e2) {

// TODO: handle exception

}

}

}

class MyHyperlinkListener implements HyperlinkListener{

public void hyperlinkUpdate(HyperlinkEvent e) {

if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){

JEditorPane pane=(JEditorPane)e.getSource();

try {

pane.setPage(e.getURL());

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值