当前位置: 首页 > 知识库问答 >
问题:

小程序无法在浏览器中工作

耿联
2023-03-14

好吧,我知道有很多关于这个的问题和答案,但我真的没有任何运气。我有一个多类、多包程序,它也使用一些外部库(作为jar文件)。我也将我的项目导出为jar文件,下面是我的“index.html”,它引用了必要的库和我的jar文件。所有这些文件都放在同一个目录中,我可以在我的网页上看到小程序:http://easlnx01.eas.muohio.edu/~whitetc2/Twitter挖掘2/

<html>
<head>
<title>Java Example</title>
</head>

<body>
<center>
This is my page<br>
Below you see an applet<br>
<br>
<applet codebase ="." code="main.BasicGUI.class"
    archive="twitter.jar, jsch-0-1.1.48.jar, twitter4j-core-2.2.5.jar, sftp.jar"
    height="600" width="450"/>
</applet>
</center>
</body>
</html> 

我的主类(BasicGUI.java)扩展了JApplet,并根据输入调用了各种其他类。它还具有public void init()。谁能告诉我为什么这不完全有效?以下是我的网站的链接,该网站将在此处上载:http://easlnx01.eas.muohio.edu/~whitetc2/Twitter挖掘2/

如您所见,小程序出现了,但现在“文件”选项卡中的“选项”面板无法工作,程序本身也无法正常运行。

这是我的主要基本界面。java类:

package main;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JApplet;

import com.jscape.inet.sftp.SftpException;

import Twitter.SearchTweets;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/** 
 *  The GUI for our Twitter Widget. Plans are to eventually
 *  set this up with a Java Applet to run straight from the 
 *  CC teams website.
 *  @author Taylor White and Alex Meyer
 */
public class BasicGUI extends JApplet{

    //Auto-generated 
    private static final long serialVersionUID = 1L;

    //Aesthetic options, have getters and setters to allow for communication across GUIs
    private static int opposite;
    private static int maxTweets = 100;
    private static boolean sortByLocation = false;
    private static boolean isOptionsOpen = false;

    // Options GUI, initiated here to avoid multiple open windows
    //private OptionsGUI options;

    // Initialize all swing objects.
    private JPanel northSubPnl1; 
    private JPanel northSubPnl2;
    private JPanel northSubPnl3;
    private JPanel northPnl;
    private JPanel eastPnl;
    private JPanel westPnl;
    private JLabel myQuery;

    /** TWITTER INITILIAZATIONS **/
    private static Twitter.TwitterDataList tweets;
    /** END TWITTER INITIALIZATIONS **/

    // Buttons
    private JButton queryBtn; // Submits the query
    private JButton uploadBtn;

    // TextField Query
    private JTextField txtQuery; // USER Query

    // Menu
    private JMenuBar mb; // Menu bar
    private JMenu mnuFile; // File Entry on Menu bar
    private JMenuItem mnuFileOpen;
    private JMenuItem mnuFileSave;
    private JMenuItem mnuFileOptions;
    private JMenuItem mnuItemQuit; // Quit sub item
    private JMenu mnuHelp; // Help Menu entry
    private JMenuItem mnuItemAbout; // About Entry

    //West Panel stuff
    private static JList list;
    private JScrollPane listScroller;
    private JLabel uploadText;
    private JCheckBox uploadHTML;
    private JCheckBox uploadXML;
    private JCheckBox uploadJSON;

    //East Panel stuff
    private JTextArea user;
    private static JTextArea sysStatus;
    private JScrollPane statusScroll;
    private static JTextArea settings;
    private static String keywords;

    /*
     * Getters and setters for aesthetic options
     */
    public static boolean isSortedByLocation() {
        return sortByLocation;
    }

    public static void setSortedByLocation(boolean toSort) {
        sortByLocation = toSort;
    }

    public static int getMaxTweets() {
        return maxTweets;
    }

    public static void setMaxTweets(int num) {
        maxTweets = num;
    }

    public static boolean isOptionsOpen() {
        return isOptionsOpen;
    }

    public static void setOptionsOpen(boolean option) {
        isOptionsOpen = option;
    }

