手机代码查看器java 如何用手机查看代码( 五 )


bar.remove (button2);
pack();
}
});
window.setSize (size);
window.setVisible (true);
}
//查看源文件
else if (e.getSource() == sourceItem ||e.getSource() == picView){
url = jurl.getText ().toString ().trim ();
if(url.length ()0!url.startsWith ("http://")) {
url="http://"+url;
}
if( !url.equals ("")) {
//根据url,获得源代码
getHtmlSource (url);
//生成显示源代码的框架对象
ViewSourceFrame vsframe = new ViewSourceFrame (htmlSource);
vsframe.setBounds (0,0,800,500);
vsframe.setVisible(true);
}
else {
JOptionPane.showMessageDialog (WebBrowser.this,"请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
}
}
//刷新
else if (e.getSource() == reloadItem){
url=jurl.getText ();
if(url.length ()0url.startsWith ("http://")) {
try {
jEditorPane1.setPage (url);
jEditorPane1.setEditable(false); //add by copy editor :)
jEditorPane1.revalidate ();
}
catch(Exception ex) {
}
}
else if(url.length ()0!url.startsWith ("http://")) {
url="http://"+url;
try {
jEditorPane1.setPage (url );
jEditorPane1.setEditable(false); //add by copy editor :)
jEditorPane1.revalidate ();
}
catch(Exception ex) {
}
}
}
}
/*
**保存文件
*/
void saveFile (final String url) {
final String linesep = System.getProperty ("line.separator");
chooser1.setCurrentDirectory (new File ("."));
chooser1.setDialogType (JFileChooser.SAVE_DIALOG);
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函数
*/

推荐阅读