我有这个小程序,我正在尝试在html文件中部署,当它发生时它给了我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;
}
答案 0 :(得分:0)
您的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