    /*
     * CONSTRUCTOR
     */
    public void init() {
        sysStatus = new JTextArea (1, 20);
        sysStatus.setEditable(false);
        northSubPnl1 = new JPanel(); 
        northSubPnl2 = new JPanel();
        northSubPnl3 = new JPanel();
        northPnl = new JPanel();
        eastPnl = new JPanel();
        westPnl = new JPanel();
        myQuery = new JLabel ("You have no queries yet");

        // TwitterDataList
        tweets = new Twitter.TwitterDataList ();

        // Buttons
        queryBtn = new JButton("Submit Query"); // Submits the query
        uploadBtn = new JButton("Upload Tweets"); // Uploads both XML and HTML files.

        // TextField Query
        txtQuery = new JTextField(); // USER Query

        // Menu
        mb = new JMenuBar(); // Menu bar
        mnuFile = new JMenu("File");
        mnuFileOpen = new JMenuItem("Open"); 
        mnuFileSave = new JMenuItem("Save");
        mnuFileOptions = new JMenuItem("Options");
        mnuItemQuit = new JMenuItem("Quit"); 
        mnuHelp = new JMenu("Help"); 
        mnuItemAbout = new JMenuItem("About"); 

        //West Panel stuff
        String[] tweetsString = new String[2000];
        list = new JList (tweetsString);
        uploadText = new JLabel("Upload as:");
        uploadHTML = new JCheckBox("HTML", false);
        uploadXML = new JCheckBox("XML", false);
        uploadJSON = new JCheckBox("JSON", false);

        //East Panel stuff
        user = new JTextArea("Username: ");
        keywords = new String ();
        statusScroll = new JScrollPane(sysStatus);
        statusScroll.setPreferredSize(new Dimension(200, 150));
        statusScroll.getVerticalScrollBar().setValue(statusScroll.getVerticalScrollBar().getMaximum());
        settings = new JTextArea(1, 20);


        // Set menu bar
        this.setJMenuBar(mb);

        //Build Menus
        this.buildMenus();
        // West Panel
        this.westPnlSetup();
        // East Panel
        this.eastPnlSetup();
        // Add objects to respective panels
        this.northPnlSetup();
        // Setup Main Frame
        this.setLayout();
        // Add Listeners
        this.addListeners();

        //Finish Launch
        Toolkit toolkit =  Toolkit.getDefaultToolkit ();
        Dimension dim = toolkit.getScreenSize();
        int width = (int)dim.getWidth();
        int height = (int)dim.getHeight();;                     //worry about location later. This is fine for testing purposes
        this.setBounds(width/2-400, height/2-400, 500, 800);    //Arbitrarily chosen for a 1600 by 900 screen 
        this.setVisible(true);
    }

