java开发手机浏览器 只要最基本的功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java开发手机浏览器 只要最基本的功能相关的知识,希望对你有一定的参考价值。
哪位爷爷给个源程序啊???
3.WebBrowser.javapackage CH04.源程序;
/*
**网页浏览器主程序
**WebBrowser.java
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;
import java.io.*;
import java.net.*;
import java.util.*;
public class WebBrowser extends JFrame implements HyperlinkListener,ActionListener
//建立工具栏用来显示地址栏
JToolBar bar=new JToolBar ();
//建立网页显示界面
JTextField jurl = new JTextField (60);
JEditorPane jEditorPane1 = new JEditorPane ();
JScrollPane scrollPane = new JScrollPane (jEditorPane1);
JFileChooser chooser=new JFileChooser ();
JFileChooser chooser1=new JFileChooser ();
String htmlSource;
JWindow window = new JWindow (WebBrowser.this);
JButton button2=new JButton ("窗口还原");
Toolkit toolkit = Toolkit.getDefaultToolkit();
//建立菜单栏
JMenuBar jMenuBar1 = new JMenuBar();
//建立菜单组
JMenu fileMenu = new JMenu ("文件(F)");
//建立菜单项
JMenuItem saveAsItem = new JMenuItem ("另存为(A)...");
JMenuItem exitItem=new JMenuItem ("退出(I)");
JMenu editMenu=new JMenu ("编辑(E)");
JMenuItem backItem=new JMenuItem ("后退");
JMenuItem forwardItem=new JMenuItem ("前进");
JMenu viewMenu=new JMenu ("视图(V)");
JMenuItem fullscreenItem=new JMenuItem ("全屏(U)");
JMenuItem sourceItem=new JMenuItem ("查看源码(C)");
JMenuItem reloadItem=new JMenuItem ("刷新(R)");
//建立工具栏
JToolBar toolBar = new JToolBar();
//建立工具栏中的按钮组件
JButton picSave = new JButton("另存为");
JButton picBack = new JButton("后退");
JButton picForward = new JButton("前进");
JButton picView = new JButton("查看源代码");
JButton picExit = new JButton("退出");
JLabel label=new JLabel ("地址");
JButton button=new JButton ("转向");
Box adress=Box.createHorizontalBox ();
//ArrayList对象,用来存放历史地址
private ArrayList history=new ArrayList();
//整型变量,表示历史地址的访问顺序
private int historyIndex;
/**
**构造函数
**初始化图形用户界面
*/
public WebBrowser()
setTitle ("网页浏览器");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//为jEditorPane1添加事件侦听
jEditorPane1.addHyperlinkListener (this);
//为组件fileMenu设置热键‘F’
fileMenu.setMnemonic('F');
saveAsItem.setMnemonic ('S');
//为“另存为”组件设置快捷键为ctrl+s
saveAsItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_S,InputEvent.CTRL_MASK));
exitItem.setMnemonic('Q');
exitItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_E,InputEvent.CTRL_MASK));
//将菜单项saveAsItem加入到菜单组fileMenu中
fileMenu.add (saveAsItem);
//在菜单项中添加隔离
fileMenu.addSeparator ();
fileMenu.add (exitItem);
backItem.setMnemonic ('B');
backItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_Z,InputEvent.CTRL_MASK));
forwardItem.setMnemonic('D');
forwardItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_P,InputEvent.CTRL_MASK));
editMenu.setMnemonic('E');
editMenu.add (backItem);
editMenu.add (forwardItem);
viewMenu.setMnemonic('V');
fullscreenItem.setMnemonic('U');
fullscreenItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_U,InputEvent.CTRL_MASK));
sourceItem.setMnemonic('C');
sourceItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_C,InputEvent.CTRL_MASK));
reloadItem.setMnemonic('R');
reloadItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_R,InputEvent.CTRL_MASK));
Container contentPane=getContentPane ();
//设置大小
scrollPane.setPreferredSize(new Dimension(100,500));
contentPane.add (scrollPane, BorderLayout.SOUTH);
//在工具栏中添加按钮组件
toolBar.add(picSave);
toolBar.addSeparator();
toolBar.add(picBack);
toolBar.add(picForward);
toolBar.addSeparator();
toolBar.add(picView);
toolBar.addSeparator();
toolBar.add(picExit);
contentPane.add (bar,BorderLayout.CENTER);
contentPane.add(toolBar,BorderLayout.NORTH);
viewMenu.add (fullscreenItem);
viewMenu.add (sourceItem);
viewMenu.addSeparator ();
viewMenu.add (reloadItem);
jMenuBar1.add (fileMenu);
jMenuBar1.add (editMenu);
jMenuBar1.add (viewMenu);
setJMenuBar (jMenuBar1);
adress.add (label);
adress.add (jurl);
adress.add (button);
bar.add (adress);
//为组件添加事件监听
saveAsItem.addActionListener(this);
picSave.addActionListener(this);
exitItem.addActionListener(this);
picExit.addActionListener(this);
backItem.addActionListener(this);
picBack.addActionListener(this);
forwardItem.addActionListener(this);
picForward.addActionListener(this);
fullscreenItem.addActionListener(this);
sourceItem.addActionListener(this);
picView.addActionListener(this);
reloadItem.addActionListener(this);
button.addActionListener(this);
jurl.addActionListener(this);
/**
**实现监听器接口的actionPerformed函数
*/
public void actionPerformed(ActionEvent e)
String url = "";
//点击转向按钮
if (e.getSource() == button)
//获得地址栏的内容
url=jurl.getText ();
//url不为“”,并且以“http://”开头
if(url.length ()>0&&url.startsWith ("http://"))
try
//JEditorPane组件显示url的内容链接
jEditorPane1.setPage (url);
//将url的内容添加到ArrayList对象history中
history.add(url);
//historyIndex的数值设为history对象的长度-1
historyIndex=history.size()-1;
//重新布局
jEditorPane1.setEditable(false); //add by copy editor :)
jEditorPane1.revalidate ();
catch(Exception ex)
//如果链接显示失败,则弹出选择对话框“无法打开该搜索页”
JOptionPane.showMessageDialog (WebBrowser.this,"无法打开该搜索页","网页浏览器",JOptionPane.ERROR_MESSAGE);
//url不为“”,并且不以“http://”开头
else if(url.length ()>0&&!url.startsWith ("http://"))
//在url前面添加“http://”
url="http://"+url;
try
jEditorPane1.setPage (url );
history.add(url);
historyIndex=history.size()-1;
jEditorPane1.setEditable(false); //add by copy editor :)
jEditorPane1.revalidate ();
catch(Exception ex)
JOptionPane.showMessageDialog (WebBrowser.this,"无法打开该搜索页","网页浏览器",JOptionPane.ERROR_MESSAGE);
//没有输入url,即url为空
else if(url.length ()==0)
JOptionPane.showMessageDialog (WebBrowser.this,"请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
//输入地址后点击回车
else if (e.getSource() == jurl)
url=jurl.getText ();
if(url.length ()>0&&url.startsWith ("http://"))
try
jEditorPane1.setPage (url);
history.add(url);
historyIndex=history.size()-1;
jEditorPane1.setEditable(false); //add by copy editor :)
jEditorPane1.revalidate ();
jurl.setMaximumSize (jurl.getPreferredSize ());
catch(Exception ex)
JOptionPane.showMessageDialog (WebBrowser.this,"无法打开该搜索页","网页浏览器",JOptionPane.ERROR_MESSAGE);
else if(url.length ()>0&&!url.startsWith ("http://"))
url="http://"+url;
try
jEditorPane1.setPage (url );
history.add(url);
historyIndex=history.size()-1;
jEditorPane1.setEditable(false); //add by copy editor :)
jEditorPane1.revalidate ();
catch(Exception ex)
JOptionPane.showMessageDialog (WebBrowser.this,"无法打开该搜索页","网页浏览器",JOptionPane.ERROR_MESSAGE);
else if(url.length ()==0)
JOptionPane.showMessageDialog (WebBrowser.this,"请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
//另存为...
else if (e.getSource() == picSave||e.getSource() == saveAsItem)
url = jurl.getText ().toString ().trim();
if(url.length ()>0&&!url.startsWith ("http://"))
url="http://"+url;
if(!url.equals (""))
//保存文件
saveFile(url);
else
JOptionPane.showMessageDialog (WebBrowser.this,"请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
//退出
else if (e.getSource() == exitItem ||e.getSource() == picExit)
System.exit(0);
//后退
else if (e.getSource() == backItem ||e.getSource() == picBack)
historyIndex--;
if(historyIndex < 0)
historyIndex = 0;
url = jurl.getText();
try
//获得history对象中本地址之前访问的地址
url = (String)history.get(historyIndex);
jEditorPane1.setPage(url);
jurl.setText(url.toString());
jEditorPane1.setEditable(false); //add by copy editor :)
jEditorPane1.revalidate ();
catch(Exception ex)
//前进
else if (e.getSource() == forwardItem ||e.getSource() == picForward)
historyIndex++;
if(historyIndex >= history.size())
historyIndex = history.size()-1;
url = jurl.getText();
try
//获得history对象中本地址之后访问的地址
url = (String)history.get(historyIndex);
jEditorPane1.setPage(url);
jurl.setText(url.toString());
jEditorPane1.setEditable(false); //add by copy editor :)
jEditorPane1.revalidate ();
catch(Exception ex)
//全屏
else if (e.getSource() == fullscreenItem)
boolean add_button2=true;
//获得屏幕大小
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Container content = window.getContentPane();
content.add (bar,"North");
content.add (scrollPane,"Center");
//button2为点击“全屏”后的还原按钮
if(add_button2==true)
bar.add (button2);
//为button2添加事件
button2.addActionListener (new ActionListener ()
public void actionPerformed (ActionEvent evt)
WebBrowser.this.setEnabled (true);
window.remove (bar);
window.remove (toolBar);
window.remove (scrollPane);
window.setVisible (false);
scrollPane.setPreferredSize (new Dimension (100,500));
getContentPane ().add (scrollPane,BorderLayout.SOUTH);
getContentPane ().add (bar,BorderLayout.CENTER);
getContentPane ().add (toolBar,BorderLayout.NORTH);
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 ()>0&&url.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函数
*/
public void hyperlinkUpdate (HyperlinkEvent e)
/* if(e.getEventType () == HyperlinkEvent.EventType.ACTIVATED)
String url = jurl.getText();
if(url.length ()>0&&url.startsWith ("http://"))
try
jEditorPane1.setPage (url);
jEditorPane1.revalidate ();
catch(Exception ex)
else if(url.length ()>0&&!url.startsWith ("http://"))
url="http://"+url;
try
jEditorPane1.setPage (url );
jEditorPane1.revalidate ();
catch(Exception ex)
*/
// Revised by copy editor :) Now with new code to help hyperlink
try
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
jEditorPane1.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.setVisible(true);
参考技术A 以前下的一个浏览器,不知道能不能再手机上用
1.MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.5.0_02 (Sun Microsystems Inc.)
Main-Class: WebBrowser
2.ViewSourceFrame.java
package CH04.源程序;
/*
**源代码框架
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;
import java.io.*;
import java.util.*;
class ViewSourceFrame extends JFrame implements ActionListener
JPanel contentPane;
JPanel panel1 = new JPanel ();
JPanel panel2 = new JPanel ();
Border border1;
JButton closebutton = new JButton ();
JButton savebutton = new JButton ();
JScrollPane jScrollPanel = new JScrollPane ();
JTextArea jTextArea1 = new JTextArea ();
String htmlSource;
/**
**构造函数,初始化图形用户界面
*/
public ViewSourceFrame (String htmlSource)
this.htmlSource = htmlSource;
enableEvents (AWTEvent.WINDOW_EVENT_MASK);
setSize (new Dimension (600,500));
setTitle ("源代码");
setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE);
contentPane = (JPanel)getContentPane ();
contentPane.setLayout (new BorderLayout());
panel2.setLayout (new FlowLayout());
savebutton.setText ("保存");
closebutton.setText ("退出");
closebutton.addActionListener(this);
savebutton.addActionListener(this);
jScrollPanel.getViewport ().add (jTextArea1,null);
border1 = BorderFactory.createEmptyBorder (4,4,4,4);
panel1.setLayout (new BorderLayout());
panel1.setBorder (border1);
panel1.add (jScrollPanel,BorderLayout.CENTER);
contentPane.add (panel1,BorderLayout.CENTER);
panel2.add (savebutton);
panel2.add (closebutton);
contentPane.add (panel2,BorderLayout.SOUTH);
this.jTextArea1.setEditable (true);
this.jTextArea1.setText (this.htmlSource);
//设置光标的位置,将其移动到文本区第0个字符
this.jTextArea1.setCaretPosition (0);
/**
**实现监听器接口的actionPerformed函数
*/
public void actionPerformed(ActionEvent e)
String url = "";
if (e.getSource() == closebutton)
dispose();
else if(e.getSource() == savebutton)
JFileChooser fc=new JFileChooser();
int returnVal=fc.showSaveDialog(ViewSourceFrame.this);
File saveFile=fc.getSelectedFile();
try
FileWriter writeOut = new FileWriter(saveFile);
writeOut.write(jTextArea1.getText());
writeOut.close();
catch (IOException ex)
System.out.println("保存失败");
java实现发送短信验证功能
现在基本每个网站或app用户注册页面都添加有手机短信验证功能,添加手机短信验证功能,能够有效的避免了非法注册和烂注册的情况,以及获取用户的有效联系方式,那么如何实现手机短信验证功能呢?下面就以java开发语言为例,详细为大家介绍下。一、实现手机短信验证功能的基本思路如下:
1、首先找一家靠谱的短信验证码接口服务商,获取短信验证码调用地址,和相关接入文档。这里强烈建议大家选择动力思维乐信短信验证码接口服务商,一家专业提供短信应用服务14年的企业;
2、在网站信息提交页面请求发送信息;
3、服务器向动力思维乐信短信接口平台提交请求;
4、动力思维乐信短信接口平台通过三大运营商将验证码内容发送到用户的手机中。
二、手机号码短信验证前台页面效果实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" > <html xmlns> <head> <title></title> <script src="js/jquery-1.4a2.min.js" type="text/javascript"></script> <script type="text/javascript"> /*-------------------------------------------*/ var InterValObj; //timer变量,控制时间 var count = 60; //间隔函数,1秒执行 var curCount;//当前剩余秒数 var code = ""; //验证码 var codeLength = 6;//验证码长度 function sendMessage() { curCount = count; var dealType; //验证方式 tel = $(’#tel’).val(); if(tel!=’’){ //验证手机有效性 var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; if(!myreg.test($(’#tel’).val())) { alert(’请输入有效的手机号码!’); return false; } tel = $(’#tel’).val(); //产生验证码 for (var i = 0; i < codeLength; i++) { code += parseInt(Math.random() * 9).toString(); } //设置button效果,开始计时 $("#btnSendCode").attr("disabled", "true"); $("#btnSendCode").val("请在" + curCount + "秒内输入验证码"); InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次 //向后台发送处理数据 $.ajax({ type: "POST", //用POST方式传输 dataType: "text", //数据格式:JSON url: ’yanzhengma.do’, //方法路径(根据实际地址) data: "&tel=" + tel + "&code=" + code, error: function (XMLHttpRequest, textStatus, errorThrown) { }, success: function (msg){ } }); }else{ alert(’请填写手机号码’); } } //timer处理函数 function SetRemainTime() { if (curCount == 0) { window.clearInterval(InterValObj);//停止计时器 $("#btnSendCode").removeAttr("disabled");//启用按钮 $("#btnSendCode").val("重新发送验证码"); code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效 } else { curCount--; $("#btnSendCode").val("请在" + curCount + "秒内输入验证码"); } } </script> </head> <body> <input name="tel" id=tel type="text" /> <input id="btnSendCode" type="button" value="发送验证码" onclick="sendMessage()" /></p> </body> </html>
三、在yanzhengma.do方法里调用接口方法
/** * 调用短信服务器短信接口 * @param accName 乐信账号用户名 * @param accPwd 乐信账号密码 * @param seed 当前时间 格式:YYYYMMDD HHMISS 例如:20130806102030 * @param aimcodes 手机号多个手机号之间英文半角逗号隔开 * @param content 内容后加签名 * @param schTime 定时时间格式如:2010-01-01 08:00:00 * @return 服务端返回的结果 ok:业务id 或者 错误代码 */ public static String sendSms(String accName,String accPwd,String mobies,String content,String schTime){ StringBuffer sb = new StringBuffer("http://sdk.lx198.com/sdk/send2?"); try { String seed=new SimpleDateFormat(dateFormatStr).format(new Date()); sb.append("&accName="+accName); sb.append("&seed="+seed); sb.append("&accPwd="+MD5.getMd5String(MD5.getMd5String(accPwd)+seed)); sb.append("&aimcodes="+mobies); sb.append("&schTime="+URLEncoder.encode(schTime,"UTF-8")); //空格标点符号做encode转换 sb.append("&content="+URLEncoder.encode(content,"UTF-8")); //中文做encode转换 URL url = new URL(sb.toString()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); return in.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; }四、提交表单信息时对短信验证码验证
//手机验证码开始
session_start(); $svalitel = $_SESSION[’mobilecode’]; $vdcodetel = empty($vdcodetel) ? ’’ : strtolower(trim($vdcodetel)); if(strtolower($vdcodetel)!=$svalitel || $svalitel==’’) { ResetVdValue(); //echo "Pageviews=".$vdcodetel; ShowMsg("手机验证码错误!", ’-1’); exit(); }相关阅读:
乐信短信验证接口接入指南:http://www.lx598.com/jrzn.html
乐信短信验证接口API文档:http://www.lx598.com/apitext.html
乐信短信验证接口java demo:http://www.lx598.com/javaCode.html
以上是关于java开发手机浏览器 只要最基本的功能的主要内容,如果未能解决你的问题,请参考以下文章