//接上
//保存文件的函数
void saveFile(final String url){
final String linesep=System.getProperty("line.separator");
chooser1.setCurrentDirectory(new File("."));
chooser1.setDialogType(JFileChooser.APPROVE_OPTION);
chooser1.setDialogTitle("另存为");
if(chooser1.showSaveDialog(this)!=JFileChooser.APPROVE_OPTION)
return;
this.repaint();
Thread thread=new Thread(){
public void run(){
try{
java.net.URL source=new URL(url);
InputStream in=new
BufferedInputStream(source.openStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
File fileName=chooser1.getSelectedFile();
FileWriter out=new FileWriter(fileName);
BufferedWriter bw=new BufferedWriter(out);
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
}
bw.flush();
bw.close();
out.close();
String dMessage=url+"已经被保存至"+linesep
+fileName.getAbsolutePath();
String dTitle="另存为";
int dType=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component)null,dMessage,dTitle,dType);
}catch(java.net.MalformedURLException muex){
JOptionPane.showMessageDialog((Component)null,muex.toString(),"网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
catch(Exception ex){
JOptionPane.showMessageDialog((Component)null,
ex.toString(),"网页浏览器",JOptionPane.ERROR_MESSAGE);
}
}
};
thread.start();
}
//获得源代码的函数
void getHtmlSource(String url){
String linesep,htmlLine;
linesep=System.getProperty("line.separator");
htmlSource="";
try{
java.net.URL source=new URL(url);
InputStream in=new BufferedInputStream(source.openStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(in));
while((htmlLine=br.readLine())!=null){
htmlSource=htmlSource+htmlLine+linesep;
}
}
catch(java.net.MalformedURLException muex){
JOptionPane.showMessageDialog(WebBrowser.this, muex.toString(),"网页浏览器"
,JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(WebBrowser.this, e.toString(),"网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
}
//实现监听器接口的hyperlinkUpdate函数
public void hyperlinkUpdate(HyperlinkEvent e){
try{
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
jEditorPanel.setPage(e.getURL());
}catch(Exception ex){
ex.printStackTrace(System.err);
}
}
//主函数生成一个IE对象
public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception e){}
WebBrowser webBrowser=new WebBrowser();
webBrowser.pack();
webBrowser.setResizable(true);
webBrowser.setVisible(true);
}
}
//保存文件的函数
void saveFile(final String url){
final String linesep=System.getProperty("line.separator");
chooser1.setCurrentDirectory(new File("."));
chooser1.setDialogType(JFileChooser.APPROVE_OPTION);
chooser1.setDialogTitle("另存为");
if(chooser1.showSaveDialog(this)!=JFileChooser.APPROVE_OPTION)
return;
this.repaint();
Thread thread=new Thread(){
public void run(){
try{
java.net.URL source=new URL(url);
InputStream in=new
BufferedInputStream(source.openStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
File fileName=chooser1.getSelectedFile();
FileWriter out=new FileWriter(fileName);
BufferedWriter bw=new BufferedWriter(out);
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
}
bw.flush();
bw.close();
out.close();
String dMessage=url+"已经被保存至"+linesep
+fileName.getAbsolutePath();
String dTitle="另存为";
int dType=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component)null,dMessage,dTitle,dType);
}catch(java.net.MalformedURLException muex){
JOptionPane.showMessageDialog((Component)null,muex.toString(),"网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
catch(Exception ex){
JOptionPane.showMessageDialog((Component)null,
ex.toString(),"网页浏览器",JOptionPane.ERROR_MESSAGE);
}
}
};
thread.start();
}
//获得源代码的函数
void getHtmlSource(String url){
String linesep,htmlLine;
linesep=System.getProperty("line.separator");
htmlSource="";
try{
java.net.URL source=new URL(url);
InputStream in=new BufferedInputStream(source.openStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(in));
while((htmlLine=br.readLine())!=null){
htmlSource=htmlSource+htmlLine+linesep;
}
}
catch(java.net.MalformedURLException muex){
JOptionPane.showMessageDialog(WebBrowser.this, muex.toString(),"网页浏览器"
,JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(WebBrowser.this, e.toString(),"网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
}
//实现监听器接口的hyperlinkUpdate函数
public void hyperlinkUpdate(HyperlinkEvent e){
try{
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
jEditorPanel.setPage(e.getURL());
}catch(Exception ex){
ex.printStackTrace(System.err);
}
}
//主函数生成一个IE对象
public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception e){}
WebBrowser webBrowser=new WebBrowser();
webBrowser.pack();
webBrowser.setResizable(true);
webBrowser.setVisible(true);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货