    public class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == queryBtn) {        
                if (txtQuery.getText().length() > 2) {
                    myQuery.setText("Your current query: " + txtQuery.getText());
                    tweets = Twitter.SearchTweets.customSearch(txtQuery.getText()); // SEARCH
                    if (SearchTweets.getKeywords() != null) {
                        if (tweets.getSize() > 0)
                            tweets = Twitter.SearchTweets.sortByKeyword(tweets, SearchTweets.getKeywords()); // Sort right after.
                        else {
                            print("No tweets found!");
                            System.out.println("No tweets found!");
                        }
                    }
                    if (tweets.getSize() > 0) {
                        list.setListData(tweets.toArray(maxTweets));
                        sysStatus.append("Top Tweet: " + tweets.get(0).getTweet());
                        System.out.println("Top Tweet: " + tweets.get(0).getTweet());
                        //tweets.saveHTML(txtQuery.getText()); NOT SAVING BY DEFAULT ANY MORE
                    }
                    else {
                        print("No tweets found!");
                        System.out.println("No tweets found!");
                    }
                }
                else {
                    print("Please input at least a 3 letter word to begin a query.");
                    System.out.println("Please input at least a 3 letter word to begin a query.");
                }
            }

            // Not sure how to make directory path independent of system. change path to your local project to get open and save to work
            else if (e.getSource() == mnuFileOpen) {
                final JFileChooser fc = new JFileChooser("C:/Users/Meyer/Desktop/YATE/DataCollection");
                int returnVal = fc.showOpenDialog(mnuFile);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    tweets = Twitter.TwitterDataList.load(file.getName());
                    if (SearchTweets.getKeywords() != null) {
                        tweets = Twitter.SearchTweets.sortByKeyword(tweets, SearchTweets.getKeywords());
                    }
                    list.setListData(tweets.toArray(maxTweets));
                    myQuery.setText("Your current query: " + file.getName());
                }
            }

            else if (e.getSource() == mnuFileSave) {
                final JFileChooser fc = new JFileChooser("C:/Users/Meyer/Desktop/YATE/DataCollection");
                int returnVal = fc.showSaveDialog(mnuFile);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    tweets.writeHTML(file.getName());
                }
            }

            else if (e.getSource() == mnuFileOptions) {
                if (!isOptionsOpen()) {
                    new OptionsGUI();
                    setOptionsOpen(true);
                }           
            }

            else if (e.getSource() == uploadBtn) {
                if (uploadHTML.isSelected() || 
                        uploadXML.isSelected() || uploadJSON.isSelected()) {
                    try {
                        tweets.saveAll(txtQuery.getText(), uploadHTML.isSelected(), 
                                uploadXML.isSelected(), uploadJSON.isSelected());
                    } catch (SftpException exception) {
                        System.out.println("Could not upload. Try another time.");
                        print("Could not upload. Try another time.");
                    }
                }
                else if (tweets.getSize() ==0) {
                    JOptionPane.showMessageDialog(null, "There are no tweets to upload!");
                }
                else {
                    JOptionPane.showMessageDialog(null, "You need to select a file type in order to upload.");
                }
            }
        }
    }

    public class KeyChecker implements KeyListener {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) 
                queryBtn.doClick();
        }

        public void keyReleased(KeyEvent arg0) {
            //ignore
        }

        public void keyTyped(KeyEvent arg0) {
            //ignore
        }
    }

    public class ListListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {    
            try {
                user.setText(tweets.get(list.getSelectedIndex()).toString());
            } catch (ArrayIndexOutOfBoundsException error) {
                user.setText(tweets.get(list.getFirstVisibleIndex()).toString());
            }
        }
    }

    public class ListenMenuQuit implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);         
        }
    }

    public class ListenCloseWdw extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);         
        }
    }

    public void buildMenus () {
        mnuFile.add(mnuFileOpen);
        mnuFile.add(mnuFileSave);
        mnuFile.add(mnuFileOptions);
        mnuFile.add(mnuItemQuit);  // Create Quit line
        mnuHelp.add(mnuItemAbout); // Create About line
        mb.add(mnuFile);        // Add Menu items to form
        mb.add(mnuHelp);
    }


    /**
     * North Panel
     */
    public void northPnlSetup () {
        northPnl.setLayout(new BoxLayout (northPnl, BoxLayout.Y_AXIS));
        northSubPnl1.add(txtQuery);
        northSubPnl1.add(queryBtn);
        txtQuery.setColumns(20);
        northPnl.add(northSubPnl1);

    }
    /**
     * Sets Layout of GUI
     */

    public void setLayout () {
        // Setup Main Frame
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(northPnl, BorderLayout.NORTH);
        this.getContentPane().add(westPnl, BorderLayout.WEST);
        this.getContentPane().add(eastPnl, BorderLayout.EAST);
    }

    /*
     * Adds action listeners    
     */
    public void addListeners(){
        mnuItemQuit.addActionListener(new ListenMenuQuit());
        mnuFileOpen.addActionListener(new ButtonListener());
        mnuFileSave.addActionListener(new ButtonListener());
        mnuFileOptions.addActionListener(new ButtonListener());
        queryBtn.addActionListener(new ButtonListener());
        uploadBtn.addActionListener(new ButtonListener());
        list.addListSelectionListener(new ListListener());
        txtQuery.addKeyListener(new KeyChecker());

    }

    public void westPnlSetup () {
        westPnl.add(new JLabel ("Tweets"));
        westPnl.add(new JLabel("-------"));
        westPnl.setLayout(new BoxLayout (westPnl, BoxLayout.Y_AXIS));
        listScroller = new JScrollPane(list);
        listScroller.setPreferredSize(new Dimension(200, 100));
        list.setListData(tweets.toArray(maxTweets));
        list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        list.setLayoutOrientation(JList.VERTICAL);
        list.setVisibleRowCount(10);
        westPnl.add(listScroller);
        // setup upload button and panel for check boxes
        northSubPnl2.setLayout(new BoxLayout(northSubPnl2, BoxLayout.Y_AXIS));
        northSubPnl2.add(uploadBtn);
        uploadBtn.setAlignmentX(CENTER_ALIGNMENT);
        northSubPnl2.add(uploadText);
        uploadText.setAlignmentX(CENTER_ALIGNMENT);
        northSubPnl2.add(northSubPnl3);
        // setup check boxes
        northSubPnl3.setLayout(new BoxLayout(northSubPnl3, BoxLayout.X_AXIS));
        northSubPnl3.add(uploadHTML);
        northSubPnl3.add(uploadXML);
        northSubPnl3.add(uploadJSON);
        westPnl.add(northSubPnl2);
    }

    public void eastPnlSetup () {
        eastPnl.setLayout(new BoxLayout (eastPnl, BoxLayout.Y_AXIS));
        eastPnl.add(new JLabel("Metadata"));
        eastPnl.add(new JLabel("-------"));
        user = new JTextArea(1, 20);
        user.setPreferredSize(new Dimension (200,50));
        user.setEditable(false);
        user.append("User: \n");
        user.append("Date: \n");
        user.append("Location: \n");
        user.append("Relevance: \n");
        user.append("Tweet: ");
        user.setWrapStyleWord(true);
        user.setLineWrap(true);
        eastPnl.add(user);
        eastPnl.add(new JLabel("Settings"));
        eastPnl.add(new JLabel(new JLabel("-------").getText()));
        String keywords = "";
        for (int i = 0; i < SearchTweets.getKeywords().size(); i++) {
            keywords = SearchTweets.getKeywords().get(i) + ", ";
        }
        try {
            keywords = keywords.substring(0, keywords.length()-2);
        } catch (StringIndexOutOfBoundsException e) {
            keywords = ""; // There are no keywords if this happens.
        }

        /*
         * || THIS IS IF IN WHITE ||
         * ||                     ||
         * VV                     VV
         */
        settings.setPreferredSize(new Dimension (200,50));
        settings.setEditable(false);
        BasicGUI.setSettingsPaneValues();
        settings.setWrapStyleWord(true);
        settings.setLineWrap(true);
        eastPnl.add(settings);

        sysStatus.append(">");
        eastPnl.add(new JLabel("System Status       "));
        eastPnl.add(new JLabel(new JLabel("-------").getText()+"----"));
        sysStatus.setWrapStyleWord(true);
        sysStatus.setLineWrap(true);
        eastPnl.add(statusScroll);
        //this.redirectSystemStreams(); // REDIRECTS the text of console to the sysStatus box. NOT REAL TIME.

        /* END WHITE */
    }


    public static void setSettingsPaneValues () {
        keywords = new String();
        for (int i = 0; i < SearchTweets.getKeywords().size(); i++) {
            keywords += SearchTweets.getKeywords().get(i) + ", ";
        }
        if (SearchTweets.getKeywords().size()== 0) {
            keywords = "none";
        }
        else {
            try {
                keywords = keywords.substring(0, keywords.length()-2);
            } catch (StringIndexOutOfBoundsException e) {
                keywords = ""; // There are no keywords if this happens.
            }
        }

        settings.setText("Sort by location: " + isSortedByLocation() +
                "\nTimeout (in seconds): " + SearchTweets.getTimeout() + 
                "\nMaximum tweets shown: " + getMaxTweets() + 
                "\nKeywords: " + keywords);
        if (SearchTweets.getKeywords() != null && tweets.getSize() > 0) {
            tweets = Twitter.SearchTweets.sortByKeyword(tweets, SearchTweets.getKeywords());
        }
        list.setListData(tweets.toArray(maxTweets));
        list.repaint();
    }


    /**
     * returns string value of the keywords used
     * @return returns instance variable keywords
     * @author Taylor
     */
    public static String getKeywords () {
        return keywords;
    }
    /**
     * Updates the sysStatus textArea
     * @param text
     */
    private void updateTextArea(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                opposite++;
                opposite = opposite%2;
                if (!text.equalsIgnoreCase(".")){ // So I can do the ". . ." sequence to signify waiting.
                    if (opposite%2 == 1) {
                        sysStatus.append("\n>" + text);
                    }
                    else sysStatus.append(text);
                }
                else sysStatus.append(text);
            }
        });
    }


    /**
     * Allows other classes to print to sysStatus
     */
    public static void print (String text) {
        opposite++;
        opposite = opposite%2;
        if (!text.equalsIgnoreCase(".")){ // So I can do the ". . ." sequence to signify waiting.
            //if (opposite%2 == 1) {
            sysStatus.append("\n\n>" + text);
            sysStatus.repaint();
            //}
            //else sysStatus.append("\n" + text);
        }
        else sysStatus.append(text);

        sysStatus.invalidate();
        sysStatus.repaint();
    }

    /**
     * Redirects all console output to the textarea.
     */
    @SuppressWarnings("unused")
    private void redirectSystemStreams() {
        OutputStream out = new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                updateTextArea(String.valueOf((char) b));
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                updateTextArea(new String(b, off, len));
            }

            @Override
            public void write(byte[] b) throws IOException {
                write(b, 0, b.length);
            }
        };

        System.setOut(new PrintStream(out, true));
        System.setErr(new PrintStream(out, true));
    }
}

