用java制作浏览器

pansiom 2005-12-14 09:27:07
请问怎么做?
...全文
766 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bovy 2005-12-22
  • 打赏
  • 举报
回复

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;

public class JavaExplorer extends JFrame
{
private final class LoadUrlAction extends MouseAdapter
{
/**
* @param meta
* @return
*/
public String getCharsetName(String meta)
{
if (null == meta || meta.trim().length() == 0)
{
return "";
}
meta = meta.trim().toLowerCase();
int index = 0;
if ((index = meta.indexOf("charset")) < 0)
{
return "";
}
if (index >= 0)
{
while (index < meta.length())
{
if (meta.charAt(index) != '=')
{
index++;
}
else
{
index++;
break;
}
}
int endIndex = index;
while (endIndex < meta.length())
{
if (meta.charAt(endIndex) != ';' && meta.charAt(endIndex) != '\"' && meta.charAt(endIndex) != '\'')
{
endIndex++;
}
else
{
break;
}
}
return meta.substring(index, endIndex);
}
return "";
}

public void mousePressed(MouseEvent e)
{
try
{
URL url = new URL(JavaExplorer.this.urlTxt.getText());
JavaExplorer.this.content.setPage(url);
// BufferedInputStream receiver = new
// BufferedInputStream(url.openStream());
// ByteArrayOutputStream _bodyByteBuffer = new
// ByteArrayOutputStream(4 * 1024);
// byte[] buffer = new byte[1024];
// int len = -1;
// do
// {
// len = receiver.read(buffer);
// if (len < 0)
// {
// break;
// }
// _bodyByteBuffer.write(buffer, 0, len);
// }
// while (len != 0);
// String charsetName =
// getCharsetName(_bodyByteBuffer.toString());
// String body;
// if (charsetName.length() > 0)
// {
// body = _bodyByteBuffer.toString(charsetName);
// }
// else
// {
// // if we can't get the charset,let's use the the lastest
// // charset.(usual default charset)
// body = _bodyByteBuffer.toString();
// }
// JavaExplorer.this.content.setText(body);
// JOptionPane.showMessageDialog(JavaExplorer.this, body);
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}

private JTextField urlTxt;

private SpringLayout springLayout;

private JEditorPane content;

/**
* Launch the application
*
* @param args
*/
public static void main(String args[])
{
try
{
JavaExplorer frame = new JavaExplorer();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* Create the frame
*/
public JavaExplorer()
{
super();
springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);
setTitle("Java 浏览器");
setBounds(100, 100, 648, 503);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setHorizontalAlignment(SwingConstants.RIGHT);
label.setHorizontalTextPosition(SwingConstants.RIGHT);
label.setText("地址:");
getContentPane().add(label);
springLayout.putConstraint(SpringLayout.SOUTH, label, 25, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, label, 90, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, getContentPane());
urlTxt = new JTextField();
urlTxt.setText("http://www.boasoft.com/t2s/index.jsp");
getContentPane().add(urlTxt);
springLayout.putConstraint(SpringLayout.SOUTH, urlTxt, 0, SpringLayout.SOUTH, label);
springLayout.putConstraint(SpringLayout.EAST, urlTxt, -75, SpringLayout.EAST, getContentPane());
springLayout.putConstraint(SpringLayout.NORTH, urlTxt, 0, SpringLayout.NORTH, label);
springLayout.putConstraint(SpringLayout.WEST, urlTxt, 5, SpringLayout.EAST, label);
final JButton goButton = new JButton();
goButton.addMouseListener(new LoadUrlAction());
goButton.setText("GO");
getContentPane().add(goButton);
springLayout.putConstraint(SpringLayout.SOUTH, goButton, 0, SpringLayout.SOUTH, urlTxt);
springLayout.putConstraint(SpringLayout.EAST, goButton, -5, SpringLayout.EAST, getContentPane());
springLayout.putConstraint(SpringLayout.NORTH, goButton, 0, SpringLayout.NORTH, urlTxt);
springLayout.putConstraint(SpringLayout.WEST, goButton, 5, SpringLayout.EAST, urlTxt);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane);
springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -5, SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, goButton);
springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 5, SpringLayout.SOUTH, label);
springLayout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, label);
content = new JEditorPane();
scrollPane.setViewportView(content);
}
}
gazhangting 2005-12-21
  • 打赏
  • 举报
