2

我在文本板中创建了这个,因为我们的讲师不希望我们使用 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
4

1 回答 1

2

它是一个小程序,需要对其进行签名才能访问套接字。未签名的小程序只允许连接到它来自的主机。查看已签名的小程序以及小程序可以做什么和不能做什么

编辑:显示 HTML 文档:

我不确定您的目标是什么,但如果您想显示 HTML 页面,您可能会发现它showDocument很有用。这是一个例子

于 2012-04-17T01:27:34.460 回答