共有1个答案

钱卓君
2023-03-14

从此处开始:

Exception in thread "AWT-EventQueue-2" java.lang.AssertionError: java.lang.reflect.InvocationTargetException
    at twitter4j.TwitterFactory.<clinit>(TwitterFactory.java:76)
    ...
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    ... 42 more
Caused by: java.lang.ExceptionInInitializerError
    at twitter4j.internal.http.HttpClientWrapper.<init>(HttpClientWrapper.java:48)
    ... 47 more
Caused by: java.security.AccessControlException: access denied 
        (java.util.PropertyPermission twitter4j.http.httpClient read)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)

通常,小程序在限制性安全沙盒中运行。要获取所有属性或跨站点访问,小程序需要由您进行数字签名,并得到最终用户的信任(他们“在提示时单击确定”)。

一般提示:

  1. 将Java控制台配置为在加载小程序时弹出。你现在是“盲目飞行”
  2. 对于损坏的代码,请确保每个catch调用都可以丢弃。printStackTrace()
 类似资料:
  • 我试图运行一些单元测试使用Selenium网络驱动程序和C#。NET for Firefox浏览器,但我无法做到这一点(Chrome和IE11浏览器工作顺利)。 以下是我收集的信息: 操作系统:Windows 10企业版 Gecko驱动程序版本:geckodriver-v0。17.0-win32 Mozilla Firefox版本:54.0.1(32位) 已在“C:\LEO\SELENIUM C\

  • 问题内容: 我只是从Java小程序开始,然后发现了这个提供教程的网站。我输入了所有代码,但该小程序未显示在我的浏览器中:HelloWorld.java 这是我的HTML文件 我已经编译了Java文件(HelloWorld.class),并将所有文件保存在桌面上。我在Mac OS X美洲狮上使用的是野生动物园的最新版本。我在谷歌浏览器中尝试过,并说要安装一个插件,但是我没有做任何事情? 问题答案:

  • 我正在尝试dockerise一个反应应用程序。我正在使用以下Dockerfile来实现这一点。 还有,在我的包裹里。json开始脚本被定义为

  • 我正在使用WebDriver(Eclipse-Java)来自动化注册页面。点击“注册”按钮时,会显示一条“成功消息”,并需要验证该消息。 我可以在IE8中成功地做到这一点。但无法在Firefox中验证相同的内容。我试过不同的等待。D1.manage().timeouts().implicitlywait(60,TimeUnit.seconds); > WebDriverWait wait=新建We

  • 问题内容: 我正在尝试在我的网站上部署Java applet。我还需要签名,因为我需要访问剪贴板。我遵循了所有可以找到但没有成功的签名教程。到目前为止,这是我所做的: 在NetBeans中编写了一个applet。它在applet查看器中运行良好。 从中制作一个.jar文件。 通过执行以下操作创建证书: 像这样用jarsigner签名: 制作了一个包含以下内容的html文件: 当我打开该html文件