浏览器编写java代码 浏览器编写java代码怎么写

用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, index1);
}
for(Object i : ls)
{
System.out.print(i" ");
}
用java做一个简易浏览器import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
【浏览器编写java代码 浏览器编写java代码怎么写】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代码怎么写?网页地址在代码中的java代码写法如下:
packagecom.test;
importjava.lang.reflect.Method;
//实现打开浏览器并跳到指定网址的类
publicclassBareBonesBrowserLaunch{
publicstaticvoidopenURL(Stringurl){
try{
browse(url);
}catch(Exceptione){
}
}
privatestaticvoidbrowse(Stringurl)throwsException{
//获取操作系统的名字
StringosName=System.getProperty("os.name","");
if(osName.startsWith("MacOS")){
//苹果的打开方式
ClassfileMgr=Class.forName("com.apple.eio.FileManager");
MethodopenURL=fileMgr.getDeclaredMethod("openURL",newClass[]{String.class});
openURL.invoke(null,newObject[]{url});
}elseif(osName.startsWith("Windows")){
//windows的打开方式 。
Runtime.getRuntime().exec("rundll32url.dll,FileProtocolHandler" url);
}else{
//UnixorLinux的打开方式
String[]browsers={"firefox","opera","konqueror","epiphany","mozilla","netscape"};
Stringbrowser=null;
for(intcount=0;countbrowsers.lengthbrowser==null;count)
//执行代码,在brower有值后跳出,
//这里是如果进程创建成功了 , ==0是表示正常结束 。
if(Runtime.getRuntime().exec(newString[]{"which",browsers[count]}).waitFor()==0)
browser=browsers[count];
if(browser==null)
thrownewException("Couldnotfindwebbrowser");
else
//这个值在上面已经成功的得到了一个进程 。
Runtime.getRuntime().exec(newString[]{browser,url});
}
}
}
//主方法测试类
publicstaticvoidmain(String[]args){
Stringurl="";
BareBonesBrowserLaunch.openURL(url);
}
用JAVA编写的一个简易浏览器 。import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.DefaultStyledDocument;
//HTTP连接与浏览
public class HTTPBrowser extends JFrame{
JTextField jtfAddress; //输入html文件地址或网址
JTextPane jtpShow; //显示页面
JTextArea jtaSource; //显示HTML源文件
public HTTPBrowser(){
super("HTTP连接与浏览"); //调用父类构造函数
jtfAddress=new JTextField(30); //实例化地址输入框
jtpShow=new JTextPane(); //实例化显示内容框
jtaSource=new JTextArea();
JPanel p1=new JPanel(); //实例化面板
JSplitPane spane=new JSplitPane(JSplitPane.VERTICAL_SPLIT); //实例化分隔面板
p1.add(new JLabel("地址")); //增加组件到面板上
p1.add(jtfAddress);
spane.add(new JScrollPane(jtpShow),JSplitPane.TOP);
spane.add(new JScrollPane(jtaSource),JSplitPane.BOTTOM);
spane.setDividerLocation(130); //设置分隔位置
spane.setDividerSize(2); //设置分隔栏尺寸
Container container=getContentPane(); //得到容器
container.add(p1,BorderLayout.NORTH); //增加组件到容器上
container.add(spane,BorderLayout.CENTER);
jtfAddress.addActionListener(new ShowHTMLListener()); //输入地址文本域事件处理
setSize(380,300); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
class ShowHTMLListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
try{
URL address=new URL(jtfAddress.getText()); //得到HTML页面的URL地址
jtpShow.setContentType("text/html"); //设置内容格式
jtpShow.setPage(address); //设置显示页面
BufferedReader in= new BufferedReader(new InputStreamReader(address.openStream())); //获取输入流
String line;
StringBuffer content = new StringBuffer(); //文件内容
while ((line = in.readLine()) != null) { //读取文件
content.append(line "\n");
}
jtaSource.setText(new String(content)); //设置显示文本
in.close(); //关闭输入流
}
catch (Exception ex){
ex.printStackTrace(); //输出出错信息
}
}
}
public static void main(String[] args){
new HTTPBrowser();
}
}
这还有一个
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
//html浏览器
public class HTTPBrowserDemo extends JFrame{
JTextField jtfAddress; //输入html文件地址或网址
JButton jbGo; //转到文件按钮
JTextPane jtpShow; //显示文件
JLabel jlInfo; //提示信息
public HTTPBrowserDemo(){
super("html浏览器"); //调用父类构造函数
jtfAddress=new JTextField(20); //实例化地址输入框
jbGo=new JButton("转到"); //实例化转向按钮
jtpShow=new JTextPane(); //实例化显示内容框
jlInfo=new JLabel(); //实例化信息提示标签
JPanel panel=new JPanel(); //实例化面板
panel.add(new JLabel("地址")); //增加组件到面板上
panel.add(jtfAddress);
panel.add(jbGo);
JScrollPane jsp=new JScrollPane(jtpShow); //实例化滚动窗体
Container container=getContentPane(); //得到容器
container.add(panel,BorderLayout.NORTH); //增加组件到容器上
container.add(jsp,BorderLayout.CENTER);
container.add(jlInfo,BorderLayout.SOUTH);
jbGo.addActionListener(new ShowHTMLListener());//事件处理,发生按钮点击时显示页面内容
jtfAddress.addActionListener(new ShowHTMLListener());
setSize(350,280);//设置窗口尺寸
setVisible(true);//设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
}
class ShowHTMLListener implements ActionListener {//显示页面内容事件处理
public void actionPerformed(ActionEvent event) {
try{
jlInfo.setText("正在连接...");//显示提示信息
URL address=new URL(jtfAddress.getText());//得到HTML页面的URL地址
jtpShow.setPage(address); //设置显示页面
jlInfo.setText("完成");
}
catch (Exception ex){
jlInfo.setText("连接出错");
ex.printStackTrace(); //输出出错信息
}
}
}
public static void main(String[] args){
new HTTPBrowserDemo();
}
}
用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脚本1.为浏览器编写java代码了确保您的计算机上安装浏览器编写java代码了Java浏览器编写java代码,让我们打开程序,看看是否所有程序中都安装了Java 。
2.点击“查看更新” , 查看Java版本是否是最新版本,如果不是,则更新到最新版本 。
3.然后在安全选项中选择“配置Java”并选中“在浏览器中启用Java” 。
4.然后打开浏览器,在工具中找到“Internet选项”,然后单击open 。
5.在“安全”选项中,找到“自定义级别”并单击“打开” 。
6.在脚本下面,找到“Javaapplet脚本”,并检查“enable” 。配置之后,重新启动浏览器 。
关于浏览器编写java代码和浏览器编写java代码怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读