java代码做浏览器 支持java的浏览器有哪些
用java做一个简易浏览器
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("地址");
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();
}
}
}
}
}
用JAVA编写一个简单的浏览器程序
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.event.*;
@SuppressWarnings("serial")
class Win3 extends JFrame implements ActionListener,Runnable
{
JButton button;
URL url;
JTextField text;
JEditorPane editPane;
byte b[]=new byte[118];
Thread thread;
public Win3()
{
text=new JTextField(20);
editPane=new JEditorPane();
editPane.setEditable(false);
button=new JButton("确定");
button.addActionListener(this);
thread=new Thread(this);
JPanel p=new JPanel();
p.add(new JLabel("输入网址:"));
p.add(text);
p.add(button);
Container con=getContentPane();
con.add(new JScrollPane(editPane),BorderLayout.CENTER);
con.add(p,BorderLayout.NORTH);
setBounds(60,60,400,300);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editPane.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent e)
{
if(e.getEventType()==
HyperlinkEvent.EventType.ACTIVATED)
{
try{
editPane.setPage(e.getURL());
}
catch(IOException e1)
{
editPane.setText(""+e1);
}
}
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(!(thread.isAlive()))
thread=new Thread(this);
try{
thread.start();
}
catch(Exception ee)
{
text.setText("我正在读取"+url);
}
}
public void run()
{
try {
int n=-1;
editPane.setText(null);
url=new URL(text.getText().trim());
editPane.setPage(url);
}
catch(MalformedURLException e1)
{
text.setText(""+e1);
return;
}
catch(IOException e1)
{
text.setText(""+e1);
return;
}
}
}
public class Example3
{
public static void main(String args[])
{
new Win3();
}
}
JAVA代码,关于浏览器 求教
html
head
script
function move(){
var x = Math.floor(Math.random()*800);
var y = Math.floor(Math.random()*600);
var r = Math.floor(Math.random()*16).toString(16);
var g = Math.floor(Math.random()*16).toString(16);
var b = Math.floor(Math.random()*16).toString(16);
document.getElementById("prank1").style.marginLeft = x+"px";
document.getElementById("prank1").style.marginTop = y+"px";
x = Math.floor(Math.random()*800);
y = Math.floor(Math.random()*600);
r = Math.floor(Math.random()*16).toString(16);
g = Math.floor(Math.random()*16).toString(16);
b = Math.floor(Math.random()*16).toString(16);
document.getElementById("prank2").style.marginLeft = x+"px";
document.getElementById("prank2").style.marginTop = y+"px";
document.body.style.background ="#"+r+g+b;
}
function win(){
alert("You Win!!!");
}
/script
/head
body
button id="close" onclick="window.close()"close window/button
button id="prank1" onMouseMove="move()" onClick="win()"Click Me/button
button id="prank2" onMouseMove="move()" onClick="win()"Click Me/button
/body
/html
java开发手机浏览器 只要最基本的功能
3.WebBrowser.java
package 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 ()0url.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 ()0url.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 ()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函数
*/
public void hyperlinkUpdate (HyperlinkEvent e) {
/* if(e.getEventType () == HyperlinkEvent.EventType.ACTIVATED) {
String url = jurl.getText();
if(url.length ()0url.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);
}
}
用Java编写个浏览器,输入字符串以及需要查到的字符
Scanner input = new Scanner(System.in);
System.out.print("请输入一段字符:");
String s = input.next();
System.out.println("请输入要查询的字符串:");
String a = input.next();
int index = s.indexOf(a);
List ls = new ArrayList();
while (index = 0)
{
ls.add(index);
index = s.indexOf(a, index + 1);
}
for(Object i : ls)
{
System.out.print(i + " ");
}
如何用java编写浏览器
正好我前几天做了一个浏览器实例,挺容易的其实。就用到了:
1.awtswing编界面和响应。
2.网页显示区域是用JEditPane来显示的。通过在上方设置一个JTextField输入url以后,这里获得url,然后直接setPage到url就行了。也不是太难。
其他没啥如果楼主要代码的话留下邮箱。
楼主编浏览器大概需要以下知识:
1.javase基础
2.http协议大致传输过程
3.基础的awtswing,以及窗口响应布局等
4.浅显的多线程知识
文章题目:java代码做浏览器 支持java的浏览器有哪些
转载来源:http://cdiso.cn/article/hjpsgd.html