一个简单的浏览器,输入URL以后
但是却不能显示出URL的页面。。
高手赐教
代码如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.URL;
import javax.swing.event.*;
import java.io.*;public class WebBrowser extends JApplet
implements ActionListener,HyperlinkListener{ private JEditorPane jep=new JEditorPane();//JEditor pane to viewer thml files超链接
private JLabel jlblURL=new JLabel("URl");//Label for URL
private JTextField jtfURL=new JTextField();//Text field for entering URL
public void init(){
JPanel jpURL=new JPanel();//Creat a panel to hold the label and text field
jpURL.setLayout(new BorderLayout());
jpURL.add(jlblURL,"West");
jpURL.add(jtfURL,"Center");
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(jep),"Center");
getContentPane().add(jpURL,"North");
jep.setEditable(false);
//place jpURL and jep in the applet
jep.addHyperlinkListener(this);
jtfURL.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
try{
URL url=new URL("http://www.sun.com/index.html"); //get the url from text field
System.out.println(jtfURL.getText());
jep.setPage(url); //display the html file
}catch(IOException ex){
System.out.println(ex);
}
}
public void hyperlinkUpdate(HyperlinkEvent e){
try{
jep.setPage(e.getURL());
}catch(IOException ex){
System.out.println(ex);
}
}
}
但是却不能显示出URL的页面。。
高手赐教
代码如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.URL;
import javax.swing.event.*;
import java.io.*;public class WebBrowser extends JApplet
implements ActionListener,HyperlinkListener{ private JEditorPane jep=new JEditorPane();//JEditor pane to viewer thml files超链接
private JLabel jlblURL=new JLabel("URl");//Label for URL
private JTextField jtfURL=new JTextField();//Text field for entering URL
public void init(){
JPanel jpURL=new JPanel();//Creat a panel to hold the label and text field
jpURL.setLayout(new BorderLayout());
jpURL.add(jlblURL,"West");
jpURL.add(jtfURL,"Center");
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(jep),"Center");
getContentPane().add(jpURL,"North");
jep.setEditable(false);
//place jpURL and jep in the applet
jep.addHyperlinkListener(this);
jtfURL.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
try{
URL url=new URL("http://www.sun.com/index.html"); //get the url from text field
System.out.println(jtfURL.getText());
jep.setPage(url); //display the html file
}catch(IOException ex){
System.out.println(ex);
}
}
public void hyperlinkUpdate(HyperlinkEvent e){
try{
jep.setPage(e.getURL());
}catch(IOException ex){
System.out.println(ex);
}
}
}
try {
URL url = new URL(jtfURL.getText());
jep.setPage(url); // display the html file
} catch (IOException ex) {
System.out.println(ex);
}
}注意:输入的url一定要是一个html页面,其他的好像不行,这应该跟JEditorPane()解析页面有关吧?!
比如http://www.sun.com/index.html就是html页面
现在的问题是即便就是http://www.sun.com/index.html它也解析不出来、、
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.URL;
import java.io.*;
import java.lang.*;public class Web extends JApplet{
private JEditorPane jep=new JEditorPane();
public void init(){
try{
URL url=new URL("http://www.sun.com/index.html");
getContentPane().add(new JScrollPane(jep));
jep.setPage(url);
}catch(IOException ex){
ex.printStackTrace();
}
}
}
连这个它都解析不出来
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.URL;
import java.io.*;
import java.lang.*;public class Web extends JApplet{
private JEditorPane jep=new JEditorPane();
public void init(){
try{
URL url=new URL("http://www.sun.com/index.html");
getContentPane().add(new JScrollPane(jep));
jep.setPage(url);
}catch(IOException ex){
ex.printStackTrace();
}
}
}