我在textpad中创建了这个,因为我们的教师不希望我们使用IDE。我正在尝试输入网站并在JEditorPane
中查看。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
public class ViewRemoteFile extends JApplet
{
private JButton jbtView = new JButton("View");
private JTextField jtfURL = new JTextField(12);
private JEditorPane jtaFile = new JEditorPane();
private JLabel jlblStatus = new JLabel();
public void init()
{
JPanel p1 = new JPanel();
p1.add(new JLabel("URL"), BorderLayout.WEST);
p1.add(jtfURL, BorderLayout.CENTER);
p1.add(jbtView, BorderLayout.EAST);
add(new JScrollPane(jtaFile), BorderLayout.CENTER);
add(p1, BorderLayout.NORTH);
add(jlblStatus, BorderLayout.SOUTH);
jbtView.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showFile();
}
});
}
private void showFile()
{
//Scanner input = null;
//URL url = null;
try
{
jtaFile.setPage(new URL(jtfURL.getText().trim()));
jlblStatus.setText("File loaded successfully");
}
catch(MalformedURLException ex)
{
jlblStatus.setText("URL " + jtfURL.getText().trim() + " not found.");
}
catch(IOException ex)
{
jlblStatus.setText(ex.getMessage());
}
/*finally
{
if(input != null)
input.close();
}*/
}
}
当我运行此代码时,它不会显示该网站,并且会因为大量错误而崩溃并填满我的控制台。
http://www.yahoo.com我得到了:
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.security.AccessControlException: access denied ("java.net.Socket
Permission" "www.yahoo.com:80" "connect,resolve")
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:366)
at java.security.AccessController.checkPermission(AccessController.java:
555)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:466)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:213)
at sun.net.www.http.HttpClient.New(HttpClient.java:300)
at sun.net.www.http.HttpClient.New(HttpClient.java:316)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLC
onnection.java:992)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConne
ction.java:928)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection
.java:846)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:1296)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468
)
... 40 more
答案 0 :(得分:2)
它是一个applet,需要签名才能访问套接字。未签名的小程序仅允许连接到它来自的主机。查看Signed Applets和What Applets Can and Cannot Do
编辑:显示HTML文档:
我不确定您的目标是什么,但如果您想显示HTML页面,您可能会发现showDocument
有用。这是一个example。