java 显示网页中特定的超链接.docx

上传人:b****5 文档编号:3409069 上传时间:2022-11-22 格式:DOCX 页数:21 大小:19.26KB
下载 相关 举报
java 显示网页中特定的超链接.docx_第1页
第1页 / 共21页
java 显示网页中特定的超链接.docx_第2页
第2页 / 共21页
java 显示网页中特定的超链接.docx_第3页
第3页 / 共21页
java 显示网页中特定的超链接.docx_第4页
第4页 / 共21页
java 显示网页中特定的超链接.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

java 显示网页中特定的超链接.docx

《java 显示网页中特定的超链接.docx》由会员分享,可在线阅读,更多相关《java 显示网页中特定的超链接.docx(21页珍藏版)》请在冰豆网上搜索。

java 显示网页中特定的超链接.docx

java显示网页中特定的超链接

11.2.1显示网页中特定的超链接

4.程序模板

按模板要求,将【代码1】~【代码8】替换为Java程序代码。

URLExample.java

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

import.*;

importjava.io.*;

importjava.util.regex.*;

importjavax.swing.event.*;

classWinextendsJFrameimplementsActionListener,Runnable

{

JButtonbutton;

URLurl,newURL;

JTextFieldtext;

JEditorPaneeditPane;

byteb[]=newbyte[118];

Threadthread;

Containercon=null;

JPanelp;

publicWin()

{

text=newJTextField(20);

editPane=【代码1】//创建editPane

【代码2】//editPane设置为不可编辑状态

button=newJButton("确定");

button.addActionListener(this);

thread=newThread(this);

p=newJPanel();

p.add(newJLabel("输入网址:

"));

p.add(text);

p.add(button);

con=getContentPane();

con.add(newJScrollPane(editPane),BorderLayout.CENTER);

con.add(p,BorderLayout.NORTH);

setBounds(60,60,450,300);

setVisible(true);

validate();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicvoidactionPerformed(ActionEvente)

{

if(!

(thread.isAlive()))

thread=newThread(this);

try{

thread.start();

}

catch(Exceptionee)

{

text.setText("我正在读取"+url);

}

}

publicvoidrun()

{

try{

intm=-1;

editPane.setText(null);

url=【代码3】//使用构造方法URL(Strings)创建url,其中参数s由text中的文本指定

InputStreamin=【代码4】//url返回输入流

Filefile=newFile("temp.html");

ByteArrayOutputStreamwrite=newByteArrayOutputStream();

while((m=in.read(b))!

=-1)

{

write.write(b,0,m);

}

write.close();

in.close();

bytecontent[]=write.toByteArray();

Stringstr=newString(content);

Patternpattern;

Matchermatch;

pattern=Ppile("

//.*>*.育.*",Pattern.CASE_INSENSITIVE);

match=pattern.matcher(str);

FileOutputStreamout=newFileOutputStream(file);

while(match.find())

{

StringstrHREF=match.group();

strHREF="
"+strHREF;

out.write(strHREF.getBytes());

}

out.close();

newURL=file.toURL();

con.removeAll();

editPane=【代码5】//创建editPane

【代码6】//editPane设置为不可编辑状态

【代码7】//editPane显示newURL

con.add(p,BorderLayout.NORTH);

con.add(newJScrollPane(editPane),BorderLayout.CENTER);

con.validate();

validate();

editPane.addHyperlinkListener(newHyperlinkListener()

{

publicvoidhyperlinkUpdate(HyperlinkEvente)

{

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

{

try{

URLlinkURL=e.getURL();

【代码8】//editPane显示linkURL

}

catch(IOExceptione1)

{

editPane.setText(""+e1);

}

}

}

}

);

}

catch(MalformedURLExceptione1)

{

text.setText(""+e1);

return;

}

catch(IOExceptione1)

{

text.setText(""+e1);

return;

}

}

}

publicclassURLExample

{

publicstaticvoidmain(Stringargs[])

{

newWin();

}

}

11.2.2过滤网页中的内容

4.程序模板

按模板要求,将【代码1】~【代码8】替换为Java程序代码。

Example.java

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

import.*;

importjava.io.*;

importjava.util.regex.*;

importjavax.swing.event.*;

classWinextendsJFrameimplementsActionListener,Runnable

{

JButtonbutton;

URLurl,newURL;

JTextFieldtext;

JEditorPaneeditPane;

byteb[]=newbyte[118];

Threadthread;

Containercon=null;

JPanelp;

publicWin()

{

text=newJTextField(20);

editPane=【代码1】//创建editPane

【代码2】//editPane设置为不可编辑状态

button=newJButton("确定");

button.addActionListener(this);

thread=newThread(this);

p=newJPanel();

p.add(newJLabel("输入网址:

"));

p.add(text);

p.add(button);

con=getContentPane();

con.add(newJScrollPane(editPane),BorderLayout.CENTER);

con.add(p,BorderLayout.NORTH);

setBounds(60,60,460,380);

setVisible(true);

validate();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicvoidactionPerformed(ActionEvente)

{

if(!

(thread.isAlive()))

thread=newThread(this);

try{

thread.start();

}

catch(Exceptionee)

{

text.setText("我正在读取"+url);

}

}

publicvoidrun()

{

try{

intm=-1;

editPane.setText(null);

url=【代码3】//使用构造方法URL(Strings)创建url,其中参数s由text中的文本指定

InputStreamin=【代码4】//url返回输入流

Filefile=newFile("temp.html");

ByteArrayOutputStreamwrite=newByteArrayOutputStream();

while((m=in.read(b))!

=-1)

{

write.write(b,0,m);

}

write.close();

in.close();

bytecontent[]=write.toByteArray();

Stringstr=newString(content);

Patternpattern;

Matchermatch;

pattern=Ppile("",Pattern.CASE_INSENSITIVE);

match=pattern.matcher(str);

str=match.replaceAll("");

bytecc[]=str.getBytes();

ByteArrayInputStreaminByte=newByteArrayInputStream(cc);

FileOutputStreamout=newFileOutputStream(file);

bytedd[]=newbyte[1024];

while((m=inByte.read(dd,0,1024))!

=-1)

{

out.write(dd,0,m);

}

out.close();

inByte.close();

newURL=file.toURL();

con.removeAll();

editPane=【代码5】//创建editPane

【代码6】//editPane设置为不可编辑状态

【代码7】//editPane显示newURL

editPane.addHyperlinkListener(newHyperlinkListener()

{

publicvoidhyperlinkUpdate(HyperlinkEvente)

{

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

{

try{

URLlinkURL=e.getURL();

【代码8】//editPane显示linkURL

}

catch(IOExceptione1)

{

editPane.setText(""+e1);

}

}

}

}

);

con.add(p,BorderLayout.NORTH);

con.add(newJScrollPane(editPane),BorderLayout.CENTER);

con.validate();

validate();

}

catch(MalformedURLExceptione1)

{

text.setText(""+e1);

return;

}

catch(IOExceptione1)

{

text.setText(""+e1);

return;

}

}

}

publicclassExample

{

publicstaticvoidmain(Stringargs[])

{

newWin();

}

}

11.2.3使用套接字传输数据

4.程序模板

按模板要求,将【代码1】~【代码16】替换为Java程序代码。

客户端模板:

Client.java

import.*;

importjava.io.*;

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

classClientextendsJFrameimplementsRunnable,ActionListener

{

JButtonconnection,computer;

JTextFieldinputA,inputB,inputC;

JTextAreashowResult;

Socketsocket=null;

DataInputStreamin=null;

DataOutputStreamout=null;

Threadthread;

publicClient()

{

socket=newSocket();

connection=newJButton("连接服务器");

computer=newJButton("求三角形面积");

computer.setEnabled(false);

inputA=newJTextField("0",12);

inputB=newJTextField("0",12);

inputC=newJTextField("0",12);

BoxboxV1=Box.createVerticalBox();

boxV1.add(newJLabel("输入边A"));

boxV1.add(newJLabel("输入边B"));

boxV1.add(newJLabel("输入边C"));

BoxboxV2=Box.createVerticalBox();

boxV2.add(inputA);

boxV2.add(inputB);

boxV2.add(inputC);

BoxbaseBox=Box.createHorizontalBox();

baseBox.add(boxV1);

baseBox.add(boxV2);

Containercon=getContentPane();

con.setLayout(newFlowLayout());

showResult=newJTextArea(8,18);

con.add(connection);

con.add(baseBox);

con.add(computer);

con.add(newJScrollPane(showResult));

computer.addActionListener(this);

connection.addActionListener(this);

thread=newThread(this);

setBounds(100,100,360,310);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicvoidrun()

{

while(true)

{

try{

doublearea=【代码1】//in读入一个double型数据

showResult.append("\n三角形的面积:

\n"+area);

showResult.setCaretPosition((showResult.getText()).length());

}

catch(IOExceptione)

{

showResult.setText("与服务器已断开");

computer.setEnabled(false);

break;

}

}

}

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==connection)

{

try

{

if(socket.isConnected())

{

}

else

{

InetAddressaddress=InetAddress.getByName("127.0.0.1");

InetSocketAddresssocketAddress=【代码2】

//创建端口为4331、地址为address的socketAddress

【代码3】//socket建立和socketAddress的连接呼叫

in=newDataInputStream(【代码4】);//socket返回输入流

out=newDataOutputStream(【代码5】);//socket返回输出流

computer.setEnabled(true);

thread.start();

}

}

catch(IOExceptionee){}

}

if(e.getSource()==computer)

{

try

{

doublea=Double.parseDouble(inputA.getText()),

b=Double.parseDouble(inputB.getText()),

c=Double.parseDouble(inputC.getText());

if(a+b>c&&a+c>b&&b+c>a)

{

【代码6】//out写入a,发送到服务器

【代码7】//out写入b,发送到服务器

【代码8】//out写入c,发送到服务器

}

else

{

inputA.setText("你输入的3个数不构成三角形");

}

}

catch(Exceptionee)

{

inputA.setText("请输入数字字符");

}

}

}

publicstaticvoidmain(Stringargs[])

{

Clientwin=newClient();

}

}

服务器端模板:

Server.java

importjava.io.*;

import.*;

publicclassServer

{

publicstaticvoidmain(Stringargs[])

{

ServerSocketserver=null;

ServerThreadthread;

Socketyou=null;

while(true)

{

try{

server=【代码9】//创建在端口4331上负责监听的ServerSocket对象

}

catch(IOExceptione1)

{

System.out.println("正在监听");

}

try{

you=【代码10】//server返回和客户端相连接的Socket对象

System.out.println("客户的地址:

"+you.getInetAddress());

}

catch(IOExceptione)

{

System.out.println("正在等待客户");

}

if(you!

=null)

{

newServerThread(you).start();

}

else

{

continue;

}

}

}

}

classServerThreadextendsThread

{

Socketsocket;

DataOutputStreamout=null;

DataInputStreamin=null;

Strings=null;

ServerThread(Sockett)

{

socket=t;

try{

in=newDataInputStream(【代码11】);//socket返回输入流

out=newDataOutputStream(【代码12】);//socket返回输出流

}

catch(IOExceptione)

{}

}

publicvoidrun()

{

while(true)

{

doublea=0,b=0,c=0,area=0;

try{

a=【代码13】//in读入一个double型数据

b=【代码14】//in读入一个double型数据

c=【代码15】//in读入一个double型数据

doublep=(a+b+c)/2.0;

area=Math.sqrt(p*(p-a)*(p-b)*(p-c));

【代码16】//out写入area,发送到客户端

}

catch(IOExceptione)

{

System.out.println("客户离开");

break;

}

}

}

}

11.2.4基于UDP的图像传输

4.程序模板

按模板要求,将【代码1】~【代码12】替换为Java程序代码。

客户端模板:

Client.java

import.*;

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjava.io.*;

classClientextendsJFrameimplementsRunnable,ActionListener

{

JLabellabel=newJLabel();

JButtonb=newJButton("获取图像");

Containercon;

Client()

{

super("Iamac

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 学科竞赛

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1