回复
这位老兄是不是学软件的啊,你也能做出一个浏览器吗?我们能做的只是他的外壳,我们中国现在都没有一个.....
只是做了不少它的外壳啊,
良少 2005-12-19
  • 打赏
  • 举报
回复
不用自己写浏览器, 可以在界面里加入一个容器,这个容器中显示系统中已存在的其他浏览器,如IE,FireFox等。 一般都是这么做的,没有人傻到自己做浏览器的!
pansiom 2005-12-19
  • 打赏
  • 举报
回复
谢谢哦,
但我有点问题:
当输入http://www.5566.net后,有部分js显示出来了.

还有,我下了个JDIC API 但不知如何用,请问你知道不?
谢谢
Q19830409 2005-12-18
  • 打赏
  • 举报
回复
输入时有加http:// 才可以搜索网址 主要的插件 自己加
Q19830409 2005-12-18
  • 打赏
  • 举报
回复
主要用超连接一些属性而已
package 文昌浏览器;
import java.io.IOException;
import java.net.URL;
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class HtmlBrowser extends JFrame{
JPanel contentPane;//include the whole frame container
BorderLayout borderLayoutAll=new BorderLayout();
JLabel jLabelPrompt=new JLabel();
JPanel jPanelMain=new JPanel();
BorderLayout borderLayoutMain=new BorderLayout();
JTextField textFieldURL=new JTextField();
JEditorPane jEditorPane=new JEditorPane();

public HtmlBrowser() {//define the hash and struct it
try{jbInit();//inintalize the GUI
}
catch(Exception e){
e.printStackTrace();}

}
private void jbInit()throws Exception{//GUI inintalize
contentPane=(JPanel)getContentPane();
contentPane.setLayout(borderLayoutAll);
jPanelMain.setLayout(borderLayoutMain);
jLabelPrompt.setText("please translate url");
textFieldURL.setText(" ");// clear the textArea
textFieldURL.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e)
{textFieldURL_actionPerformed(e);}
});
jEditorPane.setEditable(false);//she zhi bu ke bianji
jEditorPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener()
{public void hyperlinkUpdate(HyperlinkEvent e){
jEditorPane_hyperlinkUpdate(e);}
});
JScrollPane scrollPane=new JScrollPane();
scrollPane.getViewport().add(jEditorPane);
jPanelMain.add(textFieldURL,"North");
jPanelMain.add(scrollPane,"Center");
contentPane.add(jLabelPrompt,"North");
contentPane.add(jPanelMain,"Center");
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
this.setSize(new Dimension(600,500));
this.setTitle("我的浏览器");
this.setVisible(true);

}
void textFieldURL_actionPerformed(ActionEvent e){// into enter to response
try{jEditorPane.setPage(textFieldURL.getText());
}
catch(IOException ex){JOptionPane msg=new JOptionPane();
JOptionPane.showMessageDialog(this,"URL地址不正确:"+textFieldURL.getText(),"输入不正确 !",0);
}
}
void jEditorPane_hyperlinkUpdate(HyperlinkEvent e)
{
if(e.getEventType ()==
javax.swing.event.HyperlinkEvent.EventType.ACTIVATED)
{
try{
URL url=e.getURL();
jEditorPane.setPage(url);
textFieldURL.setText(url.toString());}
catch(IOException io){
JOptionPane msg=new JOptionPane();
JOptionPane.showMessageDialog(this,"打开文件失败!"+textFieldURL.getText(),"输入的不正确!",0);
}
}
}
protected void processWindowEvent(WindowEvent e) { //chulichuangti
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String[] args) { //main hashu
new HtmlBrowser();
}


}

pansiom 2005-12-14
  • 打赏
  • 举报
回复
找到这些东东,不知是不是,怎样用:

WebBrowser (Sourceforge Plugin 1.1 API) - [ 翻译此页 BETA ]
java.lang.Object extended by net.sourceforge.mavenplugins.sourceforge.WebBrowser.
public class WebBrowser; extends java.lang.Object. Emulates a web browser.
Version:: $Revision: 1.2 $ $Date: 2004/02/04 23:48:15 $; Author:: Ludovic ...
______________________________________________________________
http://maven-plugins.sourceforge.net/maven-sourceforge-plugin/apidocs/net/sourceforge/mavenplugins/sourceforge/WebBrowser.html

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