繁体   English   中英

部署Applet时java.lang.reflect.invocationtargetexception错误

[英]java.lang.reflect.invocationtargetexception Error when Deploying Applet

我有一个试图在html文件中部署的applet,当它实现时,它给了我java.lang.reflect.invocationtargetexception错误,我也不知道为什么。 (这是针对我的Java编程类的)

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.reflect.InvocationTargetException;
/* This program is meant to track how many times the user clicks btnNewButton and displays it in a JLabel.
 * The clicks can also be reset with the reset button, moving the clicker back to 0 */

public class MouseClicks extends JFrame {
    // Makes sure frame is shown
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MouseClicks frame = new MouseClicks();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public MouseClicks() {
    // Sets size of frame to 420 x 200, is resizeable though
        setSize(420,200);

    /* Displays instructions for user
     * Used HTML to make the instructions red in order to popout to the user more */
        textPane = new JLabel("<html><p><u><b>Instructions:</b></u></p><p><font color=\"red\">Click the button below for clicks to be counted and displayed,</font></p><p><font color=\"red\">press the Reset Clicks button to return click counter to 0.</font></p></html>");

        // Makes font SansSerif, plain, and size 15pt
        textPane.setFont(new Font("SansSerif", Font.PLAIN, 15));
        getContentPane().add(textPane);

        JButton btnNewButton = new JButton("Click Here to count Mouse Clicks!");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override           
            // Tracks clicks of mouse
            public void mouseClicked(MouseEvent e) {
            // Increases clicks everytime button is clicked
                clicks++;
            // When button is clicked, the counter is displayed on the Counter JLabel
                Counter.setText(header +clicks);
            }
        });
        getContentPane().add(btnNewButton);

        // Resets clicks back to 0
        JButton btnResetClicks = new JButton("Reset Clicks");
        btnResetClicks.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent c) {
                clicks = 0;
                Counter.setText(header +"0");
            }
        });
        getContentPane().add(btnResetClicks);


        Counter = new JLabel();
        getContentPane().add(Counter);

        // Makes layout flow, not a strict place where every component is
        getContentPane().setLayout(new FlowLayout());
    }
       // Static Label for # of clicks
       private static String header = "Clicks: ";

       // Sets default amount of clicks to 0
       private int clicks = 0;
       private JLabel Counter;
       private JLabel textPane;

    }

您的MouseClicks类需要扩展Applet或JApplet(在这种情况下,由于您正在使用Swing,因此应使用JApplet)。

所以改变这个:

public class MouseClicks extends JFrame {

public class MouseClicks extends JApplet {

并添加import javax.swing.JApplet; 到文件顶部的其余导入。

有关更多详细信息,请参见此处: http : //docs.oracle.com/javase/tutorial/deployment/applet/index